一,简介
GUI编程(Graphical User Interface),即用户图形界面编程。
GUI淘汰原因:界面不够美观,需要jre环境。
学习GUI的好处:1,为后期学习MVC架构打好基础;2,了解监听;3,工作时可能需要维护到Swing界面。
GUI的基本组件:窗口、弹窗、面板、文本框、列表框、按钮、图片、交互组件、监听事件、鼠标事件、键盘事件。
GUI的核心:AWT和Swing。
二,AWT
1,AWT介绍
AWT是用于创建图形用户界面的一个工具包,它提供了一系列用于实现图形界面的组件,如窗口、按钮、文本框、对话框等。在JDK中针对每个组件都提供了对应的java类,这些类都位于java.awt包中。
2,组件和容器
Component类通常被称为组件,根据Component的不同作用,可将其分为基本组件类和容器类。基本组件类是诸如按钮、文本框之类的图形界面元素,而容器类则是通过Component的子类Contaier类表示容器,它是一种特殊的组件,可以用来容纳其他组件。
我的第一个javaGUI图像界面窗口,代码如下:
//Frame 对象
Frame frame = new Frame("我的第一个javaGUI图像界面窗口");
//需要设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色
frame.setBackground(new Color(85,150,68));
//弹出的初始设置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(true);
运行后,可出现一个绿色的窗口,读者可自行实验。
3,布局管理器
1)流式布局
代码如下:
Frame frame = new Frame();
//组件-按钮
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
//设置为流式布局
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setSize(200,200);
//把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
//可见性
frame.setVisible(true);
运行后:
2)表格布局
代码如下:
Frame frame = new Frame();
Button btn1 = new Button("btn1");
Button btn2 = new Button("btn2");
Button btn3 = new Button("btn3");
Button btn4 = new Button("btn4");
Button btn5 = new Button("btn5");
Button btn6 = new Button("btn6");
//3行2列
frame.setLayout(new GridLayout(3,2));
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.add(btn6);
frame.pack();//无需设置尺寸,自动大小
frame.setVisible(true);
运行后:
3)东西南北中布局
Frame frame = new Frame();
Button east = new Button("East");
Button west = new Button("West");
Button south = new Button("South");
Button north = new Button("North");
Button center = new Button("Center");
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(south,BorderLayout.SOUTH);
frame.add(north,BorderLayout.NORTH);
frame.add(center,BorderLayout.CENTER);
frame.setSize(200,200);
frame.setVisible(true);
运行后:
4,事件监听
//事件监听
public class TestActionEvent {
public static void main(String[] args) {
//按下按钮,触发一些事件
Frame frame = new Frame();
Button button = new Button();
//因为,addActionListener需要一个ActionListener,所以需要构造一个ActionListener
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
windowClose(frame);
}
//关闭窗口事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//super.windowClosing(e);
System.exit(0);
}
});
}
}
//事件监听
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
}
5,输入框TextField监听
public class TestText01 {
public static void main(String[] args) {
//启动
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame() throws HeadlessException {
TextField textField = new TextField();
add(textField);
//监听这个文本框输入的文字
MyActionListener2 myActionListener2 = new MyActionListener2();
//按下Enter,就会触发这个输入框事件
textField.addActionListener(myActionListener2);
//设置替换编码
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField)e.getSource();//获得一些资源,返回的一个对象
System.out.println(field.getText());//获取输入框的文本
field.setText("");
}
}
6,利用监听事件实现简易计算器
//简易计算器
public class TestCalc {
public static void main(String[] args) {
new Calculator();
}
}
//计算器类
class Calculator extends Frame{
public Calculator() {
//3个文本框
TextField num1 = new TextField(10);//字符数
TextField num2 = new TextField(10);//字符数
TextField num3 = new TextField(20);//字符数
//1个按钮
Button button = new Button("=");
//为了实现加法,这个button需要绑定一个监听事件
button.addActionListener(new MyCalculatorListener(num1,num2,num3));
//1个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class MyCalculatorListener implements ActionListener{
//监听事件需要做的:获取三个变量
private TextField num1,num2,num3;
//获取计算器这个对象
Calculator calculator = null;//这个可以替换上面初始化三个变量,体现组合的思想
public MyCalculatorListener(TextField num1,TextField num2,TextField num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
//1,获取加数和被加数
int n1 = Integer.parseInt(num1.getText());//用Integer包装类,将字符型转换为int型
int n2 = Integer.parseInt(num2.getText());
//2,将这个值加法运算后放到第三个框
num3.setText(""+(n1+n2));
//3,清除前两个框
num1.setText("");
num2.setText("");
}
}
运行实例:
点击等号按钮后:
7,画笔
//画笔
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200,200,600,400);
setVisible(true);
}
//画笔
@Override
public void paint(Graphics g) {
g.setColor(Color.red);
//g.drawOval(100,100,100,100);//空心圆
g.fillOval(100,100,100,100);//实心圆
g.setColor(Color.GREEN);
g.fillRect(200,200,200,200);
}
}
运行后:
8,鼠标监听
//鼠标监听
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame("画图");
}
}
//自己的类
class MyFrame extends Frame{
//画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
ArrayList points;
public MyFrame(String title) {
super(title);
setBounds(200,200,400,300);
//存鼠标点击的点
points = new ArrayList<>();
//鼠标监听器,针对这个窗口
this.addMouseListener(new MyMouseListener());
setVisible(true);
}
@Override
public void paint(Graphics g) {
//super.paint(g);
//画画,监听鼠标事件
Iterator iterator = points.iterator();
while(iterator.hasNext()){
Point point = (Point) iterator.next();
g.setColor(Color.red);
g.fillOval(point.x,point.y,10,10);
}
}
//添加一个点到界面上
public void addPoint(Point point){
points.add(point);
}
//内部类 适配器模式
private class MyMouseListener extends MouseAdapter {
//鼠标 按下,弹起,按住不放
@Override
public void mousePressed(MouseEvent e) {
//super.mousePressed(e);
MyFrame frame = (MyFrame) e.getSource();
//我们点击时,就会在界面上产生一个点
frame.addPoint(new Point(e.getX(),e.getY()));
//这个点就是鼠标的点
//new Point(e.getX(),e.getY());
//每次点击鼠标都需要重新画一遍
frame.repaint();//刷新
}
}
}
9,窗口监听
//窗口监听
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBackground(Color.blue);
setBounds(100,100,200,200);
setVisible(true);
//addWindowListener(new MyWindowListener());
this.addWindowListener(
//匿名内部类
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//super.windowClosing(e);
System.out.println("windowClosing");//关闭窗口
System.exit(0);
}
@Override
public void windowActivated(WindowEvent e) {
//super.windowActivated(e);
WindowFrame source = (WindowFrame) e.getSource();
source.setTitle("被激活了");
System.out.println("windowActivated");//激活窗口
}
}
);
}
}
10,键盘监听
//键盘监听
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame() {
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {//键盘按下
//super.keyPressed(e);
//获得键盘下的键是哪一个,当前的码
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
if(keyCode == KeyEvent.VK_DOWN){
System.out.println("你按下了下键");
}
}
});
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
三,Swing
1,Swing介绍
Swing是一个为java设计的GUI工具包,是java基础类的一部分。Swing包括了图形用户界面(GUI)器件如 : 文本框,按钮,分隔窗格和表。Swing提供许多比AWT更好的屏幕显示元素。它们用纯Java写成,所以同Java本身一样可以跨平台运行,这一点不像AWT,它们是JFC一部分,支持可更换的面板和主题,然而不是真的使用原生平台提供的设备,而是仅仅在表面上模仿它们。这意味着你可以在任意平台上使用java支持的任意面板。
2,JFrame窗口
//初始化
public void init(){
//顶级窗口
JFrame jframe = new JFrame("这个一个JFrame窗口");
jframe.setVisible(true);
jframe.setBounds(100,100,200,200);
//设置文字
JLabel label = new JLabel("欢迎来到小西瓜的世界");
jframe.add(label);
//容器实例化
jframe.getContentPane();
//关闭事件
jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
3,JDialog弹窗
//主窗口
public class DialogDemo extends JFrame {
public DialogDemo() {
this.setVisible(true);
this.setSize(700,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放东西,容器
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("点击弹出一个对话框");//创建
button.setBounds(30,30,200,50);
//点击这个按钮的时候,弹出一个弹窗
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
});
container.add(button);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo() {
this.setVisible(true);
this.setBounds(100,100,500,500);
//弹窗默认可以关闭,无需写这行代码
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
container.add(new Label("小西瓜爱学java"));
}
}
4,Icon、ImageIcon标签
private int width;
private int height;
public IconDemo(){}//无参构造
//有参构造
public IconDemo(int width, int height){
this.width = width;
this.height = height;
}
public void init(){
IconDemo iconDemo = new IconDemo(15, 15);
//图标放在标签上,也可以放在按钮上
JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new IconDemo().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
public ImageIconDemo(){
//获取图片地址
JLabel label = new JLabel("ImageIcon");
//图片需要与代码在同级目录下
URL url = ImageIconDemo.class.getResource("tx.jpg");
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100,100,200,200);
}
public static void main(String[] args) {
new ImageIconDemo();
}
5,JScroll面板
//实现滚动
public JScrollDemo() {
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("欢迎学习java");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
6,按钮(图片按钮)
public JButtonDemo01() {
Container container = this.getContentPane();
//将一个图片变为图标
URL url = JButtonDemo01.class.getResource("tx1.jpg");
ImageIcon icon = new ImageIcon(url);
//把这个图标放在按钮上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");
container.add(button);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
7,列表
1)单选框
Container container = this.getContentPane();
//将一个图片变为图标
URL url = JButtonDemo02.class.getResource("tx1.jpg");
ImageIcon icon = new ImageIcon(url);
//单选框
JRadioButton radioButton01 = new JRadioButton("JRadioButton01");
JRadioButton radioButton02 = new JRadioButton("JRadioButton02");
JRadioButton radioButton03 = new JRadioButton("JRadioButton03");
//由于单选框只能选择一个,所以一般会分组,一组中只能选一个
ButtonGroup group = new ButtonGroup();
group.add(radioButton01);
group.add(radioButton02);
group.add(radioButton03);
container.add(radioButton01,BorderLayout.CENTER);
container.add(radioButton02,BorderLayout.NORTH);
container.add(radioButton03,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
2)多选框
Container container = this.getContentPane();
//将一个图片变为图标
URL url = JButtonDemo03.class.getResource("tx1.jpg");
ImageIcon icon = new ImageIcon(url);
//复选框
JCheckBox checkBox01 = new JCheckBox("jCheckBox01");
JCheckBox checkBox02 = new JCheckBox("jCheckBox02");
JCheckBox checkBox03 = new JCheckBox("jCheckBox03");
container.add(checkBox01,BorderLayout.SOUTH);
container.add(checkBox02,BorderLayout.NORTH);
container.add(checkBox03,BorderLayout.CENTER);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
3)下拉框
Container container = this.getContentPane();
JComboBox comboBox = new JComboBox();
comboBox.addItem(null);
comboBox.addItem("正在热映");
comboBox.addItem("已下架");
comboBox.addItem("即将上映");
container.add(comboBox);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
4)列表框
Container container = this.getContentPane();
//生成列表的内容
String[] contents = {"1","2","3"};
//列表中需要放入内容
JList jList = new JList(contents);
container.add(jList);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
8,文本框
Container container = this.getContentPane();
JTextField textField1 = new JTextField("hello");
JTextField textField2 = new JTextField("world");
container.add(textField1,BorderLayout.NORTH);
container.add(textField2,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
9,密码框
Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
container.add(passwordField);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
10,文本域
//文本域 配合面板使用
Container container = this.getContentPane();
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("欢迎学习java");
container.add(textArea);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JavaGUI的基础学习,到此结束。