1

我有这个 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();
    }   
}
4

1 回答 1

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

最终结果

在此处输入图像描述


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. GUI 应该在 EDT 上启动。
于 2012-08-12T22:56:00.923 回答