第一节 Swing简介
1,Swing是java的一个图形框架,组件,还有布局管理器
2,Swing主要涉及到容器,组件,还有布局管理器
3,Swing与用户交互的时候还涉及到事件概念
第二节 JFrame 容器
1,public void setVisible(boolean b)根据参数b的值显示或隐藏此窗体
2,public void setSize(int width,int height) 调整组件的大小,使其宽度为width高度为height
3,public void setLocation(int x,int y)将组件移到新位置。通过此组件父级坐标空间中的x和y参数来指定新位置的左上角
4,public Container getContentPane() 返回此窗体的contentPane 对象
public void setBackground(Color c)设置组件的背景颜色
public class JFrameTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JFrame jf=new JFrame();
/*Container cp=jf.getContentPane();
cp.setBackground(Color.BLUE);//设置背景颜色*/
jf.getContentPane().setBackground(Color.red);
jf.setLocation(300,200);//设置容器位置
jf.setSize(300, 500);//设置容器大小
jf.setVisible(true);//让容器显示
}
}
第三节 JButton组件
public class JButtonTest {
public static void main(String args[]){
JFrame jf=new JFrame("JButton测试");
JButton jb=new JButton("这是一个按钮");
jf.add(jb);
//jf.getContentPane().setBackground(Color.red);
jf.setLocation(300,200);//设置容器位置
jf.setSize(300, 500);//设置容器大小
jf.setVisible(true);//让容器显示
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
第四节 Swing布局管理器
1,FlowLayout 流式布局
使用此种布局方式会使所有组件像流水一样依次进行排列
public class FlowLayoutTest {
public static void main(String args[]){
JFrame jf=new JFrame("FlowLayout测试");
//jf.setLayout(new FlowLayout());
//jf.setLayout(new FlowLayout(FlowLayout.LEFT));
jf.setLayout(new FlowLayout(FlowLayout.LEFT,15,15));
JButton jb=null;
for(int i=1;i<9;i++){
jb=new JButton("按钮"+i);
jf.add(jb);
}
//jf.getContentPane().setBackground(Color.red);
jf.setLocation(300,200);//设置容器位置
jf.setSize(300, 500);//设置容器大小
jf.setVisible(true);//让容器显示
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
2,BorderLayout
使用此种布局方式将一个窗体的版面划分成东,西,南,北,中5个区域,可以直接将需要的组件放到这5个区域中
public class BorderLayoutTest {
public static void main(String args[]){
JFrame jf=new JFrame("BorderLayout测试");
// jf.setLayout(new BorderLayout());
jf.setLayout(new BorderLayout(5,5));
jf.add(new JButton("东"),BorderLayout.EAST);
jf.add(new JButton("西"),BorderLayout.WEST);
jf.add(new JButton("南"),BorderLayout.SOUTH);
jf.add(new JButton("北"),BorderLayout.NORTH);
jf.add(new JButton("中"),BorderLayout.CENTER);
//jf.getContentPane().setBackground(Color.red);
jf.setLocation(300,200);//设置容器位置
jf.setSize(300, 500);//设置容器大小
jf.setVisible(true);//让容器显示
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
3,GridLayout表格布局
使用此种布局方式是以表格形式进行布局管理的,在使用此布局管理器时必须设置显示的行数和列数
public class GridLayoutTest {
public static void main(String args[]){
JFrame jf=new JFrame("GridLayout测试");
jf.setLayout(new GridLayout(4,5,5,5));
JButton jb=null;
for(int i=1;i<20;i++){
jb=new JButton("按钮"+i);
jf.add(jb);
}
//jf.getContentPane().setBackground(Color.red);
jf.setLocation(300,200);//设置容器位置
jf.setSize(300, 500);//设置容器大小
jf.setVisible(true);//让容器显示
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
4,绝对布局
public class AbsoluteLayout {public static void main(String args[]){
JFrame jf=new JFrame("AbsoluteLayout测试");
jf.setLayout(null);
JButton jb1=new JButton("按钮1");
JButton jb2=new JButton("按钮1");
jb1.setBounds(20, 10, 200, 40);
jb2.setBounds(100, 50, 100, 100);
jf.add(jb1);
jf.add(jb2);
//jf.getContentPane().setBackground(Color.red);
jf.setLocation(300,200);//设置容器位置
jf.setSize(300, 500);//设置容器大小
jf.setVisible(true);//让容器显示
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
第五节 JLabel组件
public class JLabelTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JFrame jf=new JFrame("JLabel测试");
JLabel jl=new JLabel("JLabel组件",JLabel.CENTER);
jf.add(jl);
jf.setBackground(Color.yellow);
jf.setBounds(500, 200, 400,200);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
第六节 文本框组件
1,JTextField文本框
2,JPassWordField 密码框
3,JTextArea文本域
public class JTextFieldTest {
public static void main(String args[]){
JFrame jf=new JFrame("JTextField测试");
jf.setLayout(new GridLayout(1,2,20,20));
JLabel jb=new JLabel("用户名");
JTextField jtf=new JTextField();
jf.add(jb);
jf.add(jtf);
jf.setLocation(300,200);//设置容器位置
jf.setSize(300, 60);//设置容器大小
jf.setVisible(true);//让容器显示
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
》》》》》》》》》》》》
public class JPassWordFieldTest {
public static void main(String args[]){
JFrame jf=new JFrame("JPassWordField测试");
jf.setLayout(new GridLayout(2,2,20,20));
JLabel jb=new JLabel("用户名");
JTextField jtf=new JTextField();
JLabel jb2=new JLabel("密码");
JPasswordField jpw=new JPasswordField();
jf.add(jb);
jf.add(jtf);
jf.add(jb2);
jf.add(jpw);
jf.setLocation(300,200);//设置容器位置
jf.setSize(300, 120);//设置容器大小
jf.setVisible(true);//让容器显示
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
》》》》》》》》》》》》
public class JTextAreaTest1 {
public static void main(String args[]){
JFrame jf=new JFrame("JTextArea测试");
jf.setLayout(new GridLayout(1,2,20,20));
JLabel jb=new JLabel("描述:");
JTextArea jta=new JTextArea();
jf.add(jb);
jf.add(jta);
jf.setLocation(300,200);//设置容器位置
jf.setSize(300, 100);//设置容器大小
jf.setVisible(true);//让容器显示
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
第七节 JPanel轻量级容器
public class JPanelTest {public static void main(String[] args) {
JFrame jf=new JFrame("JPanel测试");
JPanel jp=new JPanel();
jp.setLayout(new GridLayout(3,2,10,10));
jp.setBorder(new EmptyBorder(10,10,10,10));//设置边距
jf.add(jp);
JLabel jb=new JLabel("用户名");
JTextField jtf=new JTextField();
JLabel jl=new JLabel("密码");
JPasswordField jpw=new JPasswordField();
JButton jb1=new JButton("sign");
JButton jb2=new JButton("exit");
jp.add(jb);
jp.add(jtf);
jp.add(jl);
jp.add(jpw);
jp.add(jb1);
jp.add(jb2);
jf.setBackground(Color.yellow);
jf.setBounds(500, 200, 400,200);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
第八节 Swing事件处理
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
*
* @author Administrator
*/
class JButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e){
System.out.println(e.getActionCommand());
JOptionPane.showMessageDialog(null, "我被点击了");
}
}
public class EventTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JFrame jf=new JFrame("Swing 事件测试");
JButton jb=new JButton("按钮");
JButtonListener jbl=new JButtonListener();
jb.addActionListener(jbl);//注册事件监听
jf.add(jb);
jf.setBackground(Color.yellow);
jf.setBounds(500, 200, 400,200);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
》》》》》》》》》》》》》》》
import java.awt.Color;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
/**
*
* @author Administrator
*/
class MyWindowListener implements WindowListener {
@Override
public void windowOpened(WindowEvent e) {
System.err.println("窗口被打开");
}
@Override
public void windowClosing(WindowEvent e) {
System.err.println("窗口关闭");
}
@Override
public void windowClosed(WindowEvent e) {
System.err.println("窗口被关闭");
}
@Override
public void windowIconified(WindowEvent e) {
System.err.println("窗口最小化");
}
@Override
public void windowDeiconified(WindowEvent e) {
System.err.println("窗口从最小化回复");
}
@Override
public void windowActivated(WindowEvent e) {
System.err.println("窗口被选中");
}
@Override
public void windowDeactivated(WindowEvent e) {
System.err.println("窗口选中被取消");
}
}
public class EventTest2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JFrame jf=new JFrame("Swing 事件测试");
MyWindowListener myWindowListener=new MyWindowListener();
jf.addWindowListener(myWindowListener);
jf.setBackground(Color.yellow);
jf.setBounds(500, 200, 400,200);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
》》》》》》
class MyWindowAdapter extends WindowAdapter{
public void windowClosing(WindowEvent e){
super.windowClosing(e);
System.out.println("窗口关闭......");
}
}
public class EventTest3 {
public static void main(String[] args) {
JFrame jf=new JFrame("Swing 事件测试");
MyWindowAdapter mw=new MyWindowAdapter();
jf.addWindowListener(mw);
jf.setBackground(Color.yellow);
jf.setBounds(500, 200, 400,200);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}