【问题标题】:Create a frame using FlowLayout使用 FlowLayout 创建框架
【发布时间】:2013-11-22 07:13:27
【问题描述】:

好的,所以我今天的编程练习有点问题。

练习文本是这样的:

(使用FlowLayout管理器)编写一个满足以下要求的程序:

  • 创建一个框架并将其布局设置为 FlowLayout
  • 创建两个面板并将它们添加到框架中
  • 每个面板包含三个按钮。该面板使用 FlowLayout

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

我认为将 panels 添加到 frame 时遇到了一些问题,因为当我运行程序时,它会显示 empty 框架

这是我的代码。

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

如果你们中的一些人花时间在这里帮助我,我将不胜感激。 谢谢。

【问题讨论】:

    标签: java button panel frame flowlayout


    【解决方案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】:

      你的 main 方法创建了一个框架:

      Exercise12_1 frame = new Exercise12_1();
      

      然后让它可见。

      这个'Exercise12_1'框架的构造器创建另一个框架,并将面板添加到这个另一个框架:

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

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

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

      【讨论】:

        【解决方案3】:

        您使用这样的代码 this.getContentPane().add(panel1);添加面板。

        改变

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

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

        然后它就会工作。

        【讨论】:

        • 非常感谢,我添加了您提供的代码,现在运行良好。
        猜你喜欢
        • 2010-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多