JAVA--图形用户界面编程(GUI)

JAVA–图形用户界面编程(GUI)

图形用户界面编程的概述

1、图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式显示的计算机操作用户界面。
2、Java应用程序的图形用户界面是通过Java API提供的java.awt或java.swing包中的组件实现的。这些组件构成的GUI系统通常包括一下几个部分

  • 最基本的图形用户界面组件,如菜单、按钮、文本字段等,展示系统可用的操作。
  • 容器组件,如窗口、面板等,用于容纳基本组件
  • 布局管理组件,负责容器中的组件布局,可进一步美化图形用户界面界面
  • 事件的处理用户通过图形界面进行操作时,会引发相应的事件,这些事件由一些特定的图形用户界面组件监听并处理

GUI编程基础

AWT中主要类的继承关系

在这里插入图片描述

Swing中主要类的继承关系

在这里插入图片描述

常用组件与容器

顶层容器(JFrame、JDialog)

- JFrame
JFrame窗体是一个容器,它是Swing程序中各个组件的载体,可以将JFrame看作是承载着血Swing组件的容器。在开发应用程序时,可以通过继承java.swing.JFrame类创建一个窗体。在这个窗体中添加组件,同时为组件设置事件。

package Demo01;

import java.awt.Color;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JLabel;


public class jFrameTest extends JFrame{
	public jFrameTest() {
//		JFrame f = new JFrame("测试窗口");

		setVisible(true);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocation(600,300);
		setSize(500, 350);
		Container c = getContentPane();
		c.setBackground(Color.gray);
		JLabel l = new JLabel("测试窗口");
		c.add(l);
//		c.remove(l);
		c.validate();
		setContentPane(c);
		setResizable(true);
//		setBounds(500, 200,400, 400);
	}
public static void main(String[] args)  {
	JFrame f = new JFrame("测试窗口");
	new jFrameTest();
}
}

在这里插入图片描述

- JDialog
JDialog窗体是Swing组件中的对话框,它继承了AWT组件中java.awt.Dialog类。

JDialog窗体的功能是从另外一个窗体中弹出另外一个窗体,就像是在使用IE浏览器时弹出的确定对话框一样。JDialog窗体实质上就是另一种类型的窗体,它与JFrame窗体类似,在使用时也需要调用getCOntentPane()方法将窗体转换成容器,然后在容器中 设置窗体的特性。

package Demo01;

import java.awt.Color;
import java.awt.Container;

import javax.swing.JDialog;
import javax.swing.JLabel;

public class JDialogTest extends JDialog{
public JDialogTest() {
	setVisible(true);
}
public static void main(String[] args) {
//	new JDialogTest();
	JDialog jd = new JDialog();
	jd.setVisible(true);
	jd.setBounds(200,200,400,400);
	
	Container c =jd.getContentPane();
	c.add(new JLabel("测试"));
	c.setBackground(Color.pink);
	
	
}
}

在这里插入图片描述

package button;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class MyDialog {
	public static void main(String[] args) {
		JLabel la = new JLabel();
		int n = JOptionPane.showConfirmDialog(null, "请点击按钮");
		if (n==0) 
			la.setText("您点击了按钮\"是\"!");
		else if(n==1) 
			la.setText("您点击了按钮\"否\"!");
		else if(n==2)
			la.setText("您点击了按钮\"取消\"!");
			
		
	}
}

在这里插入图片描述

中间容器

package Demo01;

import java.awt.Button;
import java.awt.Container;
import java.awt.Label;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;

public class DialogDemo01 extends JDialog {
	public DialogDemo01(JFrame jframe) {
		super(jframe,true);
		this.setBounds(100,100,300,300);	
		Container container1= this.getContentPane();
		container1.setLayout(null);
		//container.add(new Label("  "));
		JButton button1=new JButton("测试");
		button1.setBounds(30, 30, 200,50);
		container1.add(button1);
	}
	public static void main(String[] args) {
		JFrame j=new JFrame();
		j.setVisible(true);
		j.setBounds(100,100,700,500);
		j.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		Container container= j.getContentPane();
		container.setLayout(null);
		JButton button=new JButton("测试");
		button.setBounds(30, 30, 200,50);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DialogDemo01 s=new DialogDemo01(j);
				s.setVisible(true);
				System.out.println("测试成功");
			}
		});
		container.add(button);
		
	}

}

在这里插入图片描述

标签

package Demo02;

import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JPaneDemo extends JFrame {
	public JPaneDemo() {
		Container container1= this.getContentPane();
		container1.setLayout(new GridLayout(2,1,10,10));
		JPanel panel=new JPanel(new GridLayout(1,3));
		panel.add(new JButton("1"));
		panel.add(new JButton("2"));
		panel.add(new JButton("3"));
		container1.add(panel);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
		this.setBounds(100,100,400,400);
	}
public static void main(String[] args) {
	new JPaneDemo();
   }
}

在这里插入图片描述

按钮

package Demo02;

import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JPaneDemo extends JFrame {
	public JPaneDemo() {
		Container container1= this.getContentPane();
		container1.setLayout(new GridLayout(2,1,10,10));
		JPanel panel=new JPanel(new GridLayout(1,3));
		panel.add(new JButton("1"));
		panel.add(new JButton("2"));
		panel.add(new JButton("3"));
		container1.add(panel);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
		this.setBounds(100,100,400,400);
	}
public static void main(String[] args) {
	new JPaneDemo();
   }
}

在这里插入图片描述

文本框

package Demo01;

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class TestTextDemo01 extends JFrame{
	public TestTextDemo01() {
		Container container = this.getContentPane();
		JTextField textfild1 =  new JTextField("测试01");
		JTextField textfild2 =  new JTextField("测试02");
		container.add(textfild1,BorderLayout.NORTH);
		container.add(textfild2,BorderLayout.SOUTH);
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setBounds(500, 250, 500, 500);
	}
public static void main(String[] args) {
	new TestTextDemo01();
	
}
}

在这里插入图片描述

布局管理

FlowLayout布局管理器

package Demo03;

import java.awt.FlowLayout;

import javax.swing.*;

public class TestFlowLayout {
	public static void main(String[] args) {
		JFrame j=new JFrame();
		j.setVisible(true);
		j.setBounds(100,100,700,500);
		j.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		//j.setLayout(new FlowLayout());
		//j.setLayout(new FlowLayout(FlowLayout.RIGHT));
		j.setLayout(new FlowLayout(FlowLayout.LEFT));
		JButton button1=new JButton("button1");
		JButton button2=new JButton("button2");
		JButton button3=new JButton("button3");
		JButton button4=new JButton("button4");
		JButton button5=new JButton("button5");
		JButton button6=new JButton("button6");
		j.add(button1);
		j.add(button2);
		j.add(button3);
		j.add(button4);
		j.add(button5);
		j.add(button6);
	}

}

在这里插入图片描述

BorderLayout布局管理器

package Demo03;

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class TestBorderLayout {
	public static void main(String[] args) {
		JFrame j=new JFrame();
		j.setVisible(true);
		j.setBounds(100,100,700,500);
		j.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		
		JButton east=new JButton("east");
		JButton west=new JButton("west");
		JButton south=new JButton("south");
		JButton north=new JButton("north");
		JButton center=new JButton("我");
		
		j.add(east,BorderLayout.EAST);
		j.add(west,BorderLayout.WEST);
		j.add(south,BorderLayout.SOUTH);
		j.add(north,BorderLayout.NORTH);
		j.add(center,BorderLayout.CENTER);
	}

}

在这里插入图片描述

GridLayout布局管理器

package Demo03;

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class TestGrid {
	public static void main(String[] args) {
		JFrame j=new JFrame();
		j.setVisible(true);
		j.setBounds(100,100,700,500);
		j.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		j.setLayout(new GridLayout(3,2));
		
		JButton button1=new JButton("button1");
		button1.setSize(50,50);
		JButton button2=new JButton("button2");
		
		JButton button3=new JButton("button3");
		JButton button4=new JButton("button4");
		JButton button5=new JButton("button5");
		//JButton button6=new JButton("button6");
		j.add(button1);
		j.add(button2);
		j.add(button3);
		j.add(button4);
		j.add(button5);
		//j.add(button6);
		//j.pack();
	}

}

在这里插入图片描述

事件的类型和处理方法

  • 低级事件
事件类名事件描述
FocusEvent在组件获得焦点或失去焦点时产生的事件
MouseEvent用户对鼠标操作所产生的事件
KeyEvent用户对键盘操作所产生的事件
WindowEvent用户对窗口所以产生的事件
  • 语义事件
  • 键盘事件的处理
package Demo03;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class TestKeyListener {
public static void main(String[] args) {
	new KeyFrame();
}
}
class KeyFrame extends JFrame{
	public KeyFrame() {
		this.setVisible(true);
		this.pack();
		this.setBounds(500, 250, 500, 500);
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		this.addKeyListener((KeyListener) new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				int Keycode = e.getKeyCode();
				System.out.println(Keycode);
				if (Keycode==KeyEvent.VK_UP) {
					System.out.println("您点击了↑");
				}if (Keycode==KeyEvent.VK_W) {
					System.out.println("您点击了↓");
				} else {

				}
			}
		});
	}
}

在这里插入图片描述

  • 鼠标事件的处理
package Demo03;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class TextMouseListener {
public static void main(String[] args) {
	new MyFrame("测试");
}
}
class MyFrame extends JFrame{
	ArrayList points;
	public MyFrame(String title) {
		super(title);
		this.setVisible(true);
//		this.pack();
		this.setBounds(500, 250, 500, 500);
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		points = new ArrayList<>();
		this.addMouseListener(new MyMouseListener());
//	public void addPaint(Point point) {
//		points.add(point);
//	}
	
	}
	@Override
	public void paint(Graphics g) {
		Iterator iterator=points.iterator();
		while (iterator.hasNext()) {
			Point point=(Point)iterator.next();
			 g.setColor(Color.cyan);
	         g.fillOval(point.x,point.y,10,10);
			
		}
		
	}
public void addPaint(Point point) {
	points.add(point);
}	
private class MyMouseListener extends MouseAdapter{
	public void mousePressed(MouseEvent e) {
		MyFrame myFrame =(MyFrame) e.getSource();
		System.out.println("X"+e.getX()+"Y"+e.getY());
		myFrame.addPaint(new Point(e.getX(),e.getY()));
		myFrame.repaint();
		
	}
}
//private class MyMouseListener(new MyMouseListener());
}


在这里插入图片描述

综合联动测试

package JFrame1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class lss {
    public static void main(String[] args) {
        JFrame j = new JFrame("按钮测试");
        j.setVisible(true);
        j.setBounds(600,300,300,150);
        j.setDefaultCloseOperation(j.EXIT_ON_CLOSE);

        JButton b1 = new JButton("上");
        JButton b2 = new JButton("下");
        j.add(b1,BorderLayout.NORTH);
        j.add(b2,BorderLayout.SOUTH);

        JLabel l = new JLabel("",SwingConstants.CENTER);
        j.add(l,BorderLayout.CENTER);


        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getActionCommand().equals("上")){
                    l.setText("你点击了上方按钮");

                }
            }
        });


        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getActionCommand().equals("下")){
                    l.setText("你点击了下方按钮");
                }
            }
        });

    }
}

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java GUI图形界面编程是指使用Java编程语言来创建图形用户界面(Graphical User Interface,GUI)应用程序的过程。Java提供了一组丰富的类库和工具,使得开发人员可以快速、方便地创建交互性强、用户友好的应用程序。 Java GUI图形界面编程的主要组成部分是Swing和JavaFX两个库。Swing是一个基于Java图形用户界面工具包,提供了丰富的用户界面组件,如按钮、文本框、表格等,可以在应用程序中创建和管理这些组件,实现用户界面的交互。JavaFX是Java平台上的富客户端应用程序框架,提供了更强大、更灵活的界面组件和布局控制,可以创建更加先进、更具吸引力的GUI应用程序。 使用Java GUI图形界面编程进行应用程序开发有许多优点。首先,Java GUI库提供了丰富的组件和控件,开发人员可以根据自己的需求选择合适的组件,快速构建出用户界面。其次,Java GUI库具有良好的跨平台性,可以在不同操作系统上运行,并且具有相同的用户界面。再者,Java提供了强大的事件驱动模型,使得应用程序能够响应用户的操作,实现交互性。 总之,Java GUI图形界面编程是一种灵活、方便、强大的开发方式,使得开发人员能够更加简单地创建出功能强大、用户友好的应用程序。无论是简单的桌面应用还是复杂的企业级应用,都可以通过Java GUI图形界面编程来实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值