Java-GUI编程
GUI全称是Graphical User Interface,即图形用户界面。顾名思义,就是应用程序提供给用户操作的图形界面,包括窗口、菜单、按钮、工具栏和其他各种图形界面元素。目前,图形用户界面已经成为一种趋势,几乎所有的程序设计语言都提供了GUI设计功能。Java中针对GUI设计提供了丰富的类库,这些类分别位于java.awt和javax.swing包中,简称为AWT和Swing。
AWT是用于创建图形用户界面的一个工具包,它提供了一系列用于实现图形界面的组件,如窗口、按钮、文本框、对话框等。如下图:

其中,Component类通常被称为组件,根据Component的不同作用,可将其分为基本组件类和容器类。而容器类则是通过Component的子类Container实例化的对象。Container类表示容器,它是一种特殊的组件,可以用来容纳其他组件。
1、Frame
Frame相当于一个大的框架,再它里面可以放多个组件、多个面版。它是相对于了window界面来说的。

//创建一个Frame实例对象:
Frame frame = new Frame("This is my first FrameTest");
//设置界面可见:
frame.setVisible(true);
//设置窗口大小:
frame.setSize(500,500);
//设置背景颜色:
frame.setBackground(Color.orange);
//设置弹出初始位置:
frame.setLocation(500,500);
//设置界面大小固定:
frame.setResizable(false);
2、Panel
Panel也是一个容器,但是它不能单独存在,只能存在其他容器(Window或其子类)中,一个Panel对象代表了一个长方形的区域,在这个区域中可以容纳其他组件。在程序中通常会使用Panel来实现一些特殊的布局。

//Panel类似于一个空间,但是不能单独使用(可再Frame里面使用Panel)
public static void main(String[] args) {
Frame frame = new Frame();
//在Frame里面的布局:
Panel panel = new Panel();
//设置布局:
frame.setLayout(null); //默认将Panel属性置顶
//设置Frame的坐标:
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(82, 142, 146)) ;
//设置Panel的坐标,在Frame里面的左边:
panel.setBounds(50,50,100,100);
panel.setBackground(new Color(47, 15, 45));
//将Panel的布局加入到Frame里面去:
frame.add(panel);
frame.setVisible(true);
3、布局管理器
当一个容器被创建后,它们都会有一个默认的布局管理器。Window、Frame和Dialog的默认布局管理器是BorderLayout,Panel的默认布局管理器是FlowLayout。如果不希望通过布局管理器来对容器进行布局,也可以调用容器的setLayout(null)方法,将布局管理器取消。
组件不能单独存在,必须放置于容器当中,而组件在容器中的位置和尺寸是由布局管理器来决定的。接下来,我们就介绍三种比较常用的布局管理器:
1)流式布局管理器 FlowLayout
在流式布局下,容器会将组件按照添加顺序从左向右放置。当到达容器的边界时,会自动将组件放到下一行的开始位置。这些组件可以左对齐、居中对齐(默认方式)或右对齐的方式排列。FlowLayout对象有三个构造方法

三个构造方法:

Frame frame = new Frame();
frame.setVisible(true);
//组件-button
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
//设置流式布局:
frame.setLayout(new FlowLayout());
// frame.setLayout(new FlowLayout(FlowLayout.LEFT)); //布局偏左
// frame.setLayout(new FlowLayout(FlowLayout.RIGHT)); //布局偏右
frame.setSize(200,200);
//把button填加到frame:
frame.add(button1);
frame.add(button2);
frame.add(button3);
2)边界布局管理器 BorderLayout
BorderLayout(边界布局管理器)是一种较为复杂的布局方式,它将容器划分为五个区域,分别是东(EAST)、南(SOUTH)、西(WEST)、北(NORTH)、中(CENTER)。组件可以被放置在这五个区域中的任意一个。

Frame frame = new Frame("TestBorderLayout:");
frame.setVisible(true);
frame.setSize(500,500);
Button button1 = new Button("East");
Button button2 = new Button("West");
Button button3 = new Button("South");
Button button4 = new Button("North");
Button button5 = new Button("Center");
//BorderLayout的使用:
frame.add(button1,BorderLayout.EAST); //add(添加的组件,边界布局设置)
frame.add(button2,BorderLayout.WEST);
frame.add(button3,BorderLayout.SOUTH);
frame.add(button4,BorderLayout.NORTH);
frame.add(button5,BorderLayout.CENTER);
3)网格布局管理器 GridLayout
GridLayout(网格布局管理器)使用纵横线将容器分成n行m列大小相等的网格,每个网格中放置一个组件。可以设置为几行几列的布局。类似于前端中的table。
以下是GridLayout的几个构造方法:

Frame frame = new Frame("TestGridLayout:");
frame.setSize(500,500);
frame.setVisible(true);
//设置布局格式:3行2列
frame.setLayout(new GridLayout(3,2));
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
Button button5 = new Button("button5");
Button button6 = new Button("button6");
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
使用GridLayout布局,可以将Panel面板嵌套放入其中,组合使用。
4、事件监听
现在我们基本已经可以创建简单的图形界面了,但是会发现创建的界面点击关闭按钮,无法关闭。这就涉及到了事件处理的问题了。事件处理机制专门用于响应用户的操作,比如,想要响应用户的单击鼠标、按下键盘等操作,就需要使用AWT的事件处理机制。

1)窗口监听事件
Window类型的窗口需要实现WindowListener。接着通过addWindowListener()方法为事件源注册事件监听器对象,当事件源上发生事件时,便会触发事件监听器对象,由事件监听器调用相应的方法来处理相应的事件。MyWindowListener类实现WindowListener接口后,需要实现接口中定义的7个方法,然而在程序中需要用到的只有windowClosing()一个方法,其他六个方法都是空实现,没有发挥任何作用,所以可以使用事件适配器。
当对窗体事件进行处理时,首先需要定义一个实现了WindowListener接口的类作为窗体监听器,然后通过addWindowListener()方法将窗体对象与窗体监听器绑定。
public class WindowListeners {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBounds(100,100,400,300);
//添加button组件到界面中:
Button button = new Button("setVisible!");
add(button);
setVisible(true);
//设置窗口监听,使用适配器模式:
addWindowListener(new WindowAdapter() {
//监听窗口关闭:
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
}
//监听窗口动态:
@Override
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");
}
});
}
}
2)鼠标监听事件
在图形用户界面中,用户会经常使用鼠标来进行选择、切换界面等操作,这些操作被定义为鼠标事件,JDK中提供了一个MouseEvent类用于表示鼠标事件,几乎所有的组件都可以产生鼠标事件。
首先需要通过实现MouseListener接口定义监听器(也可以通过继承适配器MouseAdapter类来实现),然后调用addMouseListener()方法将监听器绑定到事件源对象。
public class mouse extends Frame {
public mouse(){
setBounds(100,100,400,350);
setVisible(true);
Button b1 = new Button("btn1");
Button b2 = new Button("btn2");
//添加鼠标事件:
b1.addMouseListener(new MouseListen());
add(b1,BorderLayout.SOUTH);
add(b2,BorderLayout.NORTH);
}
class MouseListen extends MouseAdapter {
//定义鼠标按压触发监听效果:
@Override
public void mousePressed(MouseEvent e) {
Object source = e.getSource();
System.out.println("鼠标点击了!");
}
}
public static void main(String[] args) {
new mouse();
}
3)键盘监听事件
键盘操作也是最常用的用户交互方式,例如键盘按下、释放等,这些操作被定义为键盘事件。JDK中提供了一个KeyEvent类表示键盘事件,处理KeyEvent事件的监听器对象需要实现KeyListener接口或者继承KeyAdapter类。
public class KeyListenP {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setBounds(100,100,400,300);
setVisible(true);
//键盘监听事件:
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
//当键盘按压下去时,输入按下的键值:
int keyCode = e.getKeyCode();
if (keyCode ==e.VK_ENTER){ //可以使用KeyEvent.VK_ENTER代表的键盘的值
System.out.println("按下回车键");
}
}
});
ListenClose(this);
}
}
4)动作事件
动作事件与前面三种事件有所不同,它不代表某个具体的动作,只是表示一个动作发生了。动作事件用ActionEvent类表示,处理ActionEvent事件的监听器对象需要实现ActionListener接口。可适用于窗口、鼠标、键盘等事件的监听处理。
public class TestActionListen {
public static void main(String[] args) {
//按下按钮,触发一些事件:
Frame frame = new Frame();
frame.setSize(400,300);
frame.setVisible(true);
Button button = new Button("点击一下!");
frame.add(button,BorderLayout.CENTER);
//构造一个ActionListener:
MyAction myAction = new MyAction();
button.addActionListener(myAction);
ListenCLose(frame);
}
//关闭窗口事件:
private static void ListenCLose(Frame frame) {
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//事件监听:
class MyAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
Button button = new Button();
System.out.println(e.getSource());
System.out.println("button点击了");
}
}
5、输入框TextField
TextField组件即文本输入框,可以让用户再该界面上输入文本信息,这是个基本的组件。

public class TestTextField {
public static void main(String[] args) {
new MyFrame1();
}
}
class MyFrame1 extends Frame{
public MyFrame1(){
TextField text = new TextField();
add(text);
//监听这个文本输入的文字:
MyActionL myActionL = new MyActionL();
//按下enter,触发这个输入框的事件:
text.addActionListener(myActionL);
setSize(400,200);
setVisible(true);
text.setEchoChar('*'); //设置编码替换:把输入的数据转换为**显示
}
class MyActionL implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField) e.getSource(); //获取资源,返回一个对象
System.out.println(field);
System.out.println(field.getText()); //获取输入框的文本
field.setText(""); //按下回车后文本框重置为空
}
}
6、画笔
很多GUI程序都需要在组件上绘制图形,再java中我们可以使用paint画笔工具,其包含了一个Graphics类,提供了各种绘制图形的方法,使用Graphics类的方法就可以完成在组件上绘制图形。以下为各种常用的方法:

public class TestPaint {
public static void main(String[] args) {
new MyPaint().LoadFrame();
}
}
class MyPaint extends Frame{
public void LoadFrame(){
setBounds(600,300,600,500);
setVisible(true);
}
//画笔:
@Override
public void paint(Graphics g) {
// g.drawRoundRect(200,200,300,200,300,200);
g.fillOval(100,100,100,100);
// g.setColor(Color.red);
g.fillRect(200,200,300,200);
}
}
7、Swing
Swing包中提供了更加丰富、便捷、强大的GUI组件,而且这些组件都是Java语言编写而成的,因此,Swing组件不依赖于本地平台,可以真正做到跨平台运行。这些组件的名字基本都差不多,只是在前面加上一个J。

- JFrame:最常见的一个就是JFrame,它和Frame一样是一个独立存在的顶级窗口。
- JDialog:JDialog是Swing的另外一个顶级窗口,它和Dialog一样都表示对话框。相当于弹窗

public class TestSwingP {
public void init(){
JFrame jFrame = new JFrame("This is Swing window");
jFrame.setBounds(100,100,400,300);
jFrame.setVisible(true);
//一:SwingConstants.CENTER,让文本居中显示:
// JLabel jLabel = new JLabel("this is Jlabel",SwingConstants.CENTER);
JLabel jLabel = new JLabel("this is Jlabel");
JButton button = new JButton("button");
//二:让文本水平对齐:
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
//设置颜色:
jFrame.getContentPane().setBackground(Color.pink);
button.addActionListener(new ActionLISten());
jFrame.add(jLabel);
jFrame.add(button,BorderLayout.EAST);
//关闭窗口事件:
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestSwingP().init();
}
class ActionLISten implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog();
dialog.setBounds(100,100,150,100);
dialog.setVisible(true);
dialog.setLayout(null);
JLabel label = new JLabel("this is JDialog");
label.setBounds(100,100,90,30);
dialog.add(label);
}
}
}
8、标签
可以使用paint画图,画出想要的图形添加到标签上,也可以把图片添加到标签上,再将标签放到适合的位置。
//将图片添加到标签上:
public class TestIconJpg extends JFrame{
public TestIconJpg(){
setBounds(100,100,400,300);
setVisible(true);
JLabel label = new JLabel("imgIcon");
//获取图片的地址:(获取当前'类'路径最快的方法) 元素:ImageIcon
URL url = TestIconJpg.class.getResource("img1.jpg");
ImageIcon image = new ImageIcon(url);
label.setIcon(image);
label.setHorizontalAlignment(SwingConstants.CENTER); //设置水平居中
Container container = getContentPane();
container.add(label);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new TestIconJpg();
}
}
9、按钮
在Swing中,常见的按钮组件有JButton、JCheckBox、JRadioButton等,它们都是抽象类AbstractButton类的直接或间接子类。以下是按钮的常用方法:

//按钮:
JButton button = new JButton("按钮");
button.setBounds(220,110,70,30);
container.add(button);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
1)单选按钮
JRadioButton组件被称为单选按钮,与JCheckBox复选框不同的是,单选按钮只能选中一个。

注意:单选框,需要设置分组,才可以使用其单选的作用:ButtonGroup()
public class TestJbutton2 extends JFrame {
public TestJbutton2(){
Container container = getContentPane();
setBounds(100,100,400,300);
setVisible(true);
//单选框:
JRadioButton radioButton1 = new JRadioButton("单选框1");
JRadioButton radioButton2 = new JRadioButton("单选框2");
JRadioButton radioButton3 = new JRadioButton("单选框3");
//单选框,需要设置分组,才可以使用其单选的作用:
ButtonGroup bg = new ButtonGroup();
bg.add(radioButton1);
bg.add(radioButton2);
bg.add(radioButton3);
container.add(radioButton1,BorderLayout.NORTH);
container.add(radioButton2,BorderLayout.CENTER);
container.add(radioButton3,BorderLayout.SOUTH);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJbutton2();
}
}
2)复选按钮
JCheckBox组件被称为复选框,它有选中(是)/未选中(非)两种状态,如果用户想接收的输入只有“是”和“非”,则可以通过复选框来切换状态。
;
public class TestJbutton3 extends JFrame {
public TestJbutton3(){
Container container = getContentPane();
setBounds(100,100,400,300);
setVisible(true);
//多选框:
JCheckBox checkBox1 = new JCheckBox("checkBox1");
JCheckBox checkBox2 = new JCheckBox("checkBox2");
JCheckBox checkBox3 = new JCheckBox("checkBox3");
container.add(checkBox1,BorderLayout.NORTH);
container.add(checkBox2,BorderLayout.CENTER);
container.add(checkBox3,BorderLayout.SOUTH);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJbutton3();
}
}
10、下拉框&列表框
1)下拉框
JComboBox组件被称为组合框或者下拉列表框,它将所有选项折叠收藏在一起,默认显示的是第一个添加的选项。当用户单击组合框时,会出现下拉式的选择列表,用户可以从中选择其中一项并显示。

以下是JComboBox一些常用的方法:

public class TestJbox1 extends JFrame {
public TestJbox1(){
Container container = getContentPane();
setBounds(100,100,400,300);
setLayout(null);
setVisible(true);
//下拉框:
JComboBox comboBox = new JComboBox();
comboBox.setBounds(100,100,100,30);
comboBox.addItem(null);
comboBox.addItem("秒杀特价");
comboBox.addItem("夏季特款");
comboBox.addItem("爆款热款");
container.add(comboBox);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJbox1();
}
}
2) 列表框
列表框的用法与下拉框基本差不多,只是其需要定义一个数组,再将改数组添加到列表中去-->JList()
public class TestJbox2 extends JFrame {
public TestJbox2(){
Container container = getContentPane();
setBounds(100,100,400,300);
setVisible(true);
//生成列表框的内容:
//第一种:
// String[] str = {"1","2","3","4"};
// JList list = new JList(str);
// container.add(list);
//第二种:
Vector vector = new Vector();
JList list = new JList(vector);
vector.add("列表框1");
vector.add("列表框2");
vector.add("列表框3");
vector.add("列表框4");
container.add(list);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJbox2();
}
}
11、文本域&密码框&文本框
文本组件用于接收用户输入的信息或向用户展示信息,其中包括文本框(JTextField)、文本域(JTextArea)等,它们都有一个共同父类JTextComponent。

1)JTextField
JTextField称为文本框,它只能接收单行文本的输入。JTextField常用的构造方法,如下表所示:

注意:JTextField有一个子类JPasswordText,它表示一个密码框,只能接收用户的单行输入,但是在此框中不显示用户输入的真实信息,而是通过显示指定的回显字符作为占位符。
2)JTextArea
JTextArea称为文本域,它能接收多行的文本的输入,使用JTextArea构造方法创建对象时可以设定区域的行数、列数。接下来,介绍一下JTextArea常用的构造方法,如下表所示:

public class TesttextbuttonP extends JFrame {
public TesttextbuttonP(){
Container container = getContentPane();
setBounds(100,100,400,300);
setVisible(true);
setLayout(null);
//文本框:
JTextField text = new JTextField("this is textFiled");
text.setBounds(10,10,100,30);
container.add(text);
//密码框:
JPasswordField passd = new JPasswordField();
passd.setBounds(10,50,100,30);
passd.setEchoChar('*');
container.add(passd);
//文本域:
JTextArea area = new JTextArea("this is textarea");
// area.setBounds(10,100,200,100);
//设置滚动条:
JScrollPane jScrollPane = new JScrollPane(area);
jScrollPane.setBounds(10,100,200,100);
container.add(jScrollPane);
}
public static void main(String[] args) {
new TesttextbuttonP();
}
}
到这里,基本的GUI就已经差不多讲完了,接下来就是靠自己努力复习练习了,加油!****

浙公网安备 33010602011771号