如果框不够大,面板会相互重叠

时间:2012-08-12 22:32:39

标签: java swing user-interface layout grid-layout

我有这个gui;当高度不够大时,窗格会相互重叠。我必须设置它至少200,所以我可以完全看到这两行;但是当它设置为200时,我最后会有一个很大的空行,我不希望这样。我怎么能解决这个问题?感谢。

import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.*;

public class MyFrame extends JFrame {

    JButton panicButton;
    JButton dontPanic;
    JButton blameButton;
    JButton newsButton;
    JButton mediaButton;
    JButton saveButton;
    JButton dontSave;

    public MyFrame() {
        super("Crazy App");
        setSize(400, 150);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel row1 = new JPanel();
        panicButton = new JButton("Panic");
        dontPanic = new JButton("No Panic");
        blameButton = new JButton("Blame");
        newsButton = new JButton("News");
        //adding first row
        GridLayout grid1 = new GridLayout(4, 2, 10, 10);
        setLayout(grid1);
        FlowLayout flow1 = new FlowLayout(FlowLayout.CENTER, 10, 10);
        row1.setLayout(flow1);
        row1.add(panicButton);
        row1.add(dontPanic);
        row1.add(blameButton);
        row1.add(newsButton);
        add(row1);
        //adding second row
        JPanel row2 = new JPanel();
        mediaButton = new JButton("Blame");
        saveButton = new JButton("Save");
        dontSave = new JButton("No Save");
        GridLayout grid2 = new GridLayout(3, 2, 10, 10);
        setLayout(grid2);
        FlowLayout flow2 = new FlowLayout(FlowLayout.CENTER, 10, 10);
        row2.setLayout(flow2);        
        row2.add(mediaButton);
        row2.add(saveButton);
        row2.add(dontSave);
        add(row2);       

        setVisible(true);       

    }

    public static void main(String[] args) {
        MyFrame frame = new MyFrame();
    }   
}

1 个答案:

答案 0 :(得分:3)

  1. 原始代码在两个不同的场合设置一个面板的布局。为清楚起见,请在构造函数中设置一次。
  2. 第二个布局指定了3行
  3. 调用顶级容器上的pack(),使GUI缩小到组件所需的最小值。
  4. 最终结果

    enter image description here


    import java.awt.FlowLayout;
    import javax.swing.*;
    import java.awt.*;
    
    public class MyFrame17 extends JFrame {
    
        JButton panicButton;
        JButton dontPanic;
        JButton blameButton;
        JButton newsButton;
        JButton mediaButton;
        JButton saveButton;
        JButton dontSave;
    
        public MyFrame17() {
            super("Crazy App");
            setLayout(new GridLayout(2, 2, 10, 10));
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel row1 = new JPanel();
            panicButton = new JButton("Panic");
            dontPanic = new JButton("No Panic");
            blameButton = new JButton("Blame");
            newsButton = new JButton("News");
            FlowLayout flow1 = new FlowLayout(FlowLayout.CENTER, 10, 10);
            row1.setLayout(flow1);
            row1.add(panicButton);
            row1.add(dontPanic);
            row1.add(blameButton);
            row1.add(newsButton);
            add(row1);
            //adding second row
            JPanel row2 = new JPanel();
            mediaButton = new JButton("Blame");
            saveButton = new JButton("Save");
            dontSave = new JButton("No Save");
            FlowLayout flow2 = new FlowLayout(FlowLayout.CENTER, 10, 10);
            row2.setLayout(flow2);        
            row2.add(mediaButton);
            row2.add(saveButton);
            row2.add(dontSave);
            add(row2);       
    
            pack();
            setVisible(true);       
        }
    
        public static void main(String[] args) {
            MyFrame17 frame = new MyFrame17();
        }   
    }
    

    进一步提示

    1. 不要扩展框架,只使用一个实例。
    2. 在面板中构建整个GUI,然后可以将其添加到框架,小程序,对话框中。
    3. 在开发测试类时,给它们一个比MyFrame更明智的名称。要添加的好词是Test,然后考虑一下正在测试的内容。这是关于按钮的布局,因此ButtonLayoutTest可能是一个好名字。
    4. 应该在EDT上启动GUI。