如何从窗格中删除这些空间?

时间:2013-11-04 12:45:19

标签: java swing jpanel gridbaglayout jmenubar

以下是关键代码:

  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());

结果如下:

enter image description here

我不打算在这里使用函数pack()。顶部和空间留下的空间让我很困惑。我怎样才能消除这些空间?谢谢。 虽然使用pack();可以包裹空间。我想弄清楚这些左侧空间的原因。

4 个答案:

答案 0 :(得分:1)

我自己摇摆不好。但你有没有尝试将weightx和/或weighty改为1而不是0.正如这个问题所述:SO Question只是尝试一下。

答案 1 :(得分:1)

尝试将带有空格(非空)的标签添加到最后一行(对于gridy = 2)并将 weighty 属性设置为1.此技巧必须有效

答案 2 :(得分:1)

将广告pane输入JFrame并使用setJMenuBar类中的JFrame设置菜单栏。 这是示例程序

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);
    }

}

答案 3 :(得分:1)

  

感谢您的代码。我只是想尝试另一种方式来设置   MenuBar使用布局