【发布时间】: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