GUI编程
核心技术
AWT
画笔
package com.tmy.lesson03;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPaint01 {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(500,300,600,450);
//setBackground(new Color(247, 246, 235));
//setResizable(true);
setVisible(true);
//监听事件,关闭窗口
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
//画笔
@Override
public void paint(Graphics g) {
g.setColor(Color.red);
g.drawOval(100, 100, 100, 100);//空心圆
g.fillOval(200,200,100,100);//实心圆
//画笔用完,将他还原到最初颜色
g.setColor(Color.black);
}
}
结果:
鼠标监听(模拟画图工具)
package com.tmy.lesson03;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;
public class TestMouseListener {
public static void main(String[] args) {
new MyMouseListener("画图");
}
}
class MyMouseListener extends Frame{
//需要画笔,需要监听鼠标当前位置,需要一个集合来存储这个点
ArrayList points = null;
public MyMouseListener(String title){
super(title);
setBounds(500,300,600,400);
setBackground(Color.white);
setVisible(true);
//存鼠标的点
points = new ArrayList();
//鼠标监听事件
addMouseListener(new MyMouse());
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void paint(Graphics 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);
}
}
//添加一个点到ArrayList集合的方法
public void addPaint(Point point){
points.add(point);
}
//适配器模式(不需要重写MouseListener的所有方法,只重写需要的)
private class MyMouse extends MouseAdapter{
//鼠标按下
@Override
public void mousePressed(MouseEvent e) {
MyMouseListener frame = (MyMouseListener)e.getSource();
//我们点击的时候,就会在界面上产生一个点
//这个点就是鼠标的点
frame.addPaint(new Point(e.getX(),e.getY()));//添加点到ArrayList集合
//每次点击都会重新画一遍
frame.repaint();//类似于刷新
}
}
}
结果:
窗口监听
package com.tmy.lesson03;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBounds(500,300,400,300);
setBackground(Color.RED);
setVisible(true);
addWindowListener(new WindowAdapter(){ //匿名内部类
//窗口关闭
// @Override
public void windowClosing(WindowEvent e) {
System.out.println("窗口关闭");
//System.exit(0);
}
//窗口激活
@Override
public void windowActivated(WindowEvent e) {
System.out.println("窗口被激活");
}
});
}
//内部类的方法
/* class MyWindow extends WindowAdapter{
@Override
public void windowClosing(WindowEvent e) {
System.out.println("窗口关闭");
System.exit(0);
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("窗口被激活");
}
}*/
}
结果:
键盘监听
package com.tmy.lesson03;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestKeyListener {
public static void main(String[] args) {
new MyKey();
}
}
class MyKey extends Frame{
public MyKey(){
setBackground(Color.RED);
setBounds(500,300,400,200);
setVisible(true);
addKeyListener(new KeyAdapter() {
//按下键盘按键
@Override
public void keyPressed(KeyEvent e) {
//获得你当前按下的按键对应的码
int keyCode = e.getKeyCode();//按键对应的码值不需要去记忆,只需要使用静态属性 KeyEvent.VK_xxx
char keyChar = e.getKeyChar();
System.out.println(keyChar + "说:你特么别按我知道不???");
if(keyCode==KeyEvent.VK_W){
System.out.println("你按w干嘛?");
}
}
});
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
结果:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eVSi4Xiq-1592671903585)(C:\Users\TMY\AppData\Roaming\Typora\typora-user-images\image-20200620114904803.png)]
Swing
JFrame窗体
package com.tmy.lesson04;
import javax.swing.*;
import java.awt.*;
public class TestJFrame01 {
public static void main(String[] args) {
//新建一个窗口
new TestJFrame01().init();
}
//init();初始化
public void init(){
JFrame jFrame = new JFrame("这是一个JFrame窗口");
jFrame.setBounds(500,300,500,300);
jFrame.setBackground(new Color(201, 55, 58));、//占时不知这里设置有什么,测试结果这里控制不了背景的颜色
jFrame.setVisible(true);
//设置文字
JLabel label = new JLabel();
label.setText("Hello World!!");
jFrame.add(label);
//设置标签居中
label.setHorizontalAlignment(SwingConstants.CENTER);
//容器实例化,显示背景颜色
Container contentPane = jFrame.getContentPane();
contentPane.setBackground(new Color(201, 55, 58));
//关闭事件
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
结果:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OZndfTGd-1592671903588)(C:\Users\TMY\AppData\Roaming\Typora\typora-user-images\image-20200620150318319.png)]
弹窗
package com.tmy.lesson04;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//JDialog,用来被弹出,默认就有关闭事件
public class TestDialog {
public static void main(String[] args) {
new MyTestDialog();
}
}
//创建计窗口类
class MyTestDialog extends JFrame{
public MyTestDialog(){
setTitle("学习弹窗");
setBounds(500,300,600,300);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//创建按钮
JButton jButton = new JButton("点击有惊喜");
//添加按钮,首先得或得容器
Container contentPane = getContentPane();
//布局。绝对布局
jButton.setBounds(200,100,200,60);
contentPane.setLayout(null);//绝对布局,按照添加元素自己设定的参数布局
//添加进窗口
contentPane.add(jButton);
//添加事件监听
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new MyDialog();
}
});
}
}
//创建弹窗,并设置弹窗
class MyDialog extends JDialog{
public MyDialog() {
this.setVisible(true);
this.setBounds(100,100,300,200);
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//自带不需要再次设置
Container contentPane = this.getContentPane();
contentPane.setLayout(null);
JButton jButton = new JButton("草你大爷");
contentPane.setLayout(null);
jButton.setBounds(100,50,100,50);
contentPane.add(jButton);
}
}
结果:
标签
- 普通标签
package com.tmy.lesson04;
import javax.swing.*;
import java.awt.*;
//图标需要实现类,JFrame继承
public class TestIcon extends JFrame implements Icon {
private int height;
private int width;
public TestIcon() {
}
public TestIcon(int height,int width) {
this.height = height;
this.width = width;
}
public void init(){
TestIcon icon = new TestIcon(15, 15);
//图标可以放到标签上,也可以防止按钮上
JLabel label = new JLabel("Icon",icon,SwingConstants.CENTER);
//JButton jButton = new JButton(icon);//可以防止按钮上
Container contentPane = this.getContentPane();
contentPane.add(label);
contentPane.setBackground(new Color(29, 199, 46));
setBounds(500,300,400,200);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestIcon().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;
}
}
结果:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Xq1OB7rW-1592671903590)(C:\Users\TMY\AppData\Roaming\Typora\typora-user-images\image-20200620171141942.png)]
- 图片标签
package com.tmy.lesson04;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
//将图片作为图标加到窗口
public class TestImageIcon extends JFrame {
public TestImageIcon() {
JLabel jLabel = new JLabel("ImageIcon");
JButton jButton = new JButton("百度翻译");
//获取图片地址
URL url = TestImageIcon.class.getResource("baidu.jpg");//获取TestImageIcon这个类的同级资源
//System.out.println(url);
//获取图片路径的方法(之前反射机制的方法)
URL url1 = Thread.currentThread().getContextClassLoader().getResource("com/tmy/lesson04/baidu.jpg");
//System.out.println(url1);
//创建图片标签,将地址传到图片标签中
ImageIcon imageIcon = new ImageIcon(url);
//将图标加到按钮上
jButton.setIcon(imageIcon);
//将图标加到标签上
jLabel.setIcon(imageIcon);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);//设置水平居中
//获得窗口的容器
Container contentPane = getContentPane();
//将按钮加到窗口的容器中
contentPane.add(jButton);
//将标签加到窗口的容器中
//contentPane.add(jButton);
contentPane.setBackground(new Color(172, 153, 60));
setBounds(500,300,500,300);
setVisible(true);
//设置监听关闭
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestImageIcon();
}
}
加载到的标签结果:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-V1YbFFk9-1592671903592)(C:\Users\TMY\AppData\Roaming\Typora\typora-user-images\image-20200620181902502.png)]
加载到的按钮结果:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vD36AyGH-1592671903593)(C:\Users\TMY\AppData\Roaming\Typora\typora-user-images\image-20200620182629973.png)]
面板
package com.tmy.lesson04;
import javax.swing.*;
import java.awt.*;
public class TestJPanel extends JFrame {
public TestJPanel() {
setLayout(new GridLayout(2,2,10,10));//后面的10表示两个添加物的间距
setBounds(500,300,600,400);
Container contentPane = getContentPane();
JPanel jPanel1 = new JPanel(new GridLayout(2,3));//表格布局
JPanel jPanel2 = new JPanel(new FlowLayout());//流式布局
JPanel jPanel3 = new JPanel();
jPanel3.setLayout(null);//绝对布局
JPanel jPanel4= new JPanel(new BorderLayout());//东南西北中布局
for (int i = 0; i < 6; i++) {
jPanel1.add(new JButton(""+i));
}
for (int i = 0; i < 8; i++) {
jPanel2.add(new JButton(""+i));
}
jPanel3.setBackground(Color.RED);
JButton jButton0 = new JButton("0");
jButton0.setBounds(60,70,70,40);
JButton jButton1 = new JButton("1");
jButton1.setBounds(150,70,70,40);
jPanel3.add(jButton0);
jPanel3.add(jButton1);
jPanel4.add(new JButton("上"), BorderLayout.SOUTH);
jPanel4.add(new JButton("下"), BorderLayout.NORTH);
jPanel4.add(new JButton("右"), BorderLayout.EAST);
jPanel4.add(new JButton("左"), BorderLayout.WEST);
jPanel4.add(new JButton("中"), BorderLayout.CENTER);
contentPane.add(jPanel1);
contentPane.add(jPanel2);
contentPane.add(jPanel3);
contentPane.add(jPanel4);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new TestJPanel();
}
}
结果:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vjXBRonX-1592671903595)(C:\Users\TMY\AppData\Roaming\Typora\typora-user-images\image-20200620225520514.png)]
Scroll面板(滚动面板)
package com.tmy.lesson04;
import javax.swing.*;
import java.awt.*;
//滚动面板
public class TestJScrollPanel extends JFrame {
public TestJScrollPanel() {
setBounds(500,300,600,400);
setResizable(false);//窗口不可改变大小
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//获得内容窗格
Container contentPane = getContentPane();
// 文本域 创建一个可以换行的文本域
JTextArea textArea = new JTextArea(200,500);//设置文本域的长宽
textArea.setText("速度快点啊");
//创建滚动窗格 Scroll面板
JScrollPane jScrollPane = new JScrollPane(textArea);//创建滚动窗格,并且将文本域添加到里面
//将滚动窗格添加到内容窗格(窗口)中
contentPane.add(jScrollPane);
setVisible(true);
}
public static void main(String[] args) {
new TestJScrollPanel();
}
}
结果:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4LQgJ4uw-1592671903597)(C:\Users\TMY\AppData\Roaming\Typora\typora-user-images\image-20200620233004738.png)]
按钮
图片按钮
package com.tmy.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class TestJButton extends JFrame {
public TestJButton() {
Container contentPane = getContentPane();
//两种获得路径的方法
URL url1 = TestJButton.class.getResource("baidu.jpg");//只能获得当前类路径同级的文件
URL url2 = Thread.currentThread().getContextClassLoader().getResource("com/tmy/lesson05/baidu.jpg");
System.out.println(url1);
System.out.println(url2);
//创建图片图标
ImageIcon imageIcon = new ImageIcon(url1);
//将图标加到按钮上
JButton jButton = new JButton(imageIcon);
//将按钮加载带内容面板上
contentPane.add(jButton);
setBounds(500,300,500,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new TestJButton();
}
}
结果:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-D7Sis4RZ-1592671903600)(C:\Users\TMY\AppData\Roaming\Typora\typora-user-images\image-20200621002132332.png)]
单选按钮
package com.tmy.lesson05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class TestJButton02 extends JFrame {
public TestJButton02() {
Container contentPane = getContentPane();
//单选按钮
JRadioButton radioButton1 = new JRadioButton("男");
JRadioButton radioButton2 = new JRadioButton("女");
JRadioButton radioButton3 = new JRadioButton("人妖");
JRadioButton radioButton4 = new JRadioButton("妖人");
//这里的按钮其实是可以多选的,但通过分组实现了单选,一个组里只能选一个,不在组里可继续选
//由于单选框只能选择一个,所有要分组,因为一个组里只能选择一个
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(radioButton1);
buttonGroup.add(radioButton2);
buttonGroup.add(radioButton3);
//将按钮加载带内容面板上
contentPane.setLayout(new FlowLayout());
contentPane.add(radioButton1);
contentPane.add(radioButton2);
contentPane.add(radioButton3);
contentPane.add(radioButton4);
setBounds(500,300,500,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new TestJButton02();
}
}
结果:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lvYFSksD-1592671903601)(C:\Users\TMY\AppData\Roaming\Typora\typora-user-images\image-20200621003735559.png)]
复选按钮
package com.tmy.lesson05;
import javax.swing.*;
import java.awt.*;
public class TestJButton03 extends JFrame {
public TestJButton03() {
Container contentPane = getContentPane();
//复选按钮
JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");
JCheckBox jCheckBox3 = new JCheckBox("jCheckBox3");
JCheckBox jCheckBox4 = new JCheckBox("jCheckBox4");
//将按钮加载带内容面板上
contentPane.setLayout(new FlowLayout());
contentPane.add(jCheckBox1);
contentPane.add(jCheckBox2);
contentPane.add(jCheckBox3);
contentPane.add(jCheckBox4);
setBounds(500,300,500,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new TestJButton03();
}
}
结果:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nqCur0yS-1592671903603)(C:\Users\TMY\AppData\Roaming\Typora\typora-user-images\image-20200621004423171.png)]
x1");
JCheckBox jCheckBox2 = new JCheckBox(“jCheckBox2”);
JCheckBox jCheckBox3 = new JCheckBox(“jCheckBox3”);
JCheckBox jCheckBox4 = new JCheckBox(“jCheckBox4”);
//将按钮加载带内容面板上
contentPane.setLayout(new FlowLayout());
contentPane.add(jCheckBox1);
contentPane.add(jCheckBox2);
contentPane.add(jCheckBox3);
contentPane.add(jCheckBox4);
setBounds(500,300,500,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new TestJButton03();
}
}
结果:[外链图片转存中...(img-nqCur0yS-1592671903603)]