简体   繁体   English

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

[英]Panels overlap each other when box not big enough

I have this gui; 我有这个gui; and when the height is not big enough the panes will overlap each other. 当高度不够大时,窗格会相互重叠。 I have to set it at least 200, so I can completely see the two rows; 我必须设置它至少200,所以我可以完全看到这两行; but when it is set at 200, then I have like a big empty row at the end, and I don't want that. 但是当它设置为200时,我最后会有一个很大的空行,我不希望这样。 How could I fix this? 我怎么能解决这个问题? Thanks. 谢谢。

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. The original code set the layout for one panel on two separate occasions. 原始代码在两个不同的场合设置一个面板的布局。 For clarity, set it once in the constructor. 为清楚起见,请在构造函数中设置一次。
  2. The 2nd layout specified 3 rows 第二个布局指定了3行
  3. Call pack() on the top-level container to have the GUI reduce to the minum sze needed for the components. 在顶级容器上调用pack()以使GUI减少到组件所需的最小值。

End result 最终结果

在此输入图像描述


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

Further tips 进一步提示

  1. Don't extend frame, just use an instance of one. 不要扩展框架,只使用一个实例。
  2. Build the entire GUI in a panel which can then be added to a frame, applet, dialog.. 在面板中构建整个GUI,然后可以将其添加到框架,小程序,对话框中。
  3. When developing test classes, give them a more sensible name than MyFrame . 在开发测试类时,给它们一个比MyFrame更合理的名称。 A good word to add is Test , then think about what is being tested. 要添加的一个好词是Test ,然后考虑正在测试的内容。 This is about the layout of buttons, so ButtonLayoutTest might be a good name. 这是关于按钮的布局,所以ButtonLayoutTest可能是个好名字。
  4. GUIs should be started on the EDT. GUI应该在EDT上启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM