GUI(Graphical User Interface)即图形用户界面,是指采用图形方式显示的用户界面。
为程序提供了图形界面,方便用户操作
javax.swing包中提供了出许多的图形组件
在窗口组件中添加其他功能组件
JFrame类用来创建窗口;
我们的类可以直接继承该类,实现窗体制作。
void setSize(int width, int height);//设置窗口的长宽
void setVisible(boolean b);//该窗口是否可见
void setTitle(String title);//设置窗口名称
void setResizable(boolean resizable);//是否可以更改大小
void setLocation(int x,int y);//定义位置
void setLocationRelativeTo(null);//设置窗口位置,默认是居中的
void setDefaultCloseOperation(int operation);//设置关闭窗口的选项 关闭窗口退出程序
void dispose();//关闭窗口
标签(JLable)
标签是容纳文本和图标的控件,通常用来在界面中标识别的控件
构造函数:
JLable(String text)创建一个带文本的标签
方法:
setFont(new Font("宋体",Font.BOLD,18));设置字体
单行文本(JTextField)
构造方法:
JTextField(int columns)
方法:
String getText();//获取文本框中的文本
密码框(JPasswordField)
构造函数:
JPasswordField(String text)
JPasswordField(String text,int columns)
方法:
char[] getPassword()
多行文本框(JTextArea)
构造函数:
JTextArea(int rows,int columns)创建哟个指定行数和列数的空文本域
方法:
String getText() 获得文本域中的文本
void setFont(Font font)设置文本域中文本的字体
void setLineWrap(boolean wrap) 是否自动换行,默认为false
如果需要文本区自动出现滚动条,可将文本区对象放入滚动窗格(JScrollPane)中:
JScrollPane scrollPane = new JScrollPane(txtArea)
add(scrollPane)
按钮(JButton)
构造方法:
JButton(String text) 创建一个带文本的标签
方法:
void setBackground(Color bg) 设置按钮的背景色
void setEnabled(boolean b) 设置启用(或禁用)按钮,由参数b决定
void setToolTipText(String Text) 设置按钮的悬停提示信息
事件的监听处理
组件被点击会触发一个事件,就需要有对时间的监听器
监听器类型是ActionListener
添加事件监听器(此处为匿名类)
/*
* Created by JFormDesigner on Sun Mar 09 11:27:22 CST 2025
*/
package EG.Eg3.amend;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;
/**
* @author 25822
*/
public class Eg extends JFrame {
Boolean bz = true;
Random r = new Random();
public Eg() {
initComponents();
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents @formatter:off
label1 = new JLabel();
textField1 = new JTextField();
beginbutton = new JButton();
endbutton = new JButton();
textField2 = new JTextField();
//======== this ========
Container contentPane = getContentPane();
contentPane.setLayout(null);
//---- label1 ----
label1.setText("\u65b0\u7684\u5e78\u8fd0\u513f\u5df2\u7ecf\u4ea7\u751f");
label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 6f));
contentPane.add(label1);
label1.setBounds(85, 25, 180, 35);
contentPane.add(textField1);
textField1.setBounds(120, 70, 140, 40);
//---- beginbutton ----
beginbutton.setText("\u5f00\u59cb");
contentPane.add(beginbutton);
beginbutton.setBounds(new Rectangle(new Point(35, 135), beginbutton.getPreferredSize()));
//---- endbutton ----
endbutton.setText("\u505c\u6b62");
contentPane.add(endbutton);
endbutton.setBounds(new Rectangle(new Point(245, 140), endbutton.getPreferredSize()));
contentPane.add(textField2);
textField2.setBounds(70, 185, 225, 70);
{
// compute preferred size
Dimension preferredSize = new Dimension();
for(int i = 0; i < contentPane.getComponentCount(); i++) {
Rectangle bounds = contentPane.getComponent(i).getBounds();
preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
}
Insets insets = contentPane.getInsets();
preferredSize.width += insets.right;
preferredSize.height += insets.bottom;
contentPane.setMinimumSize(preferredSize);
contentPane.setPreferredSize(preferredSize);
}
pack();
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents @formatter:on
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
beginbutton.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
beginbutton.setEnabled(false);//点击开始后不能再点击
String name = textField2.getText();//获取所给文本
String[] names = name.split(",");
bz = true;
new Thread(new Runnable() {//创建线程
@Override public void run() {
while (bz){//随机输出姓名
try {
Thread.sleep(200);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
textField1.setText(names[r.nextInt(names.length)]);
}
}
}).start();
}});
endbutton.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
bz = false;
beginbutton.setEnabled(true);//还原循环
}});
}
public static void main(String[] args) {
new Eg();
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables @formatter:off
private JLabel label1;
private JTextField textField1;
private JButton beginbutton;
private JButton endbutton;
private JTextField textField2;
// JFormDesigner - End of variables declaration //GEN-END:variables @formatter:on
}
上述示例为创建一个随机点名的窗口
JOptionPane对话框
showMessageDialog():消息对话框
主要有五种消息类型,类型不同,图标不同:
ERROR_MESSAGE 错误消息提示
INFORMATION_MESSAGE 信息提示
WARNING_MESSAGE 警告提示
QUESTION_MESSAGE 问题提示
PLAIN_MESSAGE 简洁提示
showConfirmDialog():确认对话框
主要有四种消息类型,类型不同,图标不同:
DEFAULT_OPTION 默认选项
YES_NO_OPTION 是/否选项
YES_NO_CANCEL_OPTION 是/否/取消选项
OK_CANCEL_OPTION 确定/取消