1

キーコードは次のとおりです。

  public GridBagConstraints setGridBagC(int gx,int gy,double wx,double wy,int gFill,int gAnchor){
        GridBagConstraints c = new GridBagConstraints();
        c.fill=gFill;
        c.anchor=gAnchor;
        c.gridx=gx;
        c.gridy=gy; 
        c.weightx=wx;
        c.weighty=wy;
        return c;
}

public int initFrame(Container pane){
        setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);//Set the size of the frame.
        setResizable(false);//You're not allowed to resize the frame.   
        pane.setLayout(new GridBagLayout());    
        GridBagConstraints c;       c=setGridBagC(0,0,0,0,GridBagConstraints.HORIZONTAL,GridBagConstraints.NORTH);
        JMenu ArchJ=new JMenu("Console");
        JMenu menuTwo=new JMenu("MenuTwo");
        JMenu menuThree=new JMenu("MenuThree");
        JMenu menuFour=new JMenu("MenuFour");
        JMenuBar mbar=new JMenuBar();
        mbar.add(ArchJ);
        JMenuItem OneItOne=new JMenuItem("1");
        OneItOne.setPreferredSize(new Dimension(100,30));
        JMenuItem OneItTwo=new JMenuItem("2");
        OneItTwo.setSize(100, 30);
        ArchJ.add(OneItOne);
        ArchJ.addSeparator();
        ArchJ.add(OneItTwo);
        mbar.add(menuTwo);
        mbar.add(menuThree);
        mbar.add(menuFour);
        pane.add(mbar,c);
        JButton bt=new JButton("kd");
        c=setGridBagC(0,1,1.0,0,GridBagConstraints.BOTH,GridBagConstraints.NORTH);
        pane.add(bt,c);     
        //pack();//Do not use pack()
        return 1;
    }

そして、これは の開始ですframe:

RobotCtrlFrame frame=new RobotCtrlFrame();
frame.initFrame(frame.getContentPane());

結果は次のとおりです。

ここに画像の説明を入力

ここで関数を使用するつもりはありませんpack()。上部と下部に残されたスペースは、私を混乱させます。これらのスペースを削除するにはどうすればよいですか? ありがとう。使用pack();するとスペースを埋めることができますが、これらのスペースが残っている理由を理解したいと思います。

4

4 に答える 4

1

キャストpaneを入力し、クラスで使用しJFrameてメニューバーを設定します。サンプルプログラムはこちらsetJMenuBarJFrame

package com.sample.ui;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class TestJFrame extends JFrame {

    private static final long serialVersionUID = 3509944903365488258L;

    public TestJFrame() {
        super();
        initFrame(this);
    }

    public GridBagConstraints setGridBagC(int gx, int gy, double wx, double wy,
            int gFill, int gAnchor) {
        GridBagConstraints c = new GridBagConstraints();
        c.fill = gFill;
        c.anchor = gAnchor;
        c.gridx = gx;
        c.gridy = gy;
        c.weightx = wx;
        c.weighty = wy;
        return c;
    }

    public int initFrame(Container pane) {
        setSize(300, 300);// Set the size of the frame.
        setResizable(false);// You're not allowed to resize the frame.
        pane.setLayout(new GridBagLayout());
        GridBagConstraints c;
        c = setGridBagC(0, 0, 0, 0, GridBagConstraints.HORIZONTAL,
                GridBagConstraints.NORTH);
        JMenu ArchJ = new JMenu("Console");
        JMenu menuTwo = new JMenu("MenuTwo");
        JMenu menuThree = new JMenu("MenuThree");
        JMenu menuFour = new JMenu("MenuFour");
        JMenuBar mbar = new JMenuBar();
        mbar.add(ArchJ);
        JMenuItem OneItOne = new JMenuItem("1");
        OneItOne.setPreferredSize(new Dimension(100, 30));
        JMenuItem OneItTwo = new JMenuItem("2");
        OneItTwo.setSize(100, 30);
        ArchJ.add(OneItOne);
        ArchJ.addSeparator();
        ArchJ.add(OneItTwo);
        mbar.add(menuTwo);
        mbar.add(menuThree);
        mbar.add(menuFour);

        JFrame jFrame = (JFrame) pane;
        jFrame.setJMenuBar(mbar);

        JButton bt = new JButton("kd");
        c = setGridBagC(0, 1, 1.0, 0, GridBagConstraints.BOTH,
                GridBagConstraints.NORTH);
        pane.add(bt, c);
        // pack();//Do not use pack()
        return 1;
    }

    public static void main(String[] args) {
        TestJFrame testJFrame = new TestJFrame();
        testJFrame.setVisible(true);
    }

}
于 2013-11-04T13:24:23.373 に答える
1

スペース(空ではない)を含むラベルを最後の行に追加して(gridy = 2の場合)、weightyプロパティを1に設定してみてください。このトリックは機能するはずです

于 2013-11-04T13:09:42.060 に答える