Java GUI 布局建议

标签 java swing user-interface layout jframe

对于学校作业,我需要 2 个面板。

右侧需要是 3x3 带按钮(我将其设为黑色,以便在设置 GUI 时易于识别),左侧需要 1 个标签和 4 个按钮。

标签应显示当前图片的名称(随机放置在 3x3 网格中的按钮上),3 个用于随机放置图像的按钮,以及一个用于清除图像的按钮。我不需要逻辑方面的帮助,我可以完成那部分。

我在设置面板时遇到问题,所以它看起来还不错。我正在考虑将其设为 1x5 网格,但我不知道该怎么做。我花了几个小时寻找如何做到这一点以及尝试我自己的东西(注意注释掉的东西)。任何帮助将不胜感激。

public class Characters extends JFrame {

    private Container pane;
    private JButton Button1, Button2, Button3, Button4, Button5, Button6;
    private JButton Button7, Button8, Button9;
    private JButton BMolly, BOctavious, BJimmy, BClear;
    private ImageIcon Molly, Octavious, Jimmy;
    private JLabel LName;

    public Characters() {
        setTitle("Characters");
        pane = getContentPane();
        pane.setLayout(new GridLayout(3, 3));

        Button1 = new JButton((Icon) Button1);
        Button1.setBackground(Color.BLACK);
        pane.add(Button1);
        Button2 = new JButton((Icon) Button2);
        Button2.setBackground(Color.BLACK);
        pane.add(Button2);
        Button3 = new JButton((Icon) Button3);
        Button3.setBackground(Color.BLACK);
        pane.add(Button3);
        Button4 = new JButton((Icon) Button4);
        Button4.setBackground(Color.BLACK);
        pane.add(Button4);
        Button5 = new JButton((Icon) Button5);
        Button5.setBackground(Color.BLACK);
        pane.add(Button5);
        Button6 = new JButton((Icon) Button6);
        Button6.setBackground(Color.BLACK);
        pane.add(Button6);
        Button7 = new JButton((Icon) Button7);
        Button7.setBackground(Color.BLACK);
        pane.add(Button7);
        Button8 = new JButton((Icon) Button8);
        Button8.setBackground(Color.BLACK);
        pane.add(Button8);
        Button9 = new JButton((Icon) Button9);
        Button9.setBackground(Color.BLACK);
        pane.add(Button9);
        LName = new JLabel(" ");
        pane.add(LName);
        BMolly = new JButton("Molly");
        pane.add(BMolly);
        BOctavious = new JButton("Octavious");
        pane.add(BOctavious);
        BJimmy = new JButton("Jimmy");
        pane.add(BJimmy);
        BClear = new JButton("Clear");
        pane.add(BClear);
        pack();
        setResizable(false);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(final String[] args) {
        new Characters();
    }

}

最佳答案

您需要的是两个不同的面板。

pane = new JPanel();       //instead of pane = getContentPane();

//set your Layout
//add the 9 buttons
//...

add(pane, BorderLayout.CENTER);  //add panel to the jframe


pane = new JPanel();      //creat new panel 

//set your Layout
//add the other 4 buttons + label
//...

add(pane, BorderLayout.EAST);  //add panel to the jframe

如果它仍然不起作用,我可以添加完整的代码。

关于Java GUI 布局建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21753769/

相关文章:

java - 在 Android 应用程序中使用 XML 更改进度条颜色

java - 如何避免 Swing 中的无限更新循环?

java - JTextField 中的文本未完全显示

Java - GridBagLayout位置JLabel

c - GTK+ 中的六边形按钮

java - 如何检查字符串是否为int

java - 为什么多线程之间值不更新?

java - Jacoco 仅在我的系统上失败,并出现有关 StringUtils 的奇怪错误

java - 如何使用循环绘制矩形?

objective-c - 带有图像和文本的 NSMenuItem