java中自带的GUI功能一般般了,用的比较少,一般用做通讯系统,C/S等简单的图形界面系统。网页的GUI功能强大。
一、GUI体系
java中GUI分为2种:
一种是早期的awt。重量级容器,依赖于操作系统,相同的设计在不同系统上,效果不一样。
另外一种是现在的swing框架。独立的GUI容器和组件,能够实现通用性。是轻量级的框架。它是建立在awt的基础之上的。
SWT = SWING + AWT
一般的GUI操作系统都会实现MVC架构。
M model模型:用来存放数据的
V view 视图:图形界面
C control 控制:用来控制系统的
视图和控制组合在一起,就成了UI代理,和模型这2部分。视图和控制经常相互交叉影响看,放一起更好
二、GUI中的容器和组件
容器:就是可以用来放置其他组件或者容器
组件:就是独立的,具有具体实现功能的按钮,或者标签。是最小的GUI单元。不能再分割。
重量级容器,放在顶层,可以用来放置组件和其他中间容器。如常用的JFrame, JWindow,JApplet,JDialog
中间容器,不能单独存在,只能放到顶层容器中,一般是拿来做中间布局的。里面放置需要局部布局的组件和容器。如JPanel,JLabel,JScrollPane
顶层容器一般有限,单独存在,中间容器一般有很多,主要直接放到首次容器中,组件那是很多,那里需要,那里用,不过一般都在中间容器中。
三、命名习惯
基于为了便于区分程序性质和方便理解的原则。
GUI中给属性命名也有要求:
还是沿用驼峰原则(用途+类型)的方式,出第一个外每个单词首字母大写。如nameTxt,nameLab,pwdTxt
在数据库中则使用另外一种方式:
匈牙利命名 ;用下划线来分隔每个单词
四、常用的组件和容器
JFrame,常用的首次容器。默认布局为Borderlayout。这种情况下,子容器不能用null布局,会被中间的部分吃掉。导致看不到或者只能看到一条线。
JPanel, 常用来作为子面板的载体。它不能单独显示出来,只能放入首次容器中才能显示。默认使用Flowlayout布局。
JLabel,标签,文字或者图片,图片时可以作为背景使用,在把其他的组件或者容器放到标签上面。
JTextFiled,单行输入框。在null布局中必须设置列数setColumes(5),否则不能看见,只有一条缝。
JButton,按钮,可以设置2张图片,实现翻转效果。但是,图片与文字是并列的,在一层上面,不能背景布局,按钮无法实现文字自动放到图片上,需要自己PS处理下。
JTextArea,文本区域,可以有图片和文字,一般放到JScrollPane里面使用,自动实现滚动条。
JScrollPane.showView……(Component comp);它的布局不是默认,而是null时,可以显示背景,
五、4种常用布局管理器的优劣
Borderlayout:边界布局。南北贯通,东西在中,中间最大(权利大,地方面板未使用时,可以占据地方,扩大范围。一般中间放主体,上面标签菜单,下面保留。
添加子面板时,如果不设置位置,那么默认放入最中间,add(Component , Borderlayout.position)。
Cardlayout,卡片布局。分层显示,在同一个区域中显示不同的子面板,每次其中的一层(张),可以实现子页面间的切换。与Border配合。Cardlayout.show(Container, "StringName")
Flowlayout,流布局。从左到右,从上到下,默认从中间开始,大小以组件最适合的大小自动调整,输入框也必须设置列数,当窗口大小改变时,布局也会随之改变,不方便精确控制。
Gridlayout,网格布局。等分面板成行列,一般先依行放入,不够时再分割列。组件大小自动扩张至网格大小。
还有就是null,自己手动设置位置。
六、java中独特的委托事件处理模型
事件需要2个主体:一个事件发起者,一个事件的接受者。事件的接受者就是事件源。比如按钮。
java中的事件发生时,处理事件的不是按钮,而是按键所委托的监听器来处理该事件,这个是java独有的一个模型。
事件源与监听器的关系为多对多,一个事件源可以产生多种不同类型的事件。每种事件有一个监听器。
同时,一个监听器又可以同时监听多个事件源发生的某种对应于它的一种事件。
实现监听器的3种方式:
1.第三方监听器
不可避免大量的类,大量的条件判断,方法性质不单一;还有必须要传递参数
2.自己实现
无法避免非常大量的条件判断,但可以不用传参,整个类混乱,不属于自己的功能放到了身上。
3.匿名内部类
可以用匿名监听器的子类来实现每个按钮都有专用的监听器。方法根据按钮改变,方法单一,不用太多判断。也不用传参。也不依赖其他类。
java中当监听器接口的方法有2个或以上时,使用相应的监听器的Adapter适配器类,可以大量减少代码量,只需要重写需要的功能方法就行。其他的都是空方法。
七、简易输入输出弹窗
3种可以直接使用的对话框
1.消息
JOptionPane.showMessageDialog(ParentPane,“StringMessage”),当ParentPane为null时,消息框会出现在屏幕最中央。
2.输入
JOptionPane.showInpuDialog(“message”),输入为字符串,String类型的
3.选择
JOptionPane.showConfirmDialog(ParentPane,“StringMessage”),返回0,代表yes,1代表no,2代表取消cancel。
列子:计算器
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CalculatorFrame extends JFrame {
private Double lastNumber = 0D;//上次的运算值
private String optionCharatcter = "+";//上次的运算符,每次开始时默认为+
private boolean negativeFlag;//负数的标志位
private StringBuffer sb = new StringBuffer();//每次输入数字的缓冲区
private ButtonListener lis = new ButtonListener();//监听器
private Container contentP;
private JTextField outTxt;
// private JPanel downP;
private JLabel backGround;
private JButton button9;
private JButton button8;
private JButton button7;
private JButton button6;
private JButton button5;
private JButton button4;
private JButton button3;
private JButton button2;
private JButton button1;
private JButton button0;
private JButton buttonAdd;
private JButton buttonReduce;
private JButton buttonMultiply;
private JButton buttonDivide;
private JButton buttonDian;
private JButton buttonEqual;
private class ButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("\n :"+e.getActionCommand());
calculate(e.getActionCommand());
}
}
//计算器运算算法
public void calculate(String str){
String temp = sb.toString();
if(str.matches("[\\d.]")){
sb.append(str);
outTxt.setText(temp+str);
}else{
Double tempNumber = 0D;
try{
tempNumber = Double.parseDouble(temp);
}catch(NumberFormatException e){
//多次连续输入运算符时,temp为null并且本次是负号,
//并且之前负数符号位未被激活,防止多次输入负号
if(!negativeFlag && "-".equals(str)){
negativeFlag= true;
//负号位置true,函数不再继续执行,返回等待下次调用
}else{
//本次不做运算,所有设置恢复初始值
negativeFlag= false;
optionCharatcter = "+";
lastNumber = 0.0;
sb.delete(0, sb.capacity());
outTxt.setText("0.0");
JOptionPane.showMessageDialog(null,"对不起,由于你多次连续输入非法运算符,运算无法执行!");
}
return ;
}
//输入是负数时,将数字取反,并把符号位恢复为false
if(negativeFlag){
negativeFlag = false;
tempNumber = -tempNumber;
JOptionPane.showMessageDialog(null,"你输入了一个负数"+tempNumber);
}
//进行运算,上次值,上次运算符 与输入框中的值运算
System.out.print(lastNumber+optionCharatcter+tempNumber+" = ");
if("+".equals(optionCharatcter)){
lastNumber += tempNumber;
}else if("-".equals(optionCharatcter)){
lastNumber -= tempNumber;
}else if("*".equals(optionCharatcter)){
lastNumber *= tempNumber;
}else if("/".equals(optionCharatcter)){
try{
lastNumber /= tempNumber;
}catch(Exception e){
JOptionPane.showMessageDialog(null,"除法异常!分母为0");
}
}
System.out.println(lastNumber+"\n");
//运算完了显示值。
outTxt.setText(Double.toString(lastNumber));
if(!"=".equals(str)){
//运算符不是=时,保留这次运算符,输入数字缓冲区重置
sb.delete(0, sb.capacity());
optionCharatcter = str;
}else{
//运算结束了,结果输出,
JOptionPane.showMessageDialog(null,"运算结果为:"+Double.toString(lastNumber));
//上次数值置0,默认运算符为重新置+,按键缓存区Sb置空
negativeFlag= false;
optionCharatcter = "+";
lastNumber = 0.0;
sb.delete(0, sb.capacity());
outTxt.setText("0.0");
}
}
}
public CalculatorFrame(){
this.setTitle("mudeer计算器");
this.setIconImage(Toolkit.getDefaultToolkit().createImage("xiaoxin.GIF"));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setSize(230, 290);
this.setResizable(false);
this.addContent();
this.setVisible(true);
}
private void addContent() {
// TODO Auto-generated method stub
//设置页面
this.contentP = this.getContentPane();
//添加输出部分
this.outTxt = new JTextField("输出");
this.outTxt.setEditable(false);
this.outTxt.setSize(230, 30);
this.outTxt.setBackground(Color.BLUE);
this.contentP.add(this.outTxt,BorderLayout.NORTH);
//按钮部分
this.backGround = new JLabel();
this.backGround.setIcon(new ImageIcon("美女.jpg"));
this.backGround.setSize(230, 260);
// this.downP = new JPanel();
// this.downP.setSize(230, 260);
// this.downP.setLayout(null);
// this.downP.add(this.backGround);
this.contentP.add(this.backGround);
//第一排
//7
this.button7 = new JButton("7");
this.button7.setBounds(0, 0, 50, 50);
this.backGround.add(this.button7);
this.button7.addActionListener(this.lis);
//8
this.button8 = new JButton("8");
this.button8.setBounds(60, 0, 50, 50);
this.backGround.add(this.button8);
this.button8.addActionListener(this.lis);
//9
this.button9 = new JButton("9");
this.button9.setBounds(120, 0, 50, 50);
this.backGround.add(this.button9);
this.button9.addActionListener(this.lis);
// /
this.buttonDivide = new JButton("/");
this.buttonDivide.setBounds(180, 0, 50, 50);
this.backGround.add(this.buttonDivide);
this.buttonDivide.addActionListener(this.lis);
//第二排
//4
this.button4 = new JButton("4");
this.button4.setBounds(0, 60, 50, 50);
this.backGround.add(this.button4);
this.button4.addActionListener(this.lis);
//5
this.button5 = new JButton("5");
this.button5.setBounds(60, 60, 50, 50);
this.backGround.add(this.button5);
this.button5.addActionListener(this.lis);
//6
this.button6 = new JButton("6");
this.button6.setBounds(120, 60, 50, 50);
this.backGround.add(this.button6);
this.button6.addActionListener(this.lis);
//*
this.buttonMultiply = new JButton("*");
this.buttonMultiply.setBounds(180, 60, 50, 50);
this.backGround.add(this.buttonMultiply);
this.buttonMultiply.addActionListener(this.lis);
//第三排
//1
this.button1 = new JButton("1");
this.button1.setBounds(0, 120, 50, 50);
this.backGround.add(this.button1);
this.button1.addActionListener(this.lis);
//2
this.button2 = new JButton("2");
this.button2.setBounds(60, 120, 50, 50);
this.backGround.add(this.button2);
this.button2.addActionListener(this.lis);
//3
this.button3 = new JButton("3");
this.button3.setBounds(120, 120, 50, 50);
this.backGround.add(this.button3);
this.button3.addActionListener(this.lis);
//-
this.buttonReduce = new JButton("-");
this.buttonReduce.setBounds(180, 120, 50, 50);
this.backGround.add(this.buttonReduce);
this.buttonReduce.addActionListener(this.lis);
//第四排
//0
this.button0 = new JButton("0");
this.button0.setBounds(0, 180, 50, 50);
this.backGround.add(this.button0);
this.button0.addActionListener(this.lis);
//.
this.buttonDian = new JButton(".");
this.buttonDian.setBounds(60, 180, 50, 50);
this.backGround.add(this.buttonDian);
this.buttonDian.addActionListener(this.lis);
//=
this.buttonEqual = new JButton("=");
this.buttonEqual.setBounds(120, 180, 50, 50);
this.backGround.add(this.buttonEqual);
this.buttonEqual.addActionListener(this.lis);
//+
this.buttonAdd = new JButton("+");
this.buttonAdd.setBounds(180, 180, 50, 50);
this.backGround.add(this.buttonAdd);
this.buttonAdd.addActionListener(this.lis);
}
}