【问题标题】:Nothing gets shown in the JFrameJFrame 中没有显示任何内容
【发布时间】:2014-09-29 00:22:14
【问题描述】:

我想创建一个 gui,它在顶部有两个水平组件(一个组合框和一个按钮),在底部我想添加几个组件。我创造了这样的一切:

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;

public class minimumExample extends JFrame {

    private JButton addItem;

    private JComboBox itemBox;

    private String[] itemSelect = { "test1", "test2" };

    private JPanel addUpperPane;

    private JPanel addLowerPane;


    public void createControlPane() {

        setLayout(new BorderLayout());

        addUpperPane = new JPanel(new BorderLayout(5, 5));
        addLowerPane = new JPanel(new GridLayout(0, 1));

        addItem = new JButton("Add item");

        itemBox = new JComboBox(itemSelect);

        addItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                getContentPane().setLayout(new GridLayout(0, 1));

                if(itemBox.getSelectedItem().toString().equals("test1")) {
                    addLowerPane.add(new Button("Lolonator"));
                    validate();
                    repaint();

                }

            }
        });;

        addUpperPane.add(itemBox);
        addUpperPane.add(addItem);
        addUpperPane.add(new JSeparator(JSeparator.HORIZONTAL));

        //put everything together

        add(addUpperPane);
        add(addLowerPane);

        repaint();

    }

    private void makeLayout() {

        setTitle("Test App");
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(1000, 500));

        createControlPane();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);

    }


    /**
     * starts the GUI
     */
    public void start() {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                makeLayout();   
            }
        });
    }

    public static void main(String[] args) throws IOException {
        minimumExample ex = new minimumExample();
        ex.start();
    }

}

我的问题是没有显示任何内容,而且我也认为布局不正确。有什么建议我应该改变什么来解决我的问题?

感谢您的回答!

更新

这是我的 gui 应该是什么样子的简单线框图:

更新 2

将所有内容更改为:

addUpperPane.add(itemBox, BorderLayout.EAST);
        addUpperPane.add(addItem, BorderLayout.WEST);
        addUpperPane.add(new JSeparator(JSeparator.HORIZONTAL));

        //put everything together

        add(addUpperPane, BorderLayout.NORTH);
        add(addLowerPane, BorderLayout.SOUTH);

告诉我:

有什么建议可以消除这个差距吗?

【问题讨论】:

  • BorderLayout 的每个区域只能添加一个组件。 AFAIU 将显示上述所有内容的唯一组件是分隔符。提供 ASCII 艺术或简单的图形,说明 GUI 应如何以默认大小显示,并且(如果可调整大小)具有额外的宽度/高度。
  • @AndrewThompson 感谢您的回答!改变这个:`setLayout(new FlowLayout()); addUpperPane = new JPanel(new FlowLayout());` 不会改变我的程序中的任何内容。有什么建议我应该改变吗?
  • 提供 ASCII 艺术或简单的图形,说明 GUI 应如何以默认大小显示,并且(如果可调整大小)具有额外的宽度/高度。
  • @AndrewThompson 请看看我的更新!
  • setPreferredSize(new Dimension(1000, 500));Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Swing?(是的。)这里我们应该在添加所有组件后调用pack()

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


【解决方案1】:

由于您使用了BorderLayout,因此您需要指定布局中每个组件的位置,您所做的就是仅将所有组件添加到布局的同一位置,默认情况下为CENTER

解决方案:

    addUpperPane.add(itemBox,BorderLayout.EAST);
    addUpperPane.add(addItem,BorderLayout.WEST);
    addUpperPane.add(new JSeparator(JSeparator.HORIZONTAL));

    //put everything together

    add(addUpperPane,BorderLayout.NORTH);
    add(addLowerPane,BorderLayout.SOUTH);

这也不会因为setLayout(new BorderLayout()); JFrame 的默认布局已经是BorderLayout 所以不需要再次设置布局。

编辑:

如果您希望您的组件并排放置,那么FlowLayout 是您的最佳选择:

addUpperPane = new JPanel(); //default component of JPanel is FlowLayout
addUpperPane.add(itemBox);
addUpperPane.add(addItemT);

编辑编号 2:

问题:

    getContentPane().setLayout(new GridLayout(0, 1)); //remove it

样本:

addItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            if(itemBox.getSelectedItem().toString().equals("test1")) {
                addLowerPane.add(new Button("Lolonator"));
                revalidate();
            }

        }
    });;

【讨论】:

  • "..JFrame 的默认布局已经是BorderLayout,所以不需要重新设置布局。" 默认布局JFrame 使用成为 FlowLayout。所以我说'拧紧默认设置并设置一个'。另请注意,最好在基于罗盘的常量上使用区域设置敏感的PAGE_STARTLINE_START 等。
  • @AndrewThompson 我记得你以前对我说过,:))。
  • @Rod_Algonquin 感谢您的回答!在我的 gui 中使用你的布局时,为什么我有这么大的差距?对于上部窗格,我使用了默认的流布局。
  • 感谢您的回答!我也更新了我的形象。有什么建议为什么上下窗格之间的差距如此之大?
  • 谢谢!刚改成JScrollable,不支持GridBack,这里有什么推荐的布局吗?
【解决方案2】:

Rod_Algonquin 的解法是对的。 如果您要构建更多 GUI,我可以向您推荐 MigLayout。它可以帮助您编写漂亮的 GUI,减少可读代码的行数。

他们的主页:http://miglayout.com/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-01
    • 2020-03-24
    • 2014-07-21
    • 2016-02-19
    • 2020-06-16
    • 2021-02-05
    • 2018-10-10
    相关资源
    最近更新 更多