简体   繁体   English

Java GUI布局建议

[英]Java GUI Layout Suggestions

For a school assignment I need to have 2 panels. 对于学校作业,我需要有2个小组。

The right needs to be 3x3 with buttons (which I have made black for easy identification when setting up the GUI) and the left with 1 label and 4 buttons. 右边需要3x3的按钮(我设置了黑色以便在设置GUI时识别),左边有1个标签和4个按钮。

Label should display the name of the current picture (placed randomly on a button in the 3x3 grid), 3 buttons to place images randomly, and one button to clear them off. 标签应显示当前图片的名称(随机放置在3x3网格中的按钮上),3个按钮可随机放置图像,还有一个按钮可将其清除。 I don't need help with the logic, I can do that part. 我不需要逻辑方面的帮助,我可以做那个部分。

I am having trouble setting up the panel so it looks somewhat decent. 我在设置面板时遇到了麻烦,所以它看起来有些像样。 I was thinking of making it a 1x5 grid but I don't know how to do that. 我想把它变成1x5格,但我不知道该怎么做。 I have spent multiple hours looking up how to do it as well as trying out my own stuff (notice the commented out stuff). 我花了几个小时来查找如何做以及尝试我自己的东西(注意注释掉的东西)。 Any help would be greatly appreciated. 任何帮助将不胜感激。

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

}

What you need are two different panels. 你需要的是两个不同的面板。

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

If it still dont work i can add the full code. 如果它仍然无法工作,我可以添加完整的代码。

Very good starting point for dealing with layout managers is Java documentation . 处理布局管理器的非常好的起点是Java文档 For your need looks like BorderLayout manager should be good choice. 对于你的需求看起来像BorderLayout经理应该是不错的选择。

Read how to use layout managers with examples, it gives you first look. 阅读如何使用布局管理器的示例,它首先给你看看。

There's a few issues with your code, by convention variable names start with a lower letter, and you should only do one thing per line of code (even declaring variables). 您的代码存在一些问题,按惯例变量名称以较低的字母开头,每行代码应该只做一件事(甚至声明变量)。 Also you shouldn't "extend" a class unless you are going to extend the functionality of it, if all you are going to do is use it, then just create your own instance of the JFrame . 另外你不应该“扩展”一个类,除非你要扩展它的功能,如果你要做的就是使用它,那么只需创建你自己的JFrame实例。 (Oh, and when you're posting code on fourms looking for help, if you've felt the need to add an annotation to ignore an unused warning, then you probably don't need to post it in your question either :p ) (哦,当你在4ms上发布代码寻求帮助时,如果你觉得需要添加注释来忽略未使用的警告,那么你可能不需要在你的问题中发布它:p)

For your problem you need to consider using multiple layouts (they can even be embedded in each other to provide some very complex effects) - Swing layouts 对于您的问题,您需要考虑使用多个布局(它们甚至可以相互嵌入以提供一些非常复杂的效果) - Swing布局

I've used a borderLayout and a BoxLayout to achieve something along the lines of what you require. 我已经使用了borderLayoutBoxLayout来实现你所需要的东西。

public class Characters {

    private JFrame frame;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    private JButton button5;
    private JButton button6;
    private JButton button7;
    private JButton button8;
    private JButton button9;
    private JButton mollyButton;
    private JButton octaviousButton;
    private JButton jimmyButton;
    private JButton clearButton;

    public Characters() {
        frame = new JFrame("Characters");
        JPanel rightPanel = new JPanel(new GridLayout(3, 3));
        button1 = new JButton();
        button1.setBackground(Color.BLACK);
        rightPanel.add(button1);
        button2 = new JButton();
        button2.setBackground(Color.BLACK);
        rightPanel.add(button2);
        button3 = new JButton();
        button3.setBackground(Color.BLACK);
        rightPanel.add(button3);
        button4 = new JButton();
        button4.setBackground(Color.BLACK);
        rightPanel.add(button4);
        button5 = new JButton();
        button5.setBackground(Color.BLACK);
        rightPanel.add(button5);
        button6 = new JButton();
        button6.setBackground(Color.BLACK);
        rightPanel.add(button6);
        button7 = new JButton();
        button7.setBackground(Color.BLACK);
        rightPanel.add(button7);
        button8 = new JButton();
        button8.setBackground(Color.BLACK);
        rightPanel.add(button8);
        button9 = new JButton();
        button9.setBackground(Color.BLACK);
        rightPanel.add(button9);
        JPanel leftPanel = new JPanel();
        leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
        JLabel nameLabel = new JLabel("Name");
        leftPanel.add(nameLabel);
        mollyButton = new JButton("Molly");
        leftPanel.add(mollyButton);
        octaviousButton = new JButton("Octavious");
        leftPanel.add(octaviousButton);
        jimmyButton = new JButton("Jimmy");
        leftPanel.add(jimmyButton);
        clearButton = new JButton("Clear");
        leftPanel.add(clearButton);
        JPanel centrePanel = new JPanel();
        centrePanel.add(new JLabel("Stuff goes here"));
        JPanel content = new JPanel(new BorderLayout());
        content.add(leftPanel, BorderLayout.WEST);
        content.add(centrePanel, BorderLayout.CENTER);
        content.add(rightPanel, BorderLayout.EAST);
        frame.setContentPane(content);
        frame.pack();
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

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

}

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

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