有 Java 编程相关的问题?

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

java使用FlowLayout创建一个框架

好吧,我今天的编程练习遇到了一些问题

练习内容如下:

(Use the FlowLayout manager) Write a program that meets the following requirements:

  • Create a frame and set its layout to FlowLayout
  • Create two panels and add them to the frame
  • Each panel contains three buttons. The panel uses FlowLayout

按钮应命名为“按钮1”、“按钮2”等

我想我在将面板添加到框架时遇到了一些问题,因为当我运行程序时,它会显示一个空的框架

这是我的密码

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

public class Exercise12_1 extends JFrame {

    public Exercise12_1() {
        setLayout(new FlowLayout());

        JFrame frame = new JFrame(" Exercise 12_1 ");
        frame.setLayout(new FlowLayout());

        // Create two panels
        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();

        panel1.setLayout(new FlowLayout());
        panel2.setLayout(new FlowLayout());

        // Add three buttons to each panel
        panel1.add(new JButton(" Button 1 "));
        panel1.add(new JButton(" Button 2 "));
        panel1.add(new JButton(" Button 3 "));
        panel2.add(new JButton(" Button 4 "));
        panel2.add(new JButton(" Button 5 "));
        panel2.add(new JButton(" Button 6 "));

        // Add panels to frame
        frame.add(panel1);
        frame.add(panel2);
    }

    public static void main(String[] args) {
        Exercise12_1 frame = new Exercise12_1();
        frame.setTitle(" Exercise 12_1 ");
        frame.setSize(600, 100);
        frame.setLocationRelativeTo(null); // center frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

如果你们中的一些人能抽出时间来帮助我,我将不胜感激。 谢谢


共 (3) 个答案

  1. # 1 楼答案

    检查一下:

    import javax.swing.*;
    import java.awt.*;
    
    public class Exercise12_1 extends JFrame {
    
        public Exercise12_1() {
            setLayout(new FlowLayout());
    
            //JFrame frame = new JFrame(" Exercise 12_1 ");
            this.setLayout(new FlowLayout());
    
            // Create two panels
            JPanel panel1 = new JPanel();
            JPanel panel2 = new JPanel();
    
            panel1.setLayout(new FlowLayout());
            panel2.setLayout(new FlowLayout());
    
            // Add three buttons to each panel
            panel1.add(new JButton(" Button 1 "));
            panel1.add(new JButton(" Button 2 "));
            panel1.add(new JButton(" Button 3 "));
            panel2.add(new JButton(" Button 4 "));
            panel2.add(new JButton(" Button 5 "));
            panel2.add(new JButton(" Button 6 "));
    
            // Add panels to frame
            this.add(panel1);
            this.add(panel2);
        }
    
        public static void main(String[] args) {
            Exercise12_1 frame = new Exercise12_1();
            frame.setTitle(" Exercise 12_1 ");
            frame.setSize(600, 100);
            frame.setLocationRelativeTo(null); // center frame
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    }
    
  2. # 2 楼答案

    你的主要方法是创建一个框架:

    Exercise12_1 frame = new Exercise12_1();
    

    然后让它可见

    这个“练习12_1”框架的构造器创建另一个框架,并将面板添加到另一个框架中:

    JFrame frame = new JFrame(" Exercise 12_1 ");
    frame.setLayout(new FlowLayout());
    

    构造函数不应该创建另一个框架。它应该将面板添加到this:正在构建的框架,然后使其可见

    此外,不应使用setSize(),而应使用pack(),以使框架根据其包含的所有组件的首选大小具有最合适的大小

  3. # 3 楼答案

    你使用这样的代码。getContentPane()。添加(第1组);添加面板

    改变

    frame.add(panel1);
    frame.add(panel2);
    

    this.getContentPane().add(panel1);
    this.getContentPane().add(panel2);
    

    到时候它就会起作用了