您的位置:首页 > 产品设计 > UI/UE

GUI

2015-09-25 20:07 567 查看
一.概述

GUI(图形用户界面)为程序提供图形界面。包括AWT和Swing,Swing被称为轻量级组件,,完全由java语言编写,不依赖操作系统,而AWT被称为重量级组件,依赖于本地平台

常用Swing组件 包括,JButton,JCheckBox,JComBox,JFrame,JDialog,JLable,JRadioButton,JList,JTextField,JPasswordField,JTextArea,JOptionPane。

二.组件详述

JFram:容器
<span style="font-family:SimSun;font-size:18px;">import java.awt.*;

import javax.swing.*;

public class Example1 extends JFrame {
private static final long serialVersionUID = 1L;

// 定义一个类继承JFrame类
public void CreateJFrame(String title) { // 定义一个CreateJFrame()方法
JFrame jf = new JFrame(title); // 实例化一个JFrame对象
Container container = jf.getContentPane(); // 获取一个容器
JLabel jl = new JLabel("这是一个JFrame窗体"); // 创建一个JLabel标签
// 使标签上的文字居中
jl.setHorizontalAlignment(SwingConstants.CENTER);
container.add(jl); // 将标签添加到容器中
container.setBackground(Color.white);//设置容器的背景颜色
jf.setVisible(true); // 使窗体可视
jf.setSize(200, 150); // 设置窗体大小
// 设置窗体关闭方式
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String args[]){//在主方法中调用createJFrame()方法
new Example1().CreateJFrame("创建一个JFrame窗体");
}
}
</span>


JDialog:Swing中对话框
<span style="font-family:SimSun;font-size:18px;">import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

class MyJDialog extends JDialog { // 创建新类继承JDialog类

private static final long serialVersionUID = 1L;

public MyJDialog(MyFrame frame) {
// 实例化一个JDialog类对象,指定对话框的父窗体、窗体标题和类型
super(frame, "第一个JDialog窗体", true);
Container container = getContentPane(); // 创建一个容器
container.add(new JLabel("这是一个对话框")); // 在容器中添加标签
setBounds(120, 120, 100, 100); // 设置对话框窗体大小
}
}

public class MyFrame extends JFrame { // 创建新类

private static final long serialVersionUID = 1L;

public static void main(String args[]) {
new MyFrame(); // 实例化MyJDialog类对象
}

public MyFrame() {
Container container = getContentPane(); // 创建一个容器
container.setLayout(null);
JLabel jl = new JLabel("这是一个JFrame窗体"); // 在窗体中设置标签
// 将标签的文字置于标签中间位置
jl.setHorizontalAlignment(SwingConstants.CENTER);
container.add(jl);
JButton bl = new JButton("弹出对话框"); // 定义一个按钮
bl.setBounds(10, 10, 100, 21);
bl.addActionListener(new ActionListener() { // 为按钮添加鼠标单击事件
public void actionPerformed(ActionEvent e) {
// 使MyJDialog窗体可见
new MyJDialog(MyFrame.this).setVisible(true);
}
});
container.add(bl); // 将按钮添加到容器中

container.add(bl);
container.setBackground(Color.white);
setSize(200, 200);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setVisible(true);
}
}</span>

Icon:图标
<span style="font-family:SimSun;font-size:18px;">import java.awt.*;

import javax.swing.*;

public class DrawIcon implements Icon { // 实现Icon接口
private int width; // 声明图标的宽
private int height; // 声明图标的长

public int getIconHeight() { // 实现getIconHeight()方法
return this.height;
}

public int getIconWidth() { // 实现getIconWidth()方法
return this.width;
}

public DrawIcon(int width, int height) { // 定义构造方法
this.width = width;
this.height = height;
}

// 实现paintIcon()方法
public void paintIcon(Component arg0, Graphics arg1, int x, int y) {
arg1.fillOval(x, y, width, height); // 绘制一个圆形
}

public static void main(String[] args) {
DrawIcon icon = new DrawIcon(15, 15);
// 创建一个标签,并设置标签上的文字在标签正中间
JLabel j = new JLabel("测试", icon, SwingConstants.CENTER);
JFrame jf = new JFrame(); // 创建一个JFrame窗口
Container c = jf.getContentPane();
c.add(j);
jf.setSize(100,100);
jf.setVisible(true);
jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
}
</span>

三.布局管理器

绝对布局:硬性指定组件在容器中的大小和位置,使用setLayout(null)告知编译器,使用setBounds(int x,int y,int width,int height)方法进行设置
流布局管理器:FlowLayout,最基本的布局管理器,从上到下从左到右,按照空间顺序排列,可以指定组件在每一行中的排列方式,
边界布局管理器:BorderLayout划分东南西北中5个区域,组件被添加到相应的区域中,
网格布局管理器:GridLayout将容器划分为网格,组件可以按行和列排列

四.常用面板

JPanel面板:基本面板容器
JScrollPane面板:带滚动条的面板,但是只能放置一个组件,并且不可以使用布局管理器,要实现多个组件,可以将组件放在Jpane中作为一个整体放在此面板上。
五.按钮组件

JButton:提交按钮组件
JRadioButton:单选按钮组件
JCheckBox:复选框组件
注:按钮组ButtonGroup用来容纳多个单选按钮,是否可以容纳其他按钮?

六.列表组件

JComboBox组件:下拉列表框组件。注意与window中组件不同,同时提供编辑项目中内容的功能。
JList组件:列表框组件,可以将其放入滚动面板中实现滚动选取和流浪,支持shift和ctrl多选功能
七.文本组件

JTextField:用来显示或编辑一个文本。
JPasswordField:密码框组件,提供了改变回显字符的方法(setEchoChar)。
JTextArea:文本域组件,接受用户的多行文本输入
八.常用事件监听器

事件监听器负责处理用户单击按钮后实现的功能,常用的两个事件监听器包括动作事件监听器和焦点事件监听器
在Swing事件模型中由3个完全分离的对象完成对事件的处理,分为事件源,事件以及监听程序。
事件监听器,实质上就是一个“实现特定类型监听器接口“的类对象。

ActionListener:动作事件监听器。
<span style="font-family:SimSun;font-size:18px;">import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class SimpleEvent extends JFrame{

/**
*
*/
private static final long serialVersionUID = 1L;
private JButton jb=new JButton("我是按钮,单击我");
public SimpleEvent(){
setLayout(null);
setSize(200,100);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Container cp=getContentPane();
cp.add(jb);
jb.setBounds(10, 10,100,30);
jb.addActionListener(new jbAction());
setVisible(true);
}
class jbAction implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
jb.setText("我被单击了");
}
}
public static void main(String[] args) {
new SimpleEvent();
}
}
</span>


FocusListener:焦点事件监听器。
<span style="font-family:SimSun;font-size:18px;">import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class FocusEventTest extends JFrame{

/**
* 
*/
private static final long serialVersionUID = 1L;
public FocusEventTest() {
setSize(250,100);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container cp=getContentPane();
getContentPane().setLayout(new FlowLayout());

final JLabel label = new JLabel();
getContentPane().add(label);
JTextField jt=new JTextField("请单击其他文本框",10);
JTextField jt2=new JTextField("请单击我",10);
cp.add(jt);
cp.add(jt2);
jt.addFocusListener(new FocusListener(){
//组件失去焦点时调用的方法 
public void focusLost(FocusEvent arg0) {
JOptionPane.showMessageDialog(null, "文本框失去焦点");
}
//组件获取键盘焦点时调用的方法
public void focusGained(FocusEvent arg0) {
}
});
setVisible(true);
}
public static void main(String[] args) {
new FocusEventTest();
}

}
</span>

九.一个登陆窗体

<span style="font-family:SimSun;font-size:18px;">import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class UseCase3 extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;

public UseCase3(){
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setTitle("登录窗体");
setBounds(300, 200, 300, 150);
Container cp=getContentPane();
cp.setLayout(null);
JLabel jl=new JLabel("用户名:");
jl.setBounds(10, 10, 200, 18);
final JTextField name=new JTextField();
name.setBounds(80, 10, 150, 18);
JLabel jl2=new JLabel("密码:");
jl2.setBounds(10, 50, 200, 18);
final JPasswordField password=new JPasswordField();
password.setBounds(80, 50, 150, 18);
cp.add(jl);
cp.add(name);
cp.add(jl2);
cp.add(password);
JButton jb=new JButton("确定");
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if(name.getText().trim().length()==0||new String(password.getPassword()).trim().length()==0){
JOptionPane.showMessageDialog(null, "用户名密码不允许为空");
return;
}
if(name.getText().trim().equals("mr")&&new String(password.getPassword()).trim().equals("mrsoft")){
JOptionPane.showMessageDialog(null, "登录成功");
}
else{
JOptionPane.showMessageDialog(null, "用户名或密码错误");
}
}
});
jb.setBounds(80, 80, 60, 18);
cp.add(jb);

final JButton button = new JButton();
button.setText("重置");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成方法存根
name.setText("");
password.setText("");
}
});
button.setBounds(150, 80, 60, 18);
getContentPane().add(button);
}

public static void main(String[] args) {
new UseCase3();

}

}</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: