GUI编程练习
告诉大家该怎么学? ●这是什么? ●它怎么玩? ●该如何去在我们平时运用? 组件 ●窗口 ●弹窗 ●面板 ●文本框 ●列表框 ●按钮
●图片
●监听事件 ●鼠标 ●键盘事件 ●破解工具
1、简介
Gui的核心技术: Swing AWT 1.因为界面不美观。 2.需要jre环境! 为什么我们要学习? 1.可以写出自己心中想要的一-些小工具 2.工作时候,也可能需要维护到swing界面,概率极小! 3.了解MVC架构,了解监听!
2、AWT
2.1、Awt介绍
1.包含了很多类和接口! GUI! 2.元素:窗口,按钮,文本框
3.java.awt
2.2、组件和容器
1、Frame 弹窗
package com.shao.gui;
import java.awt.*;
//GUI的第一个界面
public class TestFrame {
public static void main(String[] args) {
//Frame,JDK,看源码
Frame frame = new Frame("我的第一次学习JAVA图像界面窗口");
//需要设置可见性 w h
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色,IDEA中,颜色可以拖动选择
frame.setBackground( new Color(82, 155, 60));
//弹出的初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}
窗口此时无法关闭,停止java程序
尝试回顾封装:
package com.shao.gui;
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
//展示多个窗口
MyFrame myFrame1 = new MyFrame(100,100,200,200,Color.blue);
MyFrame myFrame2 = new MyFrame(100,300,200,200,Color.red);
MyFrame myFrame3 = new MyFrame(300,100,200,200,Color.pink);
MyFrame myFrame4 = new MyFrame(300,300,200,200,Color.green);
}
}
class MyFrame extends Frame{
static int id = 0;//可能存在多个窗口,所以我们需要一个计数器
//构造器
public MyFrame(int x,int y,int w,int h,Color color){
super("MyFrame"+(++id));//继承父类
setBackground(color);
setBounds(x,y,w,h);//直接设置坐标和长宽高
setVisible(true);//设置可见性
}
}
2、Panel 面板 ,内嵌在frame上
package com.shao.gui;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
//Panek 可以看作是一个空间,但是不能单独存在
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame();
//布局的概念
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//坐标
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(89, 191, 43));
panel.setBounds(100,100,300,300);
panel.setBackground(new Color(191, 107, 55));
frame.add(panel);
frame.setVisible(true);
//监听时间,监听窗口关闭事件,System.exit(0)
//适配器模式
frame.addWindowListener(new WindowAdapter() { //WindowAdapter 继承 WindowsListenner 接口
//窗口点击关闭时要做的事
2.3、布局管理器
●流式布局
package com.shao.gui;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame();
Button button1 = new Button("table1");
Button button2 = new Button("table2");
Button button3 = new Button("table3");
//设置为流式布局
frame.setLayout(new FlowLayout(FlowLayout.CENTER));//CENTER 中心,可以选择其他
frame.setSize(200,200);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
●东西南北中
package com.shao.gui;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestFlowLayout2 {
public static void main(String[] args) {
Button button1 = new Button("east");
Button button2 = new Button("west");
Button button3 = new Button("south");
Button button4 = new Button("nouth");
Button button5 = new Button("center");
Frame frame = new Frame();
frame.add(button1,BorderLayout.EAST);
frame.add(button2,BorderLayout.WEST);
frame.add(button3,BorderLayout.SOUTH);
frame.add(button4,BorderLayout.NORTH);
frame.add(button5,BorderLayout.CENTER);
frame.setSize(400,400);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
●表格
package com.shao.gui;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestGridLayout {
public static void main(String[] args) {
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 frame = new Frame("TestGridLayout");
frame.setLayout(new GridLayout(3,2,10,20));//参数是,行,列,行间距,列间距,四个参数
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.pack();//java函数,用来自动布局,用最优布局
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
综合应用
代码实现
package com.shao.gui;
import com.shao.duixiang.Person;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestLesson1 {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setSize(400,400);
frame.setBackground(new Color(46, 125, 223));
frame.setLocation(300,300);
frame.setVisible(true);
frame.setLayout(new GridLayout(2,1));
//4个面板
Panel panel1 = new Panel(new BorderLayout());
Panel panel2 = new Panel(new GridLayout(1,1));
Panel panel3 = new Panel(new GridLayout(2,2));
Panel panel4 = new Panel(new BorderLayout());
//上面
panel1.add(new Button("Easrt-1"),BorderLayout.EAST);
panel1.add(new Button("West-1"),BorderLayout.WEST);
panel2.add(new Button("p2-btn-1"));
panel2.add(new Button("p2-btn-2"));
panel1.add(panel2,BorderLayout.CENTER);
//下面
panel4.add(new Button("East-2"),BorderLayout.EAST);
panel4.add(new Button("East-2"),BorderLayout.WEST);
//下面中间四个
for (int i = 0; i <4 ; i++) {
panel3.add(new Button("square"+i));
}
panel4.add(panel3,BorderLayout.CENTER);
frame.add(panel1);
frame.add(panel4);
frame.addWindowListener(new WindowAdapter() {
总结:
-
Frame是一个顶级窗口
-
Panel无法单独显示,必须添加到某个容器中。 3.布局管理器 1.流式 2.东西南北中 3.表格 4.大小,定位,背景颜色,可见性,监听!
2.4、事件监听
事件监听:当某个事情发生的时候,干什么?
package com.shao.gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestLesson2 {
public static void main(String[] args) {
//按下按钮,触发一些事件
Frame frame = new Frame();
Button button = new Button("start");
//因为。addActionListener()需要一个ActionListener,所以我们需要构造一个Actionalistener
MyActionLintener myActionLintener = new MyActionLintener();
button.addActionListener(myActionLintener);
frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
winderClose(frame);
}
//创建一个关闭的方法
private static void winderClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
多个按钮共享一个事件
//多个按钮实现同一个监听类
public class TesLesson3 {
public static void main(String[] args) {
Frame frame = new Frame();
Button button1 = new Button("start");
Button button2 = new Button("stop");
//对button1的值进行设置,默认为按键的label
button1.setActionCommand("one");
// button2.setActionCommand("two");
button1.addActionListener(new MyMonitor());
button2.addActionListener(new MyMonitor());
frame.add(button1,BorderLayout.CENTER);
frame.add(button2,BorderLayout.WEST);
frame.setSize(200,200);
frame.setVisible(true);
}
}
class MyMonitor implements ActionListener{
2.5、输入框 TextField监听
实现:输出的是*,输出空格清空文本框
package com.shao.gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextText1 {
public static void main(String[] args) {
MyFrame2 myFrame2 = new MyFrame2();
}
}
class MyFrame2 extends Frame{
public MyFrame2() {
TextField textField = new TextField();
add(textField);
//监听这个文本框输入的文字
MyAcctionListener myAcctionListener = new MyAcctionListener();
textField.addActionListener(myAcctionListener);
textField.setEchoChar('*'); //设置替换编码
setVisible(true);
pack();
}
}
class MyAcctionListener implements ActionListener{
2.6 、简易计算器,组合+内部类回顾复习!
实现代码:
package com.shao.gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalculate1 {
public static void main(String[] args) {
new Calculate();
}
}
class Calculate extends Frame{
public Calculate(){
//3个文本框
TextField num1 = new TextField(10);//字符数
TextField num2 = new TextField( 10);//字符数
TextField num3 = new TextField( 20);//字符数
//1个按钮
Button button = new Button( "=");
//1个标签
Label label = new Label( "+");
//布局
setLayout(new FlowLayout());
add( num1);
add(label);
add(num2);
add(button);
add( num3);
button.addActionListener(new MyAcctionListener2(num1,num2,num3));
pack();
setVisible(true);
}
}
class MyAcctionListener2 implements ActionListener{
private TextField num1,num2,num3;
public MyAcctionListener2(TextField num1, TextField num2, TextField num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
代码优化
package com.shao.gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalculate1 {
public static void main(String[] args) {
new Calculate().loadFrame();
}
}
//计算器类
class Calculate 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 MyAcctionListener2());
//布局
setLayout(new FlowLayout());
add( num1);
add(label);
add( num2);
add( button);
add( num3);
pack();
setVisible(true);
}
//监听器类
private class MyAcctionListener2 implements ActionListener{
2.7画笔
package com.shao.gui;
import java.awt.*;
public class TestPaint1 {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200,300,500,600);
setVisible(true);
}
2.8、鼠标监听
package com.shao.gui;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
public class TestMouseListener1 {
public static void main(String[] args) {
new MyFrame4("画图");
}
}
class MyFrame4 extends Frame{
//画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
ArrayList points;
public MyFrame4(String title) {
super(title);
setBounds( 200, 200, 400, 300) ;
//存鼠标点击的点
points = new ArrayList<>();
setVisible(true);
//鼠标监听器,正对这个窗口
this.addMouseListener( new MyMouseListener1());
}
2.9、窗口监听
package com.shao.gui;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow1 {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBackground(Color.red);
setVisible(true);
setBounds(100,100,200,200);
//匿名内部类
this.addWindowListener(new WindowAdapter() {
//关闭窗口
2.10、键盘监听
package com.shao.gui;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKListener1 {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setBackground(Color.red);
setVisible(true);
setBounds(2,2,300,400);
this.addKeyListener(new KeyAdapter() {
//键盘按下
3、Swing
3.1、窗口,面板
package com.shao.gui;
import javax.swing.*;
import java.awt.*;
public class TestJFrame1 {
//init(); 初始化
public void init(){
//顶级窗口
JFrame jf = new JFrame("这是一个新的JFrame窗口");
jf.setVisible(true);
jf.setBackground(Color.BLUE);// 此处代码无用,无法设置颜色,因为JFrame是顶级窗口,是设置在容器上***************
jf.setBounds(200,200,200,200);
JLabel jLabel = new JLabel("我在学习GUI编程");
jf.add(jLabel);
//标签居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
//容器实例化
Container contentPane = jf.getContentPane();
contentPane.setBackground(Color.RED);
//JFrame关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new TestJFrame1().init();
}
}
3.2、弹窗
JDialog,用来被弹出,默认就有关闭事件
package com.shao.gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestDialog1 extends JFrame {
public TestDialog1(){
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() {
3.3、标签
label
new JLabel("xxx");
图标ICON Icon是Swing包下的一个接口
package com.shao.gui;
import javax.swing.*;
import java.awt.*;
public class TestIcon1 extends JFrame implements Icon {
private int width;
private int height;
public void init(){
TestIcon1 testIcon1 = new TestIcon1(15,15);//此处传参,
JLabel label = new JLabel("ivontest", testIcon1, SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestIcon1().init();
}
public TestIcon1() { }//无参构造
public TestIcon1(int width,int height) {
this.width = width;
this.height = height;
}
package com.shao.gui;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class TestImageIcon extends JFrame {
public TestImageIcon(){
JLabel label = new JLabel("ImageIcon");
URL url = TestImageIcon.class.getResource("zhuomian.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,200,400,500);
}
public static void main(String[] args) {
new TestImageIcon();
}
}
3.4、面板\文本域
面板
package com.shao.gui;
import javax.swing.*;
import java.awt.*;
public class TestJpanel extends JFrame {
public TestJpanel(){
Container container = this.getContentPane();
container . setLayout(new GridLayout( 2, 1, 10,10)); //后面的参数的意思,间距
JPanel panel1 = new JPanel(new GridLayout( 1, 3));
panel1. add(new JButton( "1"));
panel1. add(new JButton( "1"));
panel1. add(new JButton( "1"));
container . add(panel1);
this . setVisible(true);
this . setSize(500, 500);
this . setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJpanel();
}
}
文本域
package com.shao.gui;
import javax.swing.*;
import java.awt.*;
public class TestJScroll extends JFrame {
public TestJScroll(){
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20,50);//每行20字,50行
textArea.setText("加油加油,乘风破浪");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);//滚动面板
container.add(scrollPane);
setVisible(true);
setBounds(100,100,200,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJScroll();
}
}
3.5、按钮
●图片按钮
package com.shao.gui;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class TesJButton1 extends JFrame {
public TesJButton1(){
Container container = this.getContentPane();
//将-一个图片变为图标
URL resource = TesJButton1. class . getResource( "zhuomian.jpg");
Icon icon = new ImageIcon( resource);
//把这个图标放在按钮上
JButton button = new JButton();
button. setIcon(icon);
button. setToolTipText("图片按钮");
//add
container . add(button);
this . setVisible(true);
this. setSize(500, 300);
this . setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TesJButton1();
}
}
●单选按钮
package com.shao.gui;
import javax.swing.*;
import java.awt.*;
public class TestJButton2 extends JFrame {
public TestJButton2(){
Container container = this.getContentPane();
//单选框
JRadioButton jRadioButton1 = new JRadioButton("JRadioButton1");
JRadioButton jRadioButton2 = new JRadioButton("JRadioButton2");
JRadioButton jRadioButton3 = new JRadioButton("JRadioButton3");
//由于单选框只能选择一个分组,一个组中只能选择一个,如果去掉分组,就会类似多选
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
buttonGroup.add(jRadioButton3);
container.add(jRadioButton1,BorderLayout.NORTH);
container.add(jRadioButton2,BorderLayout.CENTER);
container.add(jRadioButton3,BorderLayout.SOUTH);
this . setVisible(true);
this. setSize(500, 300);
this . setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJButton2();
}
}
●复选按钮
package com.shao.gui; import javax.swing.*; import java.awt.*; public class TestJButton3 extends JFrame { public TestJButton3(){ Container container = this.getContentPane(); //多选框 JCheckBox jCheckBox1 = new JCheckBox("checkbox1"); JCheckBox jCheckBox2 = new JCheckBox("checkbox2"); JCheckBox jCheckBox3 = new JCheckBox("checkbox3"); container.add(jCheckBox1,BorderLayout.NORTH); container.add(jCheckBox2,BorderLayout.CENTER); container.add(jCheckBox3,BorderLayout.SOUTH); this . setVisible(true); this. setSize(500, 300); this . setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestJButton3(); } }
3.6列表
●下拉框
package com.shao.gui;
import javax.swing.*;
import java.awt.*;
public class TestCombobox1 extends JFrame {
public TestCombobox1(){
Container container = this.getContentPane();
JPanel jPanel = new JPanel();
jPanel.setBounds(100,100,100,20);
JComboBox jComboBox = new JComboBox();
jComboBox.addItem("");
jComboBox.addItem("星期一");
jComboBox.addItem("星期二");
jComboBox.addItem("星期三");
jPanel.add(jComboBox);
container.add(jPanel);
this.setVisible(true);
this.setBounds(100,100,400,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestCombobox1();
}
}
●列表框
package com.shao.gui;
import javax.swing.*;
import java.awt.*;
public class TestCombobox2 extends JFrame {
public TestCombobox2(){
Container container = this.getContentPane();
JPanel jPanel = new JPanel();
jPanel.setBounds(100,100,100,20);
//生成列表的内容
String[] contents = {"1","2","3"};
JList jList = new JList(contents);
jPanel.add(jList);
container.add(jPanel);
this.setVisible(true);
this.setBounds(100,100,400,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestCombobox2();
}
}
3.7文本框
●文本框
package com.shao.gui;
import javax.swing.*;
import java.awt.*;
public class TestText1 extends JFrame {
public TestText1(){
Container container = this.getContentPane();
JTextField text1 = new JTextField("hello");
JTextField text2 = new JTextField("hello");
JTextField text3 = new JTextField("hello");
container.add(text1,BorderLayout.NORTH);
container.add(text2,BorderLayout.CENTER);
container.add(text3,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,100,400,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestText1();
}
}
●密码框
package com.shao.gui;
import javax.swing.*;
import java.awt.*;
public class TestText1 extends JFrame {
public TestText1(){
Container container = this.getContentPane();
JPasswordField jPasswordField = new JPasswordField();
container.add(jPasswordField);//默认是黑色圆点
jPasswordField.setEchoChar('*');//换成星号替换
this.setVisible(true);
this.setBounds(100,100,400,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestText1();
}
}
●文本域
package com.shao.gui;
import javax.swing.*;
import java.awt.*;
public class TestJScroll extends JFrame {
public TestJScroll(){
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20,50);//每行20字,50行
textArea.setText("加油加油,乘风破浪");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);//滚动面板
container.add(scrollPane);
setVisible(true);
setBounds(100,100,200,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJScroll();
}
}
4、贪吃蛇项目练习
加油! 加油!