学习目标
图形界面的布局管理
(1):对话框
(2):面板
(3):布局
(4):单/多选按钮
(5):列表
学习内容
(1):对话框
package Demo01;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static javax.swing.WindowConstants.EXIT_ON_CLOSE;
public class DialogDemo01 extends JDialog {
public DialogDemo01(JFrame jframe) {
super(jframe,false);
this.setBounds(100,100,500,500);
Container container = this.getContentPane();
container.setLayout(null);
// container.add(new Label("大数据五班"));
JButton button = new JButton("我是五班");
button.setBounds(30,30,200,50);
container.add(button);
}
public static void main(String[] args) {
JFrame j = new JFrame();
j.setVisible(true);
j.setBounds(100,100,700,500);
j.setDefaultCloseOperation(EXIT_ON_CLOSE);
Container container = j.getContentPane();
container.setLayout(null);
JButton button = new JButton("我要登录");
button.setBounds(30,30,200,50);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
DialogDemo01 s = new DialogDemo01(j);
s.setVisible(true);
System.out.println("我被点击了");
}
});
container.add(button);
}
}
结果为:
(2):面板
JPanel
面板组件,非顶层容器。
一个界面只可以有一个JFrame窗体组件,但是可以有多个JPanel面板组件,而JPanel上也可以使用FlowLayout,BorderLayout,GridLayout等各种布局管理器,这样可以组合使用,达到较为复杂的布局效果。
例如:
package Demo02;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
//面板作为容器不能单独存在,必须放到其他容器里面JFrame,JDialog
public class JPaneDemo extends JFrame {
public JPaneDemo() {
Container container = this .getContentPane();
container.setLayout((new GridLayout( 2,1,10,10)));
JPanel panel = new JPanel(new GridLayout(