java基础:GUI基础编程(下)
布局管理编程演示:
§流布局管理器(FlowLayout):
import javax.swing.*;
import java.awt.*;
public class FlowLayoutDemo extends JFrame{
JPanel p;
JLabel lblUser;
JTextField txtUser;
JButton btnCheck,btnExit;
public FlowLayoutDemo(){
super("流布局演示");
setSize(250,150);
p=new JPanel();
p.setLayout(new FlowLayout());
getContentPane().add(p);
lblUser=new JLabel("用户名:");
txtUser=new JTextField(16);
btnCheck=new JButton("检测用户名");
btnExit=new JButton("退出");
p.add(lblUser);
p.add(txtUser);
p.add(btnCheck);
p.add(btnExit);
setVisible(true);
}
public static void main(String[] args) {
new FlowLayoutDemo();
}
}
§边界布局管理器(BorderLayout): import javax.swing.*;
import java.awt.*;
public class BorderLayoutDemo extends JFrame{
JPanel p;
JButton b1,b2,b3,b4;
JTextArea ta;
public BorderLayoutDemo(){
super("边界布局演示");
setSize(250,150);
p=new JPanel();
p.setLayout(new BorderLayout());
getContentPane().add(p);
b1=new JButton("East");
b2=new JButton("West");
b3=new JButton("North");
b4=new JButton("South");
ta=new JTextArea("Center");
p.add(b1,"East");
p.add(b2,BorderLayout.WEST);
p.add("North",b3);
p.add("South",b4);
p.add(ta,"Center");
setVisible(true);
}
public static void main(String[] args) {
new BorderLayoutDemo();
}
} §网格布局管理器(GridLayout): import javax.swing.*;
import java.awt.*;
public class GridLayoutDemo extends JFrame{
JPanel p;
JButton b1,b2,b3,b4;
JTextArea ta;
JTextField tf;
public GridLayoutDemo(){
super("网格布局演示");
setSize(250,150);
p=new JPanel();
p.setLayout(new GridLayout(3,2));
getContentPane().add(p);
b1=new JButton("one");
b2=new JButton("two");
b3=new JButton("three");
b4=new JButton("four");
ta=new JTextArea("JTextArea");
tf=new JTextField("JTextField");
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(ta);
p.add(tf);
setVisible(true);
}
public static void main(String[] args) {
new GridLayoutDemo();
}
} §网格袋布局管理器(GridBagLayout): import javax.swing.*;
import java.awt.*;
public class GridBagLayoutDemo extends JFrame{
JPanel p;
JTextArea ta;
JTextField tf;
JButton b1,b2;
JLabel l1,l2;
GridBagConstraints gbc;
GridBagLayout gbl;
private void addComponent(Component c,GridBagLayout g
,GridBagConstraints gc,int column ,int row,int width,int height)
{
gc.gridx=column;//指定包含组件的显示区域开始边的单元格
gc.gridy=row;//指定位于组件显示区域的顶部的单元格
gc.gridwidth=width;//指定在组件显示区域的一行中的单元格数
gc.gridheight=height;//指定在组件显示区域的一列中的单元格数
g.setConstraints(c, gc);//设置此布局中指定组件的约束条件
p.add(c);
}
public GridBagLayoutDemo(){
super("网格袋布局演示");
setSize(250,150);
p=new JPanel();
gbc=new GridBagConstraints();
gbl=new GridBagLayout();
p.setLayout(gbl);
getContentPane().add(p);
ta=new JTextArea("我是文本域",5,10);
b1=new JButton("我是按钮b1.");
b2=new JButton("我是按钮b2");
tf=new JTextField("我是文本框");
补充:软件开发 , Java ,