【问题标题】:How to order JFrame components?如何订购 JFrame 组件?
【发布时间】:2016-03-24 09:50:57
【问题描述】:

我正在尝试创建用户界面,但我无法创建有序组件。如何像第二张图片一样订购我的 JPanel 组件,有什么想法吗?

我的代码结果是

我想这样创作

这是我的java代码

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


public class ApplicationWindow2 {

    private JFrame mainJFrame;
    private JPanel topPanel;
    private JPanel centerPanel;
    private JPanel bottomPanel;


    public ApplicationWindow2(){

        mainJFrame = new JFrame("setupbox");

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        mainJFrame.setMaximumSize(new Dimension(600,600));
        mainJFrame.setMinimumSize(new Dimension((int) mainJFrame.getMaximumSize().getWidth()-1,(int) mainJFrame.getMaximumSize().getHeight()-1));
        mainJFrame.setResizable(false);
        mainJFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        int windowHeight = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        int windowWidth = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        mainJFrame.setLocation((windowWidth-mainJFrame.getWidth())/2, (windowHeight-mainJFrame.getHeight())/2);//

        // components
        mainJFrame.getContentPane().add(getTopPanel(), BorderLayout.PAGE_START);
        mainJFrame.getContentPane().add(getCenterPanel(), BorderLayout.CENTER);
        mainJFrame.getContentPane().add(getBottomPanel(), BorderLayout.PAGE_END);

        mainJFrame.setVisible(true);
        mainJFrame.pack();

    }

    private JPanel getTopPanel(){

        if (topPanel == null){

            topPanel = new JPanel();
            topPanel.add(new JLabel("top panel"));
        }

        return topPanel;
    }

    private JPanel getBottomPanel(){

        if(bottomPanel == null){
            bottomPanel = new JPanel();
            bottomPanel.add(new Label("bottom panel"));
        }

        return bottomPanel;
    }

    private JPanel getCenterPanel(){

        if(centerPanel == null){

            centerPanel = new JPanel();
            centerPanel.setSize(new Dimension((int) mainJFrame.getMaximumSize().getWidth()-100,(int) mainJFrame.getMaximumSize().getHeight()-100));
            centerPanel.setBorder(BorderFactory.createLineBorder(Color.black));

            BoxLayout boxLayout = new BoxLayout(centerPanel, BoxLayout.Y_AXIS);
            centerPanel.setLayout(boxLayout);

            centerPanel.add(getTest());
            centerPanel.add(getTest1());
            centerPanel.add(Box.createVerticalStrut(30));
            centerPanel.add(getTest());

        }

        return centerPanel;
    }

    private JPanel getTest(){

        JPanel testPanel = new JPanel(new GridBagLayout());
        testPanel.setMaximumSize(new Dimension(getCenterPanel().getWidth(), 40));
        testPanel.setBorder(BorderFactory.createLineBorder(Color.black));

        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.NONE;
        c.gridy = 0;
        c.insets = new Insets(5, 5, 5, 5);

        JCheckBox box1 = new JCheckBox("asdasdasdasdasdasd");
        c.gridx = 0;
        c.anchor = GridBagConstraints.WEST;
        testPanel.add(box1, c);

        JLabel l3 = new JLabel("lbl3asdssssssssssssssssssssssssss");
        l3.setPreferredSize(new Dimension(20,30));
        l3.setBorder(BorderFactory.createLineBorder(Color.black));
        c.gridx = 1;
        c.anchor = GridBagConstraints.LINE_END;
        testPanel.add(l3, c);

        return testPanel;

    }

    private JPanel getTest1(){

        JPanel testPanel = new JPanel(new GridBagLayout());
        testPanel.setMaximumSize(new Dimension(getCenterPanel().getWidth(), 40));
        testPanel.setBorder(BorderFactory.createLineBorder(Color.black));

        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.NONE;
        c.gridy = 0;
        c.insets = new Insets(5, 5, 5, 5);

        JCheckBox box1 = new JCheckBox("aass1133");
        c.gridx = 0;
        c.anchor = GridBagConstraints.LINE_START;
        testPanel.add(box1, c);

        JLabel l3 = new JLabel("lbl3as");
        l3.setPreferredSize(new Dimension(20,30));
        l3.setBorder(BorderFactory.createLineBorder(Color.black));
        c.gridx = 1;
        c.anchor = GridBagConstraints.LINE_END;
        testPanel.add(l3, c);

        return testPanel;
    }
}

【问题讨论】:

  • JTable? MigLayout?
  • 我们可以在 migLayout 中设置单元格的不同大小吗?以及我们如何导入intellij?看起来不错
  • 我个人没有用过MigLayout,但有人建议它可以解决这类问题。基本上,您需要下载 Jar 文件并将其包含在您的项目中。也不熟悉intellij:P
  • 感谢您的建议,我会试试看:D
  • 有一个 MigLayout Demo 。其中,有几个示例如何设置单元格大小。例如,您可以使用某个组件的百分比、使用固定的单元格大小等。

标签: java swing layout-manager grid-layout gridbaglayout


【解决方案1】:

我用 MigLayout 解决了这个问题,这是我的代码

private JPanel getTest1(){

        MigLayout layout = new MigLayout("debug");
        JPanel content = new JPanel(layout);
        content.setMaximumSize(new Dimension(getCenterPanel().getWidth(), 40));

        JCheckBox a1 = new JCheckBox("d");
        content.add(a1, "gapright 10, height 30, width 250, cell 0 0");

        JLabel label = new JLabel();
        label.setBackground(Color.RED);
        label.setOpaque(true);
        content.add(label, "gapright 10, height 30, width 30, cell 1 0");

        JButton btn = new JButton("try again");
        content.add(btn,"height 30, width 80, cell 2 0" );

        return content;

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 2023-03-05
    • 1970-01-01
    相关资源
    最近更新 更多