【问题标题】:Swing: Need a FourSquare and Bottom Button LayoutSwing:需要 FourSquare 和底部按钮布局
【发布时间】:2013-08-07 14:26:26
【问题描述】:

我正在尝试制作一个包含 4 个正方形的布局,这些正方形组成一个更大的正方形,下方的一个按钮延伸到整个正方形。

我尝试GridLayout 完美地创建了正方形,但按钮没有在屏幕上伸展。

我也试过GridBagLayout,但有间距,正方形很小,很糟糕。

PS:按钮是button1,button2,button3,button4,用于方形,start为底部按钮。

我还希望正方形为 400x400。

我的代码GridLayout:

this.setLayout(new GridLayout(3,2));




        button1 = new JButton();
        //button1.setBackground(Color.green);
        button1.setBounds(0, 0, 200, 200);

        button2 = new JButton();
        //button2.setBounds(0, 200, 200, 200);
        button2.setBackground(Color.red);

        button3 = new JButton();
        //button3.setBackground(Color.yellow);
        button3.setBounds(200, 0, 200, 200);

        button4 = new JButton();
        //button4.setBounds(200, 200, 200, 200);
        button4.setBackground(Color.blue);

        start = new JButton("Start");
        //start.setBounds(300, 300, 100, 100);

        this.add(button1);
        this.add(button2);
        this.add(button3);
        this.add(button4);
        this.add(start);

【问题讨论】:

    标签: java swing


    【解决方案1】:

    尝试使用组件布局...BorderLayoutGridLayout 的组合应该可以为您提供所需的结果。

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class FourSquare {
    
        public static void main(String[] args) {
            new FourSquare();
        }
    
        public FourSquare() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JPanel squarePane = new JPanel(new GridLayout(2, 2));
                    JPanel controlPane = new JPanel(new BorderLayout());
    
                    squarePane.add(new JButton("1"));
                    squarePane.add(new JButton("2"));
                    squarePane.add(new JButton("3"));
                    squarePane.add(new JButton("4"));
    
                    controlPane.add(squarePane);
                    controlPane.add(new JButton("Bottom"), BorderLayout.SOUTH);
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(controlPane);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      我还不能作为 cmets 提问,但我猜你想要一个由四个使用 gridlayout 的按钮组成的正方形,然后在其下方延伸一个更长的按钮?

      (像这样?)

      [1] [2]
      [3] [4]
      [开始]

      对我来说,我会这样做:

      //assuming this code is in a class that extends JFrame
      this.setLayout( new BorderLayout() );
      
      JPanel pnlSquare = new JPanel( new GridLayout( 2 , 2 ) );
      button1.setPreferredSize( new Dimension( 200 , 200 ) );
      pnlSquare.add( button1 );
      button2.setPreferredSize( new Dimension( 200 , 200 ) );
      pnlSquare.add( button2 );
      button3.setPreferredSize( new Dimension( 200 , 200 ) );
      pnlSquare.add( button3 );
      button4.setPreferredSize( new Dimension( 200 , 200 ) );
      pnlSquare.add( button4 );
      this.add( pnlSquare , BorderLayout.CENTER );
      
      this.add( start , BorderLayout.SOUTH );
      

      让我知道这是否可以。

      另外,如果这是一个学校项目,你最好确保你能解释代码,否则你可能会因为抄袭而惹上麻烦。

      【讨论】:

      • 有没有办法满足正方形必须是 400 x 400 的要求?感谢您的格式编辑和链接 - 学到了一些新东西!
      • 是的,这太可怕了。基本上,你需要重写getPreferredSize 方法并返回你想要的结果。一般来说,我会避免这种混乱,但这就是我:P
      • 如果你真的想要硬编码的尺寸(注意:你几乎从不想要:-),在 的级别上指定它们LayoutManager(相对于组件)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-06
      • 2016-04-17
      • 1970-01-01
      • 2019-11-09
      • 1970-01-01
      相关资源
      最近更新 更多