ai哟

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

GUI

  定义规则:

    大小:先水平、后垂直;先宽、后高(单位像素)

    位置:原点:x:0/y:0(原点定位于左上角)

    窗体关闭即退出:

  1. 界面元素:(1)容器:

         (2)组件:

  2.布局——嵌套布局

  3.事件处理

   

  标题栏占25, 设置窗体面板大小是减去标题栏的内容面板  this.setSize(500, 400);//设置窗体大小--像素

 SWT开发:

  Java.awt包(关于界面类):容器类与组件类:带外观(过时不在使用)

                            工具类:颜色类、字体类

  Java.swing包:容器类与组件类(带Jxxxx)

 

  界面元素:(1)容器:4种首层容器:Jwindow(最原始容器)/JFrame(原始容器升级版)/JDialog(对话框)/JApplet(Java小应用程序);

         (2)组件:标签;文本框(密码框);按钮;下拉列表;单选;复选;文本域

容器类JFrame:

public class MyFrame extends JFrame{
	
  private Container contentP;//内容面板
	
  private JLabel msgLab;//文字标签
	
  private JLabel imgLab;//图片标签
	
  private JTextField usernameTxt;//文本框
	
  private JPasswordField pwdTxt;//密码框
	
  private JButton okBtn;//按钮
	
  private JButton getMoentyBtn;//取钱按钮
	
  private JComboBox<String> teacherCmb;//下拉列表
	
  private JTextArea selfArea;//文本域
	
  private JRadioButton maleRad;//单选框
	
  private JRadioButton femaleRad;
	
  private JCheckBox hobbitBox;//复选框
		
  public MyFrame(){
	Toolkit tk = Toolkit.getDefaultToolkit();//获取工具对象
	int screenWidth = (int)tk.getScreenSize().getWidth();
	int screenHeight = (int)tk.getScreenSize().getHeight();
	this.setSize(500, 400);//设置窗体大小--像素
	this.setLocation((screenWidth-500)/2, (screenHeight-400)/2);//设置窗体的位置
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体关闭即退出程序
	this.setTitle("我的第一个GUI窗体");//标题栏设置标题
	this.setIconImage(tk.createImage("image/icon.png"));//设置标题栏图标
	this.setResizable(false);//设置窗体改变大小的能力
	this.addContent();
	this.setVisible(true);//设置该窗体可见
  }
	
  private void addContent(){
	this.contentP = this.getContentPane();//获取内容面板
	this.contentP.setBackground(Color.WHITE);//设置窗体背景色
	this.contentP.setLayout(null);//设置布局管理器为null---代表放入该容器的组件的大小位置全靠自定义
		
	//文本标签
	this.msgLab = new JLabel("用户名:");//产生对象
	this.msgLab.setText("用户名:");
//	this.msgLab.setBorder(BorderFactory.createLineBorder(Color.BLACK));//给标签设置边框--调试用
	this.msgLab.setFont(new Font("微软雅黑",Font.BOLD,16));//设置字体
	this.msgLab.setForeground(new Color(82,254,211));//设置字体颜色
	this.msgLab.setBounds(100, 20, 80, 30);//设置大小位置
	this.contentP.add(this.msgLab);//放入容器
		
	//图片标签
	this.imgLab = new JLabel(new ImageIcon("image/fish.jpg"));
	this.imgLab.setBounds(200, 20, 243, 167);
	this.contentP.add(this.imgLab);
		
	//文本框
	this.usernameTxt = new JTextField();
	this.usernameTxt.setBounds(20, 70, 100, 30);
	this.usernameTxt.setFont(new Font("微软雅黑",Font.BOLD,16));//设置字体
	this.usernameTxt.setForeground(new Color(82,254,211));//设置字体颜色
//	this.usernameTxt.setEditable(false);//设置文本框不可编辑
	this.contentP.add(this.usernameTxt);
		
	//密码框
	this.pwdTxt = new JPasswordField();
	this.pwdTxt.setEchoChar('*');
	this.pwdTxt.setFont(new Font("微软雅黑",Font.BOLD,16));//设置字体
	this.pwdTxt.setForeground(new Color(82,254,211));//设置字体颜色
	this.pwdTxt.setBounds(20, 120, 100, 30);
	this.contentP.add(this.pwdTxt);
		
	//按钮
	this.okBtn = new JButton("确定");
	this.okBtn.setText("确定");
	this.okBtn.setFont(new Font("微软雅黑",Font.BOLD,16));//设置字体
	this.okBtn.setForeground(new Color(82,254,211));//设置字体颜色
	this.okBtn.setBounds(20, 160, 100, 30);
	this.contentP.add(this.okBtn);
		
	this.getMoentyBtn = new JButton(new ImageIcon("image/buttonGet.jpg"));
	this.getMoentyBtn.setBounds(20, 200, 140, 50);
	this.contentP.add(this.getMoentyBtn);
		
	//下拉列表
	this.teacherCmb = new JComboBox<String>();
	this.teacherCmb.addItem("周春艳");
	this.teacherCmb.addItem("刘弯弯");
	this.teacherCmb.addItem("万洁");
	this.teacherCmb.addItem("张欣");
	this.teacherCmb.addItem("何茹薇");
	this.teacherCmb.setEditable(true);//设置为可编辑为true
	this.teacherCmb.setBounds(20, 260, 100, 20);
	this.contentP.add(this.teacherCmb);
		
	//文本域
	this.selfArea = new JTextArea();
	JScrollPane scrollP = new JScrollPane(this.selfArea);
	scrollP.setBounds(200, 200, 280, 160);
	this.contentP.add(scrollP);
		
	//单选框
	this.maleRad = new JRadioButton("男");
	this.femaleRad = new JRadioButton("女");
	this.maleRad.setBounds(20, 290, 50, 25);
	this.femaleRad.setBounds(80, 290, 50, 25);
	this.maleRad.setBackground(Color.WHITE);
	this.femaleRad.setBackground(Color.WHITE);
	this.maleRad.setSelected(true);//设置默认选中
	this.contentP.add(this.maleRad);
	this.contentP.add(this.femaleRad);
	ButtonGroup bGroup = new ButtonGroup();//按钮分组
	bGroup.add(this.maleRad);
	bGroup.add(this.femaleRad);
		
	//复选框
	this.hobbitBox = new JCheckBox("兴趣爱好");
	this.hobbitBox.setBounds(20, 325, 100, 25);
	this.contentP.add(this.hobbitBox);
  }
	
}

public static void main(String[] args) {   // TODO Auto-generated method stub   //容器类JFrame   MyFrame frame = new MyFrame(); }

 

布局layut

  最常用的4种布局管理器:“只要使用布局管理器,任何自定义布局无法使用”

1.边界布局(应用最多):BorderFrame

  布局方式:把整个容器划分为5个部分:东西南北中,南北要贯通,中间最大(不仅是中间的范围最大,权利也最大“周边不存在的时候,中间会占领周边”),当中间不存在的时候周边也不能占领中间;

  使用场景:不是用来直接放组件的,而是用来放置子容器(中间容器)的;

public class BorderFrame extends JFrame {
	
  private Container contentP;//内容面板
	
  private JButton btn1;
  private JButton btn2;
  private JButton btn3;
  private JButton btn4;
  private JButton btn5;
	
  public BorderFrame(){
	Toolkit tk = Toolkit.getDefaultToolkit();//获取工具对象
	int screenWidth = (int)tk.getScreenSize().getWidth();
	int screenHeight = (int)tk.getScreenSize().getHeight();
	this.setSize(500, 400);//设置窗体大小--像素
	this.setLocation((screenWidth-500)/2, (screenHeight-400)/2);//设置窗体的位置
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体关闭即退出程序
	this.setTitle("我的第一个GUI窗体");//标题栏设置标题
	this.setIconImage(tk.createImage("image/icon.png"));//设置标题栏图标
	this.setResizable(false);//设置窗体改变大小的能力
	this.addContent();
	this.setVisible(true);//设置该窗体可见
  }
	
	
  private void addContent(){
	this.contentP = this.getContentPane();//获取内容面板
	this.contentP.setBackground(Color.WHITE);//设置窗体背景色
	this.contentP.setLayout(new BorderLayout());//设置布局管理器为边界布局管理器---在默认的情况下,JFrame的内容面板就是边界布局
		
	this.btn1 = new JButton("button1");
	this.btn2 = new JButton("button2");
	this.btn3 = new JButton("button3");
	this.btn3.setFont(new Font("宋体",Font.BOLD,28));
	this.btn4 = new JButton("button4");
	this.btn5 = new JButton("button5button5");
	this.contentP.add(this.btn1);//直接add就是默认往中间放
	this.contentP.add(BorderLayout.NORTH, this.btn2);
	this.contentP.add(BorderLayout.SOUTH,this.btn3);
	this.contentP.add(BorderLayout.EAST,this.btn4);
	this.contentP.add(BorderLayout.WEST,this.btn5);
		
  }
}

public static void main(String[] args) {
  // TODO Auto-generated method stub

  BorderFrame bframe = new BorderFrame();
}

 

 

2.流布局:FlowFrame

  布局方式:按从左往右,从上往下,由中间开始的方式依次排放组件;

       组件大小要根据组件内容那个确定;

       组件的位置随着容器大小的改变而改变;

 

  使用场景:流布局是用来放置组件的,而不是用来放中间容器的;最好只做单行布局; 

public class FlowFrame extends JFrame {
  private Container contentP;//内容面板
	
  private JButton btn1;
  private JButton btn2;
  private JButton btn3;
  private JButton btn4;
  private JButton btn5;
  private JTextField inputTxt;
	
  public FlowFrame(){
	Toolkit tk = Toolkit.getDefaultToolkit();//获取工具对象
	int screenWidth = (int)tk.getScreenSize().getWidth();
	int screenHeight = (int)tk.getScreenSize().getHeight();
	this.setSize(500, 400);//设置窗体大小--像素
	this.setLocation((screenWidth-500)/2, (screenHeight-400)/2);//设置窗体的位置
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体关闭即退出程序
	this.setTitle("我的第一个GUI窗体");//标题栏设置标题
	this.setIconImage(tk.createImage("image/icon.png"));//设置标题栏图标
	this.setResizable(true);//设置窗体改变大小的能力
	this.addContent();
	this.setVisible(true);//设置该窗体可见
  }
  private void addContent(){ this.contentP = this.getContentPane();//获取内容面板 this.contentP.setBackground(Color.WHITE);//设置窗体背景色 this.contentP.setLayout(new FlowLayout());//设置布局管理器为流布局管理器 this.btn1 = new JButton("button1"); this.btn2 = new JButton("button2"); this.btn3 = new JButton("button3"); this.btn3.setFont(new Font("宋体",Font.BOLD,28)); this.btn4 = new JButton("button4"); this.btn5 = new JButton("button5button5"); this.inputTxt = new JTextField(); this.inputTxt.setColumns(10); this.add(this.btn1); this.add(this.btn2); this.add(this.btn3); this.add(this.btn4); this.add(this.btn5); this.add(this.inputTxt);   } }
public static void main(String[] args) {
  // TODO Auto-generated method stub
  FlowFrame fFrame = new FlowFrame();
}

 

 

3.网格布局:GridFrame:

  布局方式:按行列将整个容器划分为等大的区域,放入的子容器的数目如果与设置的数目不等,优先保证行改变列

  使用场景:不是用来放置组件的,而是用来放置容器(中间容器)的。

 

       使用场景:不是用来放置组件的,而是用来放置容器(中间容器)的。

public class GridFrame extends JFrame {
	private Container contentP;//内容面板
	
	private JButton btn1;
	private JButton btn2;
	private JButton btn3;
	private JButton btn4;
	private JButton btn5;
	
  public GridFrame(){ Toolkit tk = Toolkit.getDefaultToolkit();//获取工具对象 int screenWidth = (int)tk.getScreenSize().getWidth(); int screenHeight = (int)tk.getScreenSize().getHeight(); this.setSize(500, 400);//设置窗体大小--像素 this.setLocation((screenWidth-500)/2, (screenHeight-400)/2);//设置窗体的位置 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体关闭即退出程序 this.setTitle("我的第一个GUI窗体");//标题栏设置标题 this.setIconImage(tk.createImage("image/icon.png"));//设置标题栏图标 this.setResizable(false);//设置窗体改变大小的能力 this.addContent(); this.setVisible(true);//设置该窗体可见   }   private void addContent(){ this.contentP = this.getContentPane();//获取内容面板 this.contentP.setBackground(Color.WHITE);//设置窗体背景色 this.contentP.setLayout(new GridLayout(2,3));//设置布局管理器为网格布局管理器 this.btn1 = new JButton("button1"); this.btn2 = new JButton("button2"); this.btn3 = new JButton("button3"); this.btn3.setFont(new Font("宋体",Font.BOLD,28)); this.btn4 = new JButton("button4"); this.btn5 = new JButton("button5"); this.contentP.add(this.btn1); this.contentP.add(this.btn2); this.contentP.add(this.btn3); this.contentP.add(this.btn4); this.contentP.add(this.btn5);   } }

public static void main(String[] args) {
  // TODO Auto-generated method stub
  GridFrame gFrame = new GridFrame();
}

 

面板类JPand:面板嵌套


1.定义内容面板:设置分割框体:

public class GameFrame extends JFrame {
	
  private Container contentP;
	
  private UpPanel upP;
	
  private MidPanel midP;   private DownPanel dnP;   public GameFrame(){ Toolkit tk = Toolkit.getDefaultToolkit();//获取工具对象 int screenWidth = (int)tk.getScreenSize().getWidth(); int screenHeight = (int)tk.getScreenSize().getHeight(); this.setSize(300, 300);//设置窗体大小--像素 this.setLocation((screenWidth-300)/2, (screenHeight-300)/2);//设置窗体的位置 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体关闭即退出程序 this.setTitle("猜数字游戏");//标题栏设置标题 this.setIconImage(tk.createImage("image/icon.png"));//设置标题栏图标 this.setResizable(false);//设置窗体改变大小的能力 this.addContent(); this.setVisible(true);//设置该窗体可见   }   private void addContent() { // TODO Auto-generated method stub this.contentP = this.getContentPane(); this.contentP.setLayout(new GridLayout(3,1));//用网格布局划分框体 this.upP = new UpPanel(); this.midP = new MidPanel(); this.dnP = new DownPanel(this); this.contentP.add(this.upP); this.contentP.add(this.midP); this.contentP.add(this.dnP); }   public Container getContentP() { return contentP;   }   public void setContentP(Container contentP) { this.contentP = contentP;   }   public UpPanel getUpP() { return upP;   }   public void setUpP(UpPanel upP) { this.upP = upP;   }   public MidPanel getMidP() { return midP;   }   public void setMidP(MidPanel midP) { this.midP = midP;   }   public DownPanel getDnP() { return dnP;   }   public void setDnP(DownPanel dnP) { this.dnP = dnP;   } }

2.先设置布局上框架:

public class UpPanel extends JPanel {
	
  private JTextField inputTxt;//设置文本框
	
  public UpPanel() {
	// TODO Auto-generated constructor stub
	this.setBackground(Color.LIGHT_GRAY);
	this.setLayout(new FlowLayout());//JPanel默认的布局管理器就是流布局
	this.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Your Guess"));
     //(Color.BLACK):边框颜色, "Your Guess":设置边框文字
this.inputTxt = new JTextField(); this.inputTxt.setColumns(10); this.add(this.inputTxt);   }   public JTextField getInputTxt() { return inputTxt;   }   public void setInputTxt(JTextField inputTxt) { this.inputTxt = inputTxt;   } }

3.设置中间框架:

public class MidPanel extends JPanel {
	
  private JLabel msgLab;
	
  public MidPanel() {
	// TODO Auto-generated constructor stub
	this.setBackground(Color.LIGHT_GRAY);
	this.setLayout(null);
	this.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK), "Hint"));
		
	this.msgLab = new JLabel("Let's Play HiLo");
	this.msgLab.setBounds(100, 30, 100, 30);
	this.add(this.msgLab);
  }

  public JLabel getMsgLab() {
	return msgLab;
  }

  public void setMsgLab(JLabel msgLab) {
	this.msgLab = msgLab;
  }

4.设置下部框体:

4-1.先设置边界布局:并设置按钮

public class DownPanel extends JPanel {
	
  private ButtonPanel btnP;//设置按钮
	
  public DownPanel(GameFrame gm) {
	// TODO Auto-generated constructor stub
	this.setBackground(Color.LIGHT_GRAY);
	this.setLayout(new BorderLayout());
		
	this.btnP = new ButtonPanel(gm);
	this.add(BorderLayout.SOUTH,this.btnP);
  }

  public ButtonPanel getBtnP() {
	return btnP;
  }

  public void setBtnP(ButtonPanel btnP) {
	this.btnP = btnP;
  }
}

4-2.按钮设置

public class ButtonPanel extends JPanel {
	
  private JButton enterBtn;
	
  private JButton cancelBtn;
		
	this.setBackground(Color.LIGHT_GRAY);
	this.enterBtn = new JButton("确定");
	this.cancelBtn = new JButton("取消");
		
	this.add(this.enterBtn);
	this.add(this.cancelBtn);
		
	this.enterBtn.addActionListener(new ActionListener(){

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			//首先获取文本框中的输入
			//做输入有效性校验---正则
			//校验通过,转换为整数,再和随机数比较
			//然后根据比较结果,修改标签的显示信息
				
				
		}
			
	});
  }

  public JButton getEnterBtn() { return enterBtn;   }   public void setEnterBtn(JButton enterBtn) { this.enterBtn = enterBtn;   }   public JButton getCancelBtn() { return cancelBtn;   }   public void setCancelBtn(JButton cancelBtn) { this.cancelBtn = cancelBtn;   } }
public static void main(String[] args) {
	// TODO Auto-generated method stub
	GameFrame game = new GameFrame();
}

 

事件处理:

  事件:按钮——事件源对象

  委托事件模型:(1)事件源:——绑定关系——2)监听器对象:     

    一个监听器可以绑定多个事件源;一个事件源可以绑定多个监听器;监听器有各自的监听事件类型;

 

  步骤:1.写好界面,事件源对象已经有了;

     2.选择合适的监听器类型;

     3.书写监听器的实现类(写好处理代码);

     4.产生监听器对象和事件源对象进行绑定;

 

    Event + Listrener:事件源监听器接口:Action(动作)

  监听器实现方式一:

    单独书写一个类,实现监听器接口。

    特点:1.可以让一个监听器类监听多个事件源,用if判断进行分割;但这么做违背了单一职责、因此从设计上每一个事件源书写单独的监听器类。

       2.如果需要操作费时间远的其它组件,只能传参。

  监听器实现方式二:

    让容器类充当监听器。

    特点:1.只能让一个监听器类监听多个事件源,用if判断进行分割;铁定违背单一职责。

       2.操作本容器的组件或中间容器,不用传参;

  监听器实现方式三:

    匿名内部类——在跟事件源对象绑定监听器对象的同时,实现监听器类。

    特点:1.每个事件源对象拥有独立的监听器类,同时没有多的java文件。即满足单一,又没有增加文件量。

       2.操作外部容器类的任何组件或子容器不用传参。

public class ColorFrame extends JFrame{	
	private Container contentP;
	
	private JButton redBtn;
	
	private JButton greenBtn;
	
	public ColorFrame(){
		Toolkit tk = Toolkit.getDefaultToolkit();//获取工具对象
		int screenWidth = (int)tk.getScreenSize().getWidth();
		int screenHeight = (int)tk.getScreenSize().getHeight();
		this.setSize(300, 300);//设置窗体大小--像素
		this.setLocation((screenWidth-300)/2, (screenHeight-300)/2);//设置窗体的位置
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体关闭即退出程序
		this.setTitle("我的第一个GUI窗体");//标题栏设置标题
		this.setIconImage(tk.createImage("image/icon.png"));//设置标题栏图标
		this.setResizable(false);//设置窗体改变大小的能力
		this.addContent();
		this.setVisible(true);//设置该窗体可见
	}

	private void addContent() {
		// TODO Auto-generated method stub
		this.contentP = this.getContentPane();
		this.contentP.setBackground(Color.WHITE);
		this.contentP.setLayout(new FlowLayout());
		
		//事件源对象
		this.redBtn = new JButton("红色");
		this.redBtn.setActionCommand("red");
		this.greenBtn = new JButton("绿色");
		this.greenBtn.setActionCommand("green");
				
		this.redBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				ColorFrame.this.contentP.setBackground(Color.RED);
				ColorFrame.this.redBtn.setEnabled(false);
				ColorFrame.this.greenBtn.setEnabled(true);
			}
		});
		
		this.greenBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				ColorFrame.this.contentP.setBackground(Color.GREEN);
				ColorFrame.this.redBtn.setEnabled(true);
				ColorFrame.this.greenBtn.setEnabled(false);
			}
		});
		
		
		this.contentP.add(this.redBtn);
		this.contentP.add(this.greenBtn);
	}	
}

读取文件:

public class LoginFrame extends JFrame {
	
	private Container contentP;
	
	private JLabel nameLab;
	
	private JLabel pwdLab;
	
	private JTextField nameTxt;
	
	private JPasswordField pwdTxt;
	
	private JButton loginBtn;
	
	private Properties props;
	
	public LoginFrame() {
		// TODO Auto-generated constructor stub
		Toolkit tk = Toolkit.getDefaultToolkit();//获取工具对象
		int screenWidth = (int)tk.getScreenSize().getWidth();
		int screenHeight = (int)tk.getScreenSize().getHeight();
		this.setSize(300, 300);//设置窗体大小--像素
		this.setLocation((screenWidth-300)/2, (screenHeight-300)/2);//设置窗体的位置
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体关闭即退出程序
		this.setTitle("用户登录");//标题栏设置标题
		this.setIconImage(tk.createImage("image/icon.png"));//设置标题栏图标
		this.setResizable(false);//设置窗体改变大小的能力
		this.addContent();
		this.setVisible(true);//设置该窗体可见
		
		this.props = new Properties();
		try {
			props.load(new FileInputStream("user.properties"));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	private void addContent() {
		// TODO Auto-generated method stub
		this.contentP = this.getContentPane();
		this.contentP.setBackground(Color.WHITE);
		this.contentP.setLayout(null);
		
		this.nameLab = new JLabel("用户名:");
		this.nameLab.setBounds(20, 20, 80, 30);
		this.contentP.add(this.nameLab);
		
		this.pwdLab = new JLabel("密     码:");
		this.pwdLab.setBounds(20, 70, 80, 30);
		this.contentP.add(this.pwdLab);
		
		this.nameTxt = new JTextField();
		this.nameTxt.setBounds(110, 25, 100, 20);
		this.contentP.add(this.nameTxt);
		
		this.pwdTxt = new JPasswordField();
		this.pwdTxt.setBounds(110, 75, 100, 20);
		this.contentP.add(this.pwdTxt);
		
		this.loginBtn = new JButton("登录");
		this.loginBtn.setBounds(160, 110, 80, 25);
		this.contentP.add(this.loginBtn);
		
		this.loginBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String inputName = LoginFrame.this.nameTxt.getText();
				String password = new String(LoginFrame.this.pwdTxt.getPassword());
				
				if(inputName.equals(props.getProperty("username")) && password.equals(props.getProperty("password"))){
					JOptionPane.showMessageDialog(LoginFrame.this, "登录成功!");
				}else{
					JOptionPane.showMessageDialog(LoginFrame.this, "登录失败!");
				}
				
				LoginFrame.this.nameTxt.setText("");
				LoginFrame.this.pwdTxt.setText("");
				LoginFrame.this.nameTxt.requestFocus();
				
			}
		});
	}

 

4.卡片布局(最实用“事件处理用”):布局管理器最后一种,也是最常用种;

  整个翻页: 内容面板 设置为 卡片布局

 

  布局翻页: JPael 设置为 卡片布局

  4-1.设置内容面板:

public class CardFrame extends JFrame{
	
	private Container contentP;
	
	private SeasonPanel seaP;//设置卡片布局管理器
	
	private ButtonPanel btnP;//设置布局器管理器的步骤
	
	public CardFrame(){
		Toolkit tk = Toolkit.getDefaultToolkit();//获取工具对象
		int screenWidth = (int)tk.getScreenSize().getWidth();
		int screenHeight = (int)tk.getScreenSize().getHeight();
		this.setSize(300, 300);//设置窗体大小--像素
		this.setLocation((screenWidth-300)/2, (screenHeight-300)/2);//设置窗体的位置
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体关闭即退出程序
		this.setTitle("卡片布局管理");//标题栏设置标题
		this.setIconImage(tk.createImage("image/icon.png"));//设置标题栏图标
		this.setResizable(false);//设置窗体改变大小的能力
		this.addContent();
		this.setVisible(true);//设置该窗体可见
	}

	private void addContent() {
		// TODO Auto-generated method stub
		this.contentP = this.getContentPane();
		this.contentP.setLayout(new BorderLayout());
		
		this.seaP = new SeasonPanel();
		this.btnP = new ButtonPanel(this);
		
		this.contentP.add(BorderLayout.CENTER,this.seaP);
		this.contentP.add(BorderLayout.SOUTH,this.btnP);
	}

	public SeasonPanel getSeaP() {
		return seaP;
	}

	public void setSeaP(SeasonPanel seaP) {
		this.seaP = seaP;
	}

	public ButtonPanel getBtnP() {
		return btnP;
	}

	public void setBtnP(ButtonPanel btnP) {
		this.btnP = btnP;
	}
}

  4-2.设置卡片管理布局器

public class SeasonPanel extends JPanel {
	
	private SpringPanel sprP;//设置颜色或添加相片
	
	private SummerPanel sumP;//设置颜色或添加相片
private AutumnPanel autP;//设置颜色或添加相片
private WinterPanel winP;//设置颜色或添加相片
public SeasonPanel(){ this.setBackground(Color.BLACK); this.setLayout(new CardLayout());//设置卡片布局管理器 this.sprP = new SpringPanel(); this.sumP = new SummerPanel(); this.autP = new AutumnPanel(); this.winP = new WinterPanel(); //第一个被放入的卡片在最上面 //每张卡片放入的时候要给出别名 this.add("spring", this.sprP); this.add("summer", this.sumP); this.add("autumn", this.autP); this.add("winter", this.winP); } public SpringPanel getSprP() { return sprP; } public void setSprP(SpringPanel sprP) { this.sprP = sprP; } public SummerPanel getSumP() { return sumP; } public void setSumP(SummerPanel sumP) { this.sumP = sumP; } public AutumnPanel getAutP() { return autP; } public void setAutP(AutumnPanel autP) { this.autP = autP; } public WinterPanel getWinP() { return winP; } public void setWinP(WinterPanel winP) { this.winP = winP; } }

  4-2-1.设置颜色或添加图片

public class SpringPanel extends JPanel {
	
	public SpringPanel(){
		this.setBackground(Color.GREEN);
	}
	
}

public class SummerPanel extends JPanel{
	
	public SummerPanel(){
		this.setBackground(Color.RED);
	}
	
}

public class AutumnPanel extends JPanel {
	
	public AutumnPanel(){
		this.setBackground(Color.ORANGE);
	}
	
}

public class WinterPanel extends JPanel {
	
	public WinterPanel(){
		this.setBackground(Color.LIGHT_GRAY);
	}
	
}

  4-3.设置卡片管理布局器步骤

public class ButtonPanel extends JPanel {
	
	private JButton firstBtn;
	
	private JButton lastBtn;
	
	private JButton nextBtn;
	
	private JButton preBtn;
	
	private JButton sumBtn;
	
	private CardFrame cardF;
	
	public ButtonPanel(CardFrame cardF) {
		// TODO Auto-generated constructor stub
		this.cardF = cardF;
		
		this.setBackground(Color.WHITE);
		this.setLayout(new FlowLayout());
		
		this.firstBtn = new JButton("|<");
		this.firstBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				//翻页步骤:1、首先得到被设置为卡片布局的那个容器
				SeasonPanel sp = ButtonPanel.this.cardF.getSeaP();
				//2、得到这个容器的布局管理器
				CardLayout card = (CardLayout)sp.getLayout();
				//3、调用该布局管理器翻页
				card.first(sp);
			}
		});
			
		this.preBtn = new JButton("<<");
		this.preBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				//翻页步骤:1、首先得到被设置为卡片布局的那个容器
				SeasonPanel sp = ButtonPanel.this.cardF.getSeaP();
				//2、得到这个容器的布局管理器
				CardLayout card = (CardLayout)sp.getLayout();
				//3、调用该布局管理器翻页
				card.previous(sp);
			}
		});
				
		this.nextBtn = new JButton(">>");
		this.nextBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				//翻页步骤:1、首先得到被设置为卡片布局的那个容器
				SeasonPanel sp = ButtonPanel.this.cardF.getSeaP();
				//2、得到这个容器的布局管理器
				CardLayout card = (CardLayout)sp.getLayout();
				//3、调用该布局管理器翻页
				card.next(sp);
			}
		});
			
		this.lastBtn = new JButton(">|");
		this.lastBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				//翻页步骤:1、首先得到被设置为卡片布局的那个容器
				SeasonPanel sp = ButtonPanel.this.cardF.getSeaP();
				//2、得到这个容器的布局管理器
				CardLayout card = (CardLayout)sp.getLayout();
				//3、调用该布局管理器翻页
				card.last(sp);
			}
		});
		
		this.sumBtn = new JButton("夏天");
		this.sumBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				//翻页步骤:1、首先得到被设置为卡片布局的那个容器
				SeasonPanel sp = ButtonPanel.this.cardF.getSeaP();
				//2、得到这个容器的布局管理器
				CardLayout card = (CardLayout)sp.getLayout();
				//3、调用该布局管理器翻页
				card.show(sp, "summer");
			}
		});
		
		this.add(this.firstBtn);
		this.add(this.preBtn);
		this.add(this.nextBtn);
		this.add(this.lastBtn);
		this.add(this.sumBtn);
	}
}

 设置背景:

public class CardFrame2 extends JFrame {
	
	private Container contentP;
	
	private JLabel backLab;
	
	private JButton okBtn;
	
	public CardFrame2(){
		Toolkit tk = Toolkit.getDefaultToolkit();//获取工具对象
		int screenWidth = (int)tk.getScreenSize().getWidth();
		int screenHeight = (int)tk.getScreenSize().getHeight();
		this.setSize(600, 428);//设置窗体大小--像素;大小要与背景图片一样并且加上标题框高度+28
		this.setLocation((screenWidth-600)/2, (screenHeight-428)/2);//设置窗体的位置
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体关闭即退出程序
		this.setTitle("背景图片问题");//标题栏设置标题
		this.setIconImage(tk.createImage("image/icon.png"));//设置标题栏图标
		this.setResizable(false);//设置窗体改变大小的能力
		this.addContent();
		this.setVisible(true);//设置该窗体可见
	}

	private void addContent() {
		// TODO Auto-generated method stub
		this.contentP = this.getContentPane();
		this.contentP.setLayout(new BorderLayout());		
		this.backLab = new JLabel(new ImageIcon("background.jpg"));
		this.contentP.add(this.backLab);
		
		this.backLab.setLayout(null);
		
		this.okBtn = new JButton("确定");
		this.okBtn.setBounds(200,50, 80, 30);
		this.backLab.add(this.okBtn);
		
	}
}

  

posted on 2016-12-16 21:07  ai哟  阅读(289)  评论(0编辑  收藏  举报