Java 使用 FlowLayout 创建框架

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19890196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:04:56  来源:igfitidea点击:

Create a frame using FlowLayout

javabuttonpanelframeflowlayout

提问by Khilmarsen

Okay, so I'm having some trouble with my Programming Exercise today.

好的,所以我今天的编程练习遇到了一些麻烦。

The Exercise text goes like this:

练习文本是这样的:

(Use the FlowLayoutmanager) 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

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

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

The buttons should be named "Button 1", "Button 2" and so on.

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

I think I'm having some trouble with adding the panelsto the framebecause when i run the program, it shows an emptyframe.

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

Here is the code i have.

这是我的代码。

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

I would greatly appreciate it if some of you took your time to help me out here. Thanks.

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

采纳答案by JB Nizet

Your main method creates a frame:

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

Exercise12_1 frame = new Exercise12_1();

and then makes it visible.

然后让它可见。

And the constructor of this 'Exercise12_1' frame creates anotherframe, and adds panel to this other frame:

这个“Exercise12_1”框架的构造函数创建了另一个框架,并将面板添加到另一个框架中:

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

The constructor shouldn't create another frame. It should add the panels to this: the frame being constructed, and that will then be made visible.

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

Also, you should not use setSize(), but pack(), to make the frame have the most appropriate size based on the preferred size of all the components it contains.

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

回答by MouseLearnJava

You use use such code like this.getContentPane().add(panel1); to add Panel.

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

Change

改变

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

To

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

and it will be working then.

然后它将起作用。

回答by Ramesh Kotha

Check this:

检查这个:

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