GUI编程
输入框事件监听
画笔
鼠标监听
窗口监听
键盘监听
Swing
窗口,面板
弹窗
标签
面板
按钮
列表
文本框
GUI编程
输入框事件监听
package com.wang.Lesson02;
/**
* @Auther: 王海新
* @Date: 2021/2/9 12:47
* @Description: oop七大原则之一:组合,大于继承
*/
public class GroupDemo {
public static void main(String[] args) {
}
}
class A{//将GroupDemo类私有给A,这就是组合
public GroupDemo groupDemo;
}
package com.wang.Lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @Auther: 王海新
* @Date: 2021/2/9 13:09
* @Description: 简易计算器
*/
//启动类
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.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;
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());
int n2 = Integer.parseInt(num2.getText());
//2.将结果放到第三个框
num3.setText("" + (n1+n2));
//3.清空前两个框
num1.setText("");
num2.setText("");
}
}
优化后代码:完全面向对象写法。组合思想
package com.wang.Lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @Auther: 王海新
* @Date: 2021/2/9 13:09
* @Description: 简易计算器
*/
//启动类
public class TestCalc {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//计算器
class Calculator extends Frame{
TextField num1,num2,num3;
public void loadFrame(){
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
Button button = new Button("=");
Label label = new Label("+");
button.addActionListener(new MyCalculatorListener(this));
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
class MyCalculatorListener implements ActionListener{
//组合思想,直接将另一个类引用进来。
Calculator calculator = null;
public MyCalculatorListener(Calculator calculator) {
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.获取加数和被加数
int n1 = Integer.parseInt(calculator.num1.getText());
int n2 = Integer.parseInt(calculator.num2.getText());
//2.将结果放到第三个框
calculator.num3.setText("" + (n1+n2));
//3.清空前两个框
calculator.num1.setText("");
calculator.num2.setText("");
}
}
package com.wang.Lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @Auther: 王海新
* @Date: 2021/2/9 13:09
* @Description: 简易计算器
*/
//启动类
public class TestCalc {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//计算器
class Calculator extends Frame{
TextField num1,num2,num3;
public void loadFrame(){
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
Button button = new Button("=");
Label label = new Label("+");
button.addActionListener(new MyCalculatorListener());
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
//监听器类
//内部类最大的好处,就是畅通无阻的访问外部的属性和方法。
private class MyCalculatorListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//1.获取加数和被加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//2.将结果放到第三个框
num3.setText("" + (n1+n2));
//3.清空前两个框
num1.setText("");
num2.setText("");
}
}
}
画笔
package com.wang.Lesson03;
import java.awt.*;
/**
* @Auther: 王海新
* @Date: 2021/2/10 15:09
* @Description: 画笔
*/
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200,200,600,500);
setVisible(true);
}
//画笔
public void paint(Graphics g){
//画笔 需要有颜色,画笔可以画画
g.setColor(Color.red);
g.drawOval(100,100,100,100);
g.setColor(Color.blue);
g.fillOval(200,200,100,100);
g.setColor(Color.green);
g.fillRect(150,200,200,200);
//养成习惯,画笔用完,将他还原到最初的颜色
}
}
鼠标监听
package com.wang.Lesson03;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
/**
* @Auther: 王海新
* @Date: 2021/2/10 16:59
* @Description: 鼠标监听事件 画板
*/
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<>();
setVisible(true);
//鼠标监听器,正对这个窗口
this.addMouseListener(new MyMouseListener());
}
@Override
public void paint(Graphics g) {
//画画,监听鼠标的事件
Iterator iterator = points.iterator();
while (iterator.hasNext()){
Point point = (Point) iterator.next();
g.setColor(Color.BLUE);
g.fillOval(point.x,point.y,10,10);
}
}
public void addPaint(Point point){
points.add(point);
}
//适配器模式
private class MyMouseListener extends MouseAdapter{
//鼠标 按下,弹起,按住不放
@Override
public void mousePressed(MouseEvent e) {
MyFrame frame = (MyFrame) e.getSource();
//这个我们点击的时候,就会在界面上产生一个点!
//这个点就是鼠标的点
frame.addPaint(new Point(e.getX(),e.getY()));
//每次点击鼠标都需要重新画一遍
frame.repaint();//刷新
}
}
}
窗口监听
package com.wang.Lesson03;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* @Auther: 王海新
* @Date: 2021/2/11 11:33
* @Description: 窗口监听事件
*/
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
static class WindowFrame extends Frame{
public WindowFrame(){
setBackground(Color.BLUE);
setBounds(100,100,200,200);
setVisible(true);
this.addWindowListener(
// 匿名内部类
new WindowAdapter() {
//关闭窗口
@Override
public void windowClosing(WindowEvent e) {
System.out.println("你点击了X");
}
//激活窗口
@Override
public void windowActivated(WindowEvent e) {
WindowFrame source = (WindowFrame) e.getSource();
source.setTitle("被激活了");
System.out.println("WindowActivated");
}
}
);
}
}
}
键盘监听
package com.wang.Lesson03;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
/**
* @Auther: 王海新
* @Date: 2021/2/11 12:17
* @Description: 键
*/
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
static class KeyFrame extends Frame{
public KeyFrame(){
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
//获得键盘侠的键是哪一个,当前的码
int keyCode = e.getKeyCode();
System.out.println(keyCode);//不需要去记录这个数值,直接使用静态属性,VK_XXX
if (keyCode == KeyEvent.VK_UP) {
System.out.println("按下了上键");
}
}
});
}
}
}
Swing
窗口,面板
package com.wang.lesson04;
import javax.swing.*;
import java.awt.*;
/**
* @Auther: 王海新
* @Date: 2021/2/11 13:05
* @Description: Swing之JFrame窗体
*/
public class JFrameDemo02 {
public static void main(String[] args) {
new MyFrame2().init();
}
}
class MyFrame2 extends JFrame {
public void init() {
this.setBounds(10,10,200,300);
this.setVisible(true);
JLabel label = new JLabel("欢迎来到java");
this.add(label);
//让文本标签居中 设置水平对齐
label.setHorizontalAlignment(SwingConstants.CENTER);
//获得一个容器
Container container = this.getContentPane();
container.setBackground(Color.BLUE);
}
}
package com.wang.lesson04;
import javax.swing.*;
import java.awt.*;
/**
* @Auther: 王海新
* @Date: 2021/2/11 13:05
* @Description: Swing之JFrame窗体
*/
public class JFrameDemo02 {
public static void main(String[] args) {
new MyFrame2().init();
}
}
class MyFrame2 extends JFrame {
public void init() {
this.setBounds(10,10,200,300);
this.setVisible(true);
JLabel label = new JLabel("欢迎来到java");
this.add(label);
//让文本标签居中 设置水平对齐
label.setHorizontalAlignment(SwingConstants.CENTER);
//获得一个容器
Container container = this.getContentPane();
container.setBackground(Color.BLUE);
}
}
弹窗
JDialog 默认就有关闭事件,不需要我们重新再写。
package com.wang.lesson04;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @Auther: 王海新
* @Date: 2021/2/11 14:35
* @Description: JDialog弹窗
*/
//主窗口
public class DialogDemo extends JFrame {
public DialogDemo(){
this.setVisible(true);
this.setSize(700,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放东西,容器
Container contentPane = this.getContentPane();
//绝对布局
contentPane.setLayout(null);
//按钮
JButton button = new JButton("点击弹出一个对话框");
button.setBounds(30,30,200,50);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
});
contentPane.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 contentPane = this.getContentPane();
contentPane.setLayout(null);
contentPane.add(new Label("学Java"));
}
}
标签
label
new JLabel("xxx");
图标 ICON
package com.wang.lesson04;
import javax.swing.*;
import java.awt.*;
/**
* @Auther: 王海新
* @Date: 2021/2/11 15:10
* @Description: 图标 需要实现类,Frame继承
*/
public class IconDemo extends JFrame implements Icon {
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("contest", iconDemo, SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.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;
}
}
图片ICON
package com.wang.lesson04;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
/**
* @Auther: 王海新
* @Date: 2021/2/11 15:50
* @Description: imageIcon标签
*/
public class ImageIconDemo extends JFrame {
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 contentPane = getContentPane();
//将标签放到容器中
contentPane.add(label);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
JScroll
面板
package com.wang.lesson05;
import javax.swing.*;
import java.awt.*;
/**
* @Auther: 王海新
* @Date: 2021/2/11 16:24
* @Description: 面板:JPanel
*/
public class JPanelDemo extends JFrame {
public JPanelDemo() {
Container contentPane = this.getContentPane();
contentPane.setLayout(new GridLayout(2,1,10,10));//后面的参数的意思,间距,就是多个面板之间的间距
JPanel jPanel = new JPanel(new GridLayout(1, 3));
JPanel jPanel1 = new JPanel(new GridLayout(1, 2));
JPanel jPanel2 = new JPanel(new GridLayout(2, 1));
JPanel jPanel3 = new JPanel(new GridLayout(3, 2));
jPanel.add(new JButton("1"));
jPanel.add(new JButton("1"));
jPanel.add(new JButton("1"));
jPanel1.add(new JButton("2"));
jPanel1.add(new JButton("2"));
jPanel2.add(new JButton("3"));
jPanel2.add(new JButton("3"));
jPanel3.add(new JButton("4"));
jPanel3.add(new JButton("4"));
jPanel3.add(new JButton("4"));
jPanel3.add(new JButton("4"));
jPanel3.add(new JButton("4"));
jPanel3.add(new JButton("4"));
contentPane.add(jPanel);
contentPane.add(jPanel1);
contentPane.add(jPanel2);
contentPane.add(jPanel3);
this.setVisible(true);
this.setSize(500,500);
}
public static void main(String[] args) {
new JPanelDemo();
}
}
-
有滚动条的面板
package com.wang.lesson05;
import javax.swing.*;
import java.awt.*;
/**
* @Auther: 王海新
* @Date: 2021/2/11 17:08
* @Description:
*/
public class JScrollDemo extends JFrame {
public JScrollDemo() {
Container contentPane = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("感谢狂神说java");
//Scroll面板
JScrollPane jScrollPane = new JScrollPane(textArea);
contentPane.add(jScrollPane);
this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
按钮
package com.wang.lesson05;
import com.wang.lesson04.ImageIconDemo;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
/**
* @Auther: 王海新
* @Date: 2021/2/14 09:51
* @Description: 按钮
*/
public class JButtonDemo01 extends JFrame {
public JButtonDemo01() {
Container contentPane = this.getContentPane();
//将一个图片变为一个图标
URL url = JButtonDemo01.class.getResource("aa.jpg");
Icon icon = new ImageIcon(url);
//把这个图标放到按钮上
JButton jButton = new JButton();
jButton.setIcon(icon);
jButton.setToolTipText("图片按钮");
//add
contentPane.add(jButton);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
-
单选框
package com.wang.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
/**
* @Auther: 王海新
* @Date: 2021/2/14 11:31
* @Description:
*/
public class JButtonDemo02 extends JFrame {
public JButtonDemo02() {
Container contentPane = this.getContentPane();
//将一个图片变为一个图标
URL url = JButtonDemo01.class.getResource("aa.jpg");
Icon icon = new ImageIcon(url);
//单选框
JRadioButton jRadioButton1 = new JRadioButton("JRadioButton01");
JRadioButton jRadioButton2 = new JRadioButton("JRadioButton02");
JRadioButton jRadioButton3 = new JRadioButton("JRadioButton03");
//由于单选框只能选择一个,分组,一个组中只能选择一个
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
buttonGroup.add(jRadioButton3);
contentPane.add(jRadioButton1,BorderLayout.CENTER);
contentPane.add(jRadioButton2,BorderLayout.NORTH);
contentPane.add(jRadioButton3,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo02();
}
}
-
多选框
package com.wang.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
/**
* @Auther: 王海新
* @Date: 2021/2/14 11:31
* @Description:
*/
public class JButtonDemo03 extends JFrame {
public JButtonDemo03() {
Container contentPane = this.getContentPane();
//将一个图片变为一个图标
URL url = JButtonDemo01.class.getResource("aa.jpg");
Icon icon = new ImageIcon(url);
//多选框
JCheckBox jCheckBox01 = new JCheckBox("jCheckBox01");
JCheckBox jCheckBox02 = new JCheckBox("jCheckBox02");
contentPane.add(jCheckBox01,BorderLayout.NORTH);
contentPane.add(jCheckBox02,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo03();
}
}
列表
package com.wang.lesson06;
import javax.swing.*;
import java.awt.*;
/**
* @Auther: 王海新
* @Date: 2021/2/14 11:47
* @Description:
*/
public class TestComboboxDemo01 extends JFrame {
public TestComboboxDemo01(){
Container contentPane = this.getContentPane();
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在热映");
status.addItem("已下架");
status.addItem("即将上映");
contentPane.add(status);
this.setVisible(true);
this.setSize(500,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
TestComboboxDemo01 testComboboxDemo01 = new TestComboboxDemo01(); }
}
package com.wang.lesson06;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
/**
* @Auther: 王海新
* @Date: 2021/2/14 17:31
* @Description: 列表框
*/
public class TestComboboxDemo02 extends JFrame {
public TestComboboxDemo02(){
Container container = this.getContentPane();
/* //生成列表的内容
String[] contents = {"1","2","3"};*/
Vector contents = new Vector();
//列表中需要放入内容
JList jList = new JList(contents);
contents.add("zhangsan ");
contents.add("lisi ");
contents.add("wangwu ");
container.add(jList);
this.setVisible(true);
this.setSize(400,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo02();
}
}
- 应用场景
- 选择地区,或者一些单选框按钮
- 列表,展示信息,一般是动态扩容
文本框
-
文本框
package com.wang.lesson06;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
/**
* @Auther: 王海新
* @Date: 2021/2/15 15:50
* @Description: 文本框测试
*/
public class TestTextDemo01 extends JFrame {
public TestTextDemo01(){
Container container = this.getContentPane();
JTextField textField = new JTextField("hello");
JTextField textField2 = new JTextField("world",20);
container.add(textField,BorderLayout.SOUTH);
container.add(textField2,BorderLayout.NORTH);
this.setVisible(true);
this.setSize(400,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}
-
密码框
package com.wang.lesson06;
import javax.swing.*;
import java.awt.*;
/**
* @Auther: 王海新
* @Date: 2021/2/15 15:54
* @Description: 密码框
*/
public class TestTextDemo02 extends JFrame {
public TestTextDemo02(){
Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();// ***
passwordField.setEchoChar('*');
container.add(passwordField);
this.setVisible(true);
this.setSize(400,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo02();
}
}
-
文本框
package com.wang.lesson05;
import javax.swing.*;
import java.awt.*;
/**
* @Auther: 王海新
* @Date: 2021/2/11 17:08
* @Description: 滚动条 文本域
*/
public class JScrollDemo extends JFrame {
public JScrollDemo() {
Container contentPane = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("感谢狂神说java");
//Scroll面板
JScrollPane jScrollPane = new JScrollPane(textArea);
contentPane.add(jScrollPane);
this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}