有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

swing Java GUI布局建议

对于学校作业,我需要有两个面板

右边需要有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();
    }

}

共 (3) 个答案

  1. # 1 楼答案

    您的代码有一些问题,按照惯例,变量名以小写字母开头,并且每行代码只应执行一项操作(甚至声明变量)。此外,除非要扩展类的功能,否则不应该“扩展”类,如果要做的只是使用它,那么只需创建自己的JFrame实例。(哦,当你在fourms上发布代码寻求帮助时,如果你觉得有必要添加注释以忽略未使用的警告,那么你可能也不需要在问题中发布它:p)

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

    我使用了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();
        }
    
    }
    
  2. # 2 楼答案

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

    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
    

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