首页 > 代码库 > java用户界面—创建一个面板

java用户界面—创建一个面板

先从基础学起

创建一个面板

代码如下:

package Day08; import java.awt.FlowLayout; import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel; public class Homework1 extends JFrame {
public Homework1() {
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
p1.add(new JButton("JButton1-1"));
p1.add(new JButton("JButton1-2"));
p1.add(new JButton("JButton1-3"));
JPanel p2 = new JPanel();
p2 .setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
p2.add(new JButton("JButton2-1"));
p2.add(new JButton("JButton2-2"));
p2.add(new JButton("JButton2-3"));
setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
add(p1);
add(p2);
}

public static void main(String[] args) {
Homework1 frame = new Homework1();
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
} }

java用户界面—创建一个面板