图形用户界面设计

1.GUI(Graphics User Interface,图形用户接口)编程基础
设计和实现图形用户时,主要包括两项内容
(1)创建图形界面中需要的元素,进行相应的布局。
(2)定义界面元素对用户交互事件的响应以及对事件的处理。
Java中构成图形用户界面的各种元素和成分可粗略的分为三类:容器、控制组件和用户自定义成分。

2.常用容器类
2.1顶层容器类

顶层JFrame的应用:
功能实现:创建一个空白的窗体框架,标题为“JFrame窗口演示”。


import javax.swing.JFrame;
@SuppressWarnings("serial")
public class Window extends JFrame{
	public Window(){
	    this.setTitle("JFrame窗口演示");
	    this.setVisible(true);//或者this.show(),使窗口显现出来
	    this.setSize(600,600);//设置窗口大小
	    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public static void main(String[] args){
		new Window();
	}

}

定义完窗口框架后,在加入控制组件之前首先要得窗口的内容窗格。顶层容器中除菜单之外的组件都放在这个内容格窗中。

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JFrame;
@SuppressWarnings("serial")
public class Window extends JFrame{
	public Window(){
	    this.setTitle("JFrame窗口演示");
	    Container container=this.getContentPane();//获取内容窗格
	    container.setLayout(new BorderLayout());//设置内容窗格的布局
	    this.setVisible(true);
	    this.setSize(600,450);
	    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public static void main(String[] args){
		new Window();
	}

}

2.2 中间容器类
中间容器类不能独立存在,必须被添加到其他容器内部。常用的中间容器类有JPanel类、JScrollPane类等。

2.2.1 JPanel类
面板(JPanel)与顶层容器不同的是,面板不能独立存在,必须被添加到其他容器内部。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Window extends JFrame{
	public Window(){
	    this.setTitle("JFrame窗口演示");
	    Container container=this.getContentPane();//获取内容窗格
	    container.setLayout(new BorderLayout());//设置内容窗格的布局
	    JPanel panel=new JPanel();//创建一个面板对象
	    panel.setBackground(Color.black);//设置背景颜色
	    JButton bt=new JButton("登录");//创建命令按钮对象
	    panel.add(bt);//将按钮添加到面板容器对象里
	    container.add(panel,BorderLayout.SOUTH);//添加面板到内容窗格的下方
	    this.setSize(600,450);
	    this.setVisible(true);
	    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public static void main(String[] args){
		new Window();
	}

}

在这里插入图片描述

2.2.2 JScrollPane类
JScrollPane称为滚动窗口,与JPanel创建的容器不同的是,JScrollPane带有滚动条并且只能向滚动窗口中添加一个组件,所以经常将一些组件添加到一个面板容器中,然后再把这个面板添加到滚动窗口中。
JScrollPane的应用:
功能实现:在窗口上放置5个命令按钮,其中前四个放置到JScrollPane容器中,放到窗格的中间区域,当窗口的大小变化时,可以通过单击滚动滑轮条浏览被隐藏的组件。

import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class scrollPane extends JFrame{
	JPanel p;
	JScrollPane scrollpane;
	private Container container;
	public scrollPane() {
		// TODO Auto-generated constructor stub
		this.setTitle("滚动窗口演示");
		container=this.getContentPane();
		container.setLayout(new BorderLayout());
		container.setBackground(Color.gray);
		scrollpane=new JScrollPane();
		scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);//设置水平滚动条的显示策略为一直显示
		scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);//设置垂直滚动条的显示策略为一直显示
		p=new JPanel();
		p.add(new JButton("按钮1"));
		p.add(new JButton("按钮2"));
		p.add(new JButton("按钮3"));
		p.add(new JButton("按钮4"));
		scrollpane.setViewportView(p);//设置视图,将带有组件的面板JPanel放置到JScrollPane容器中
		container.add(scrollpane);//将面板容器添加到内容窗格中
		JButton jb=new JButton("第五个按钮");
		container.add(jb,BorderLayout.SOUTH);
		this.setVisible(true);
		this.setSize(400, 400);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

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

}

在这里插入图片描述

3.布局管理器
采用一定的策略来管理容器中各个控件的大小、位置等属性。
Java.awt定义了五种布局管理器,这里介绍一种:FlowLayout的布局策略是将遵循这种布局策略的容器中的组件按照加入的先后顺序从左向右排列,每当排满一行之后就转到下一行继续从左至右排列。

FlowLayout布局管理器的应用—
功能实现:创建窗体框架,并以FlowLayout的布局放置四个命令按钮。

import javax.swing.*;
import java.awt.*;

public class FlowlayoutDemo extends JFrame{
	public FlowlayoutDemo(){
		this.setTitle("布局管理器演示");
		Container container=this.getContentPane();//获得窗格
		container.setLayout(new FlowLayout(FlowLayout.LEFT));//左对齐
		JButton bt1=new JButton("A");
		JButton bt2=new JButton("B");
		JButton bt3=new JButton("C");
		JButton bt4=new JButton("D");
		//将组件添加到内容窗格中,组件的大小和位置由FlowLayout布局管理器指定
		container.add(bt1);
		container.add(bt2);
		container.add(bt3);
		container.add(bt4);
		this.setVisible(true);
		this.setSize(600, 450);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

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

}

在这里插入图片描述
4.Java的GUI事件处理
如ActionEvent事件。
4.1功能实现:单击命令按钮时关闭窗口,结束程序的运行。
在2.2.1的基础上加上事件监听器。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

@SuppressWarnings("serial")
public class TestEvent extends JFrame{

	public TestEvent(){
		this.setTitle("事件处理演示");
		Container container=new Container();
		container=this.getContentPane();
		container.setLayout(new BorderLayout());
		JPanel jp=new JPanel();
		jp.setBackground(Color.darkGray);
		JButton bt=new JButton("退出");
		bt.addActionListener(new ButtonEvent());
		jp.add(bt);
		container.add(jp,BorderLayout.SOUTH);
		this.setVisible(true);
		this.setSize(350, 450);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	//该类为内部类,作为事件监听程序类,该类必须为实现事件对应的接口
	 class ButtonEvent  implements ActionListener{
		public void actionPerformed(ActionEvent e){
			System.exit(0);
			
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new TestEvent();
	}

}

4.2 使用匿名内部类作为事件监听程序,大部分事件监听器只是临时使用一次,所以使用匿名内部类形式的事件监听器更合适。
实现功能同4.1。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

@SuppressWarnings("serial")
public class TestEventPlus extends JFrame{
	public TestEventPlus(){
		this.setTitle("匿名内部类监听演示");
		Container container=new Container();
		container=this.getContentPane();
		container.setLayout(new FlowLayout());
		JButton jb=new JButton("退出");
		
		/*匿名内部作为事件监听程序类,该类必须实现事件对应的ActionListener接口,
		当触发ActionEvent事件时,执行该方法中的代码*/
		jb.addActionListener(new ActionListener(){
		  public void actionPerformed(ActionEvent e){
			System.exit(0);
		  }
	});
		
		container.add(jb);
		this.setVisible(true);
		this.setSize(300, 400);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new TestEventPlus();
	}

}

4.3窗口事件的处理
大部分GUI应用程序都需要使用窗体作为最外层的容器,可以说窗体是组建GUI应用程序的基础,应用中需要使用的其他控件都是直接或间接放在窗体中的。与面板的区别在于面板需要加入组件才能使用。
窗口事件的应用:
功能实现;创建两个窗口,对窗口事件进行测试,根据对窗口的操作不同在屏幕上显示对应的提示信息。

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;

public class windowEventDemo {
	JFrame f1,f2;
	public windowEventDemo() 
	{
		// TODO Auto-generated constructor stub
		 f1=new JFrame("这是第一个测试窗口");
		 f2=new JFrame("这是第二个测试窗口");
		Container container=new Container();
		container=f1.getContentPane();
		f1.setSize(400,650);
		f2.setSize(300,500);
		container.setBackground(Color.black);//第一个窗口设置背景颜色,便于区分
		f1.setVisible(true);
		f2.setVisible(true);
		f1.addWindowListener(new WinLis());
		f1.addWindowListener(new WinLis());
	}
	class WinLis implements WindowListener{
		public void windowActivated(WindowEvent e){//窗口被设置为活动窗口
		}
		public void windowClosed(WindowEvent arg0) {//窗口关闭时调用
			// TODO Auto-generated method stub
			
		}
		@Override
		public void windowClosing(WindowEvent arg0) {//窗口关闭
			// TODO Auto-generated method stub
			System.exit(0);
		}
		@Override
		public void windowDeactivated(WindowEvent arg0) {//窗口设置为非活动窗口
			// TODO Auto-generated method stub
			if(arg0.getSource()==f1)
				System.out.println("第一个窗口失去焦点");
			else
				System.out.println("第二个窗口失去焦点");
		}
		@Override
		public void windowDeiconified(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		@Override
		public void windowIconified(WindowEvent arg0) {//窗口图标化时调用
			// TODO Auto-generated method stub
			if(arg0.getSource()==f1)
				System.out.println("第一个窗口被最小化");
			else
				System.out.println("第二个窗口被最小化");

		}
		@Override
		public void windowOpened(WindowEvent arg0) { //窗口打开时调用
			// TODO Auto-generated method stub
			System.out.println("窗口被打开");
		}
		}


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

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值