繁体   English   中英

Java GUI布局建议

[英]Java GUI Layout Suggestions

对于学校作业,我需要有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文档 对于你的需求看起来像BorderLayout经理应该是不错的选择。

阅读如何使用布局管理器的示例,它首先给你看看。

您的代码存在一些问题,按惯例变量名称以较低的字母开头,每行代码应该只做一件事(甚至声明变量)。 另外你不应该“扩展”一个类,除非你要扩展它的功能,如果你要做的就是使用它,那么只需创建你自己的JFrame实例。 (哦,当你在4ms上发布代码寻求帮助时,如果你觉得需要添加注释来忽略未使用的警告,那么你可能不需要在你的问题中发布它:p)

对于您的问题,您需要考虑使用多个布局(它们甚至可以相互嵌入以提供一些非常复杂的效果) - Swing布局

我已经使用了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