文章目录
GUI
核心:Swing AWT
缺点:1.界面不美观2.需要jre环境
2.AWT
2.1Awt介绍
1.包含很多类和接口!GUI
2.元素:窗口 按钮
2.2组件和容器
frame
package gui;
import java.awt.*;
public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("这是我的第一个Java图像接口设置");
//需要设置可见性 w h
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色color
frame.setBackground(new Color(0, 253, 223));
//弹出初始位置
frame.setLocation(200,200);
//设置大小不可变
frame.setResizable(false);
}
}
缺点:不可关闭 解决:停止java运行
package gui.lesson01;
import java.awt.*;
public class TestFrame02 {
public static void main(String[] args) {
MyFrame myFrame1 = new MyFrame(Color.blue, 100, 100, 200, 200);
MyFrame myFrame2 = new MyFrame(Color.yellow, 300, 100, 200, 200);
MyFrame myFrame3 = new MyFrame(Color.red, 100, 300, 200, 200);
MyFrame myFrame4 = new MyFrame(Color.orange, 300, 300, 200, 200);
}
}
class MyFrame extends Frame {
static int id=0;//可能存在多个窗口,我们需要一个计数器
public MyFrame(Color color,int x,int y,int w,int h){
super("Myframe+"+(++id));
setBackground(color);
setBounds(x,y,w,h);
setVisible(true);
}
}
panel
package gui.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//panel 面板可以看作一个空间,但不能单独存在
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame();
//布局的概念
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//坐标
frame.setBackground(new Color(0xFFB700));
frame.setBounds(300,400,500,600);
//panel 坐标 相对于frame
panel.setBackground(Color.BLUE);
panel.setBounds(100,200,300,300);
//frame.add(panel)
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭事件 system.exit(0)关闭
//适配器模式:adapted
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
可以关闭
布局管理器
- 流式布局
- 东西南北中
- 表格布局
package gui.lesson01;
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
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button2");
//设置流式布局
//frame.setLayout(new FlowLayout(FlowLayout.LEFT));//靠左
//frame.setLayout(new FlowLayout(FlowLayout.RIGHT));//靠右
frame.setLayout(new FlowLayout());//居中
frame.setSize(200,200);
//添加按钮
frame.add(button1);
frame.add(button2);
frame.add(button3);
//frame可视化
frame.setVisible(true);
//frame关闭
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}
东西南北中
package gui.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame();
Button earth = new Button("earth");
Button west = new Button("west");
Button south = new Button("south");
Button north= new Button("north");
Button center = new Button("center");
frame.add(earth,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);
//监听
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}
表格grid
package gui.lesson01;
import java.awt.*;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestGridLayout");
Button but1 = new Button("but1");
Button but2 = new Button("but2");
Button but3 = new Button("but3");
Button but4 = new Button("but4");
Button but5 = new Button("but5");
Button but6 = new Button("but6");
frame.setLayout(new GridLayout(3,2));
frame.add(but1);
frame.add(but2);
frame.add(but3);
frame.add(but4);
frame.add(but5);
frame.add(but6);
frame.pack();//java函数 寻找适合布局
frame.setVisible(true);
}
}
练习题
构思:1个 frame 2.4个面板 border 左button 中panel 右button
package gui.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestLesson {
public static void main(String[] args) {
Frame frame = new Frame("课堂练习" );
frame.setSize(100,200);
frame.setLocation(100,100);
frame.setBackground(new Color(0, 253, 149));
frame.setLayout(new GridLayout(2,3));
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout(2,1));
Panel p3 = new Panel(new BorderLayout());
Panel p4 = new Panel(new GridLayout(2,2));
p1.add(new Button("EAST-1"),BorderLayout.EAST);
p1.add(new Button("WEST-1"),BorderLayout.WEST);
p2.add(new Button("p2-btn-1"));
p2.add(new Button("p2-btn-2"));
p1.add(p2,BorderLayout.CENTER);
p3.add(new Button("EAST-2"),BorderLayout.EAST);
p3.add(new Button("WEST-2"),BorderLayout.WEST);
p4.add(new Button("p4-btn-1"));
p4.add(new Button("p4-btn-2"));
p4.add(new Button("p4-btn-3"));
p4.add(new Button("p4-btn-4"));
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
Button button3 = new Button("button3");
Button button4 = new Button("button4");
Button button5 = new Button("button5");
Button button6 = new Button("button6");
Button button7 = new Button("button7");
Button button8 = new Button("button8");
Button button9 = new Button("button9");
Button button10 = new Button("button10");
frame.pack();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}
总结:1.frame是一个顶级窗口
2.panel 无法单独显示,必须添加到某个容器
3.布局管理器
1.流式
2.东西南北中
3.表格
4.大小、定位、背景颜色、可见性,监听!
2.3.事件监听
事件监听:当某件事情发生的时候,干什么?
package gui.lesson01;
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 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();
windowClose(frame);
frame.setVisible(true);
}
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
}
多个按钮共享一个事件
package gui.lesson01;
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 TestActionEvent2 {
public static void main(String[] args) {
//两个按钮实现同一个监听
//开始 停止
Frame frame = new Frame("开始-停止");
Button btn1 = new Button("start");
Button btn2 = new Button("stop");;
//可以显示的定义触发会返回的命令,如果不显式定义,则会走默认的值
//可以多个按钮只写一个监听器
btn1.setActionCommand("btn1-start");
Mymonitor mymonitor = new Mymonitor();
btn1.addActionListener(mymonitor);
btn2.addActionListener(mymonitor);
frame.add(btn1,BorderLayout.EAST);
frame.add(btn2,BorderLayout.WEST);
close(frame);
frame.pack();
frame.setVisible(true);
}
public static void close(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class Mymonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//getActionCommand()获得按钮信息
System.out.println("按钮被执行了:msg=>"+e.getActionCommand());
}
}
2.4输入框
package gui.lesson01;
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 TestText01 {
public static void main(String[] args) {
//启动!
new MyFrame1();
}
}
class MyFrame1 extends Frame{
public MyFrame1() {
TextField textField = new TextField();
add(textField);
//监听文本框输入的文字
//按下回车 就会触发这个输入框的事件
textField.addActionListener(new MyActionListener2());
//设置替换编码
textField.setEchoChar('*');
setVisible(true);
pack();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field=(TextField)e.getSource();//获得一些资源
System.out.println(field.getText());//获得输入框的文本
field.setText("");//null
}
}
2.5简易计算器 组合+内部类回顾
oop原则:组合>继承
class A extends B{}//继承
class A {
public B b;
}//组合
package gui.lesson01;
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 TestCale {
public static void main(String[] args) {
new Calculator();
}
}
//计算器类
class Calculator extends Frame{
public Calculator() {
//三个文本框
TextField num1 = new TextField(10);//字符数
TextField num2 = new TextField(10);//字符数
TextField num3 = new TextField(20);//字符数
//一个按钮
Button button = new Button("=");
//一个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
button.addActionListener(new MyCalcListener(num1,num2,num3));
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//监听器类
class MyCalcListener implements ActionListener{
//获得三个变量
private TextField num1,num2,num3;
public MyCalcListener(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 gui.lesson01;
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 TestCale {
public static void main(String[] args) {
Calculator calculator = new Calculator();
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 MyCalcListener(this));
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//监听器类
class MyCalcListener implements ActionListener{
//获得三个变量
Calculator calculator=null;
public MyCalcListener(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 gui.lesson01;
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 TestCale {
public static void main(String[] args) {
Calculator calculator = new Calculator();
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 MyCalcListener());
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
//监听器类
//内部类最大的优势可以畅通无阻的访问外部类
private class MyCalcListener 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("");
}
}
}
2.6画笔paint
package gui.lesson01;
import java.awt.*;
public class Testpaint {
public static void main(String[] args) {
new Mypaint().loadFrame();
}
}
class Mypaint extends Frame{
public void loadFrame(){
setBounds(200,300,400,500);
setVisible(true);
}
@Override
public void paint(Graphics g) {
//选取颜色
//画笔可以画画
g.setColor(Color.red);
g.drawOval(100,100,100,100);
g.fillOval(50,50,50,50);
g.setColor(Color.GREEN);
g.drawRect(100,300,150,150);
//养成习惯,画笔用完,将他还原为原来原色
g.setColor(Color.BLACK);
}
}
2.7鼠标监听
目的:点击鼠标 画点
package gui.lesson01;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
//鼠标监听
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame2("画图");
}
}
class MyFrame2 extends Frame{
ArrayList points;
//画画需要画笔 需要监听鼠标的位置,需要集合来储存这个点的位置
public MyFrame2(String title){
super(title);
setBounds(200,200,300,400);
//存鼠标的点
points = new ArrayList<>();
//鼠标监听器 在Frame
this.addMouseListener(new MyMouselListenner());
setVisible(true);
}
@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 addPoint(Point point){
points.add(point);
}
//适配器模式
private class MyMouselListenner extends MouseAdapter{
// 鼠标 按下 弹起 按住不动
@Override
public void mousePressed(MouseEvent e) {
MyFrame2 frame=(MyFrame2)e.getSource();
//我们点击的时候会产生一个点!frame.addpoint()没有方法 自己写
//这个点就是鼠标的点
frame.addPoint(new Point(e.getX(),e.getY()));
//需要重画
frame.repaint();
}
}
}
2.8键盘监听
package gui.lesson01;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//键盘
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame() {
setBounds(10,10,100,200);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keyCode= e.getKeyCode();
System.out.println(keyCode);
if(keyCode==KeyEvent.VK_UP) {
System.out.println("你按了上键");
}
//根据按下不同操作,进行不同处理
}
});
}
}
2.9窗口监听
package gui.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindowListener {
public static void main(String[] args) {
new MyWindowFrame();
}
}
class MyWindowFrame extends Frame {
public MyWindowFrame() {
setBounds(100, 100, 200, 300);
setBackground(Color.blue);
setVisible(true);
//addWindowListener(new MyWindowListener());
this.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
super.windowOpened(e);
}
@Override
public void windowClosed(WindowEvent e) {
super.windowClosed(e);
}
//激活窗口
@Override
public void windowActivated(WindowEvent e) {
MyWindowFrame source =(MyWindowFrame) e.getSource();
source.setTitle("被激活了");
super.windowActivated(e);
}
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}
/*class MyWindowListener extends WindowAdapter{
@Override
public void windowClosing(WindowEvent e) {
setVisible(false);//隐藏窗口 通过按钮 隐藏当前窗口
System.exit(0);//正常退出,非正常推出1
}
}
}*/
3.Swing
3.1 窗口 面板
package lesson02;
import com.sun.javaws.util.JfxHelper;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo02 {
public static void main(String[] args) {
new MyJframe().init();
}
}
class MyJframe extends JFrame{
public void init(){
this.setVisible(true);
this.setBounds(10,10,200,200);
JLabel jLabel = new JLabel("欢迎");
this.add(jLabel);
//文本居中
jLabel.setHorizontalAlignment(0);
Container contentPane = this.getContentPane();//获得容器
contentPane.setBackground(Color.blue);
}
}
3.2弹窗
package gui.lesson013;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DialogDemo extends JFrame {
public DialogDemo() {
this.setVisible(true);
this.setBounds(100,100,200,300);
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 MyDialog();
}
});
//加入容器
container.add(button);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗的窗口
class MyDialog extends JDialog{
public MyDialog() {
this.setVisible(true);
this.setBounds(100,100,500,500);
Container container =this.getContentPane();
container.setLayout(null);
//container.add(new Label("我去"));
}
}
3.3标签
new JLabel("xxx")
图标 Icon
package gui.lesson013;
import javax.swing.*;
import java.awt.*;
//图标,需要实现类,Frame 继承
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public IconDemo(){}//无参构造
public IconDemo(int width,int height){
this.height=height;
this.width=width;
}//有参构造
public void init(){
IconDemo iconDemo = new IconDemo(30, 30);
//图标放在标签,或按钮上
JLabel label = new JLabel("小图标黑点", iconDemo, SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
this.setVisible(true);
this.setBounds(100,100,200,500);
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,height,width);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
图片Icon
package gui.lesson013;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDome extends JFrame {
public ImageIconDome(){
JLabel jLabel = new JLabel();
URL url = ImageIconDome.class.getResource("萌狗.jpg");//获得当前包下的文件
ImageIcon imageIcon = new ImageIcon(url);
jLabel.setIcon(imageIcon);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(jLabel);
setBounds(100,100,300,300);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ImageIconDome();
}
}
3.4面板
Jpanel
package lesson02;
import javax.swing.*;
import java.awt.*;
public class JpanelDemo extends JFrame {
public JpanelDemo(){
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,2,10,10));
JPanel panel1 = new JPanel(new GridLayout(2,2));
JPanel panel2 = new JPanel(new GridLayout(2,1));
JPanel panel3 = new JPanel(new GridLayout(3,1));
JPanel panel4 = new JPanel(new GridLayout(3,2));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel2.add(new JButton("2"));
panel2.add(new JButton("2"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
panel3.add(new JButton("3"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
panel4.add(new JButton("4"));
container.add(panel1);
container.add(panel2);
container.add(panel3);
container.add(panel4);
this.setVisible(true);
this.setSize(300,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JpanelDemo();
}
}
JScroll
package lesson02;
import javax.swing.*;
import java.awt.*;
public class JscrollDemo extends JFrame {
public JscrollDemo(){
JTextArea jTextArea = new JTextArea(30,50);
jTextArea.setText("初次见面 请多多关照");
//Scrollpanel
JScrollPane jScrollPane = new JScrollPane(jTextArea);
Container container = this.getContentPane();
container.add(jScrollPane);
this.setVisible(true);
this.setBounds(100,200,300,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JscrollDemo();
}
}
3.5按钮
普通按钮:
package gui.lesson013;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JbuttonDemo extends JFrame {
public JbuttonDemo(){
Container container = this.getContentPane();
URL resource = JbuttonDemo.class.getResource("萌狗.jpg");
ImageIcon imageIcon = new ImageIcon(resource);
JButton jButton = new JButton();
jButton.setIcon(imageIcon);
jButton.setToolTipText("图片按钮");
container.add(jButton);
this.setVisible(true);
this.setSize(300,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JbuttonDemo();
}
}
-
单选按钮 Jradiobutton
package gui.lesson013; import javax.swing.*; import java.awt.*; import java.net.URL; public class JbuttonDemo extends JFrame { public JbuttonDemo(){ Container container = this.getContentPane(); URL resource = JbuttonDemo.class.getResource("萌狗.jpg"); ImageIcon imageIcon = new ImageIcon(resource); //单选框 JRadioButton radioButton01 = new JRadioButton("jbutton01"); JRadioButton radioButton02 = new JRadioButton("jbutton02"); JRadioButton radioButton03 = new JRadioButton("jbutton03"); //单选框 每次只能选一个 分组 group 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(300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JbuttonDemo(); } }
-
复选按钮JcheckBox
package gui.lesson013; import javax.swing.*; import java.awt.*; import java.net.URL; public class JbuttonDemo extends JFrame { public JbuttonDemo(){ Container container = this.getContentPane(); container.setLayout(new FlowLayout()); URL resource = JbuttonDemo.class.getResource("萌狗.jpg"); ImageIcon imageIcon = new ImageIcon(resource); //多选框 JCheckBox checkBox01 = new JCheckBox("工资"); JCheckBox checkBox02 = new JCheckBox("头发"); JCheckBox checkBox03 = new JCheckBox("感情"); container.add(checkBox01); container.add(checkBox02); container.add(checkBox03); this.setVisible(true); this.setSize(300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JbuttonDemo(); } }
3.6列表
下拉框JcomboBox
package gui.Lesson04;
import javafx.scene.control.ComboBox;
import javax.swing.*;
import java.awt.*;
public class TestComboBoxDemo extends JFrame {
public TestComboBoxDemo() {
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(300,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboBoxDemo();
}
}
列表框
package gui.Lesson04;
import javafx.scene.control.ComboBox;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestComboBoxDemo extends JFrame {
public TestComboBoxDemo() {
Container container = this.getContentPane();
//String[] content={"1","2","3"};
Vector vector = new Vector();
JList jList = new JList(vector);
vector.add("太清");
vector.add("上清");
vector.add("玉清");
container.add(jList);
this.setVisible(true);
this.setSize(300,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboBoxDemo();
}
}
应用场景
- 选择地区,或者一些单个选项
- 列表,展示信息,一般是动态扩容
3.7文本框
文本框
package gui.Lesson04;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo01 extends JFrame {
public TestTextDemo01() {
Container container = this.getContentPane();
//container.setLayout(null);
JTextField textField = new JTextField("hello");
JTextField textField02 = new JTextField("world");
container.add(textField,BorderLayout.WEST);
container.add(textField02,BorderLayout.EAST);
this.setVisible(true);
this.setSize(300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}
密码框Jpasswordfield
package gui.Lesson04;
import javax.swing.*;
import java.awt.*;
public class TestTextDemo01 extends JFrame {
public TestTextDemo01() {
Container container = this.getContentPane();
//container.setLayout(null);
JPasswordField passwordField = new JPasswordField("asdhkj");
//passwordField.setEchoChar('*');
container.add(passwordField);
//container.add(textField02,BorderLayout.EAST);
this.setVisible(true);
this.setSize(300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestTextDemo01();
}
}
文本域
见弹窗处
贪吃蛇
帧 如果时间片足够小,就是动画,一秒30帧,一秒60帧。连起来是动画,拆开静态是图片!
键盘监听
定时器Timer
四部:1.定义数据
2.画到画板
3.事件监听
package gui.game;
import javax.swing.*;
import java.net.URL;
//数据中心
public class Date {
public static URL headerURL=Date.class.getResource("statics/header.png");
public static ImageIcon header= new ImageIcon(headerURL);
public static URL bodyURL=Date.class.getResource("statics/body.png");
public static ImageIcon body= new ImageIcon(bodyURL);
public static URL upURL=Date.class.getResource("statics/up.png");
public static ImageIcon up= new ImageIcon(upURL);
public static URL downURL=Date.class.getResource("statics/down.png");
public static ImageIcon down= new ImageIcon(downURL);
public static URL rightURL=Date.class.getResource("statics/right.png");
public static ImageIcon right= new ImageIcon(rightURL);
public static URL leftURL=Date.class.getResource("statics/left.png");
public static ImageIcon left= new ImageIcon(leftURL);
public static URL foodURL=Date.class.getResource("statics/food.png");
public static ImageIcon food= new ImageIcon(foodURL);
}
package gui.game;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
//游戏面板
public class GamePanel extends JPanel implements KeyListener, ActionListener {
//绘制画板,游戏中的所有东西,都是由这个画笔来画
//绘制静态蛇
int length;//蛇的长度
int[] snakeX=new int[600];//25*25为1格
int[] snakeY=new int[600];//25*25为1格
String fx;//方向
//游戏当前的状态
boolean isstart=false;//游戏
//定时器
//Timer timer=new Timer(80,this);//100ms执行一次
//食物坐标
int foodx;
int foody;
boolean isFail=false;//判定是否失败
Random random = new Random();
int score;
int time=80;
//定时器
public GamePanel() {
init();
//获得焦点和键盘情况
this.setFocusable(true);//获得焦点情况
this.addKeyListener(this);//获得键盘情况
Timer timer=new Timer(time,this);//100ms执行一次
timer.start();
//初始化食物
foodx=25+25*random.nextInt(34);
foody=75+25*random.nextInt(24);
}
public void init(){//初始化
length=3;
snakeX[0]=100;snakeY[0]=100;//头
snakeX[1]=75;snakeY[1]=100;//第一个身体
snakeX[2]=50;snakeY[2]=100;//第二个身体
fx="R";
score=0;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//绘制静态面板
this.setBackground(Color.white);
Date.header.paintIcon(this,g,25,11);//广告栏
g.fillRect(25,75,850,600);//默认界面
//画食物
Date.food.paintIcon(this,g,foodx,foody);
//把小蛇画上去
if(fx.equals("R")) {
Date.right.paintIcon(this, g, snakeX[0], snakeY[0]);//蛇头初始化向右
}else if(fx.equals("L")) {
Date.left.paintIcon(this, g, snakeX[0], snakeY[0]);
}else if(fx.equals("U")) {
Date.up.paintIcon(this, g, snakeX[0], snakeY[0]);
}else if(fx.equals("D")) {
Date.down.paintIcon(this, g, snakeX[0], snakeY[0]);
}
for (int i = 1; i < length; i++) {
Date.body.paintIcon(this,g,snakeX[i],snakeY[i]);//第一个身体
}
//画积分
g.setColor(Color.WHITE);
g.setFont(new Font("微软雅黑",Font.BOLD,18));
g.drawString("长度"+length,750,35);
g.drawString("得分为"+score,750,55);
//游戏状态
if(!isstart){
g.setColor(Color.WHITE);
g.setFont(new Font("微软雅黑",Font.BOLD,40));
g.drawString("按下空格开始游戏",300,300);
}
if(isFail){
g.setColor(Color.RED);
g.setFont(new Font("微软雅黑",Font.BOLD,40));
g.drawString("游戏失败,得分为:"+score,300,300);
}
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode=e.getKeyCode();//获得按键是哪一个
if(keyCode==KeyEvent.VK_SPACE) {
if(isFail){
isFail=false;
init();
}
isstart = !isstart;//取反
repaint();
}
if(keyCode==KeyEvent.VK_UP && fx != "D") {
fx="U";
}else if(keyCode==KeyEvent.VK_DOWN && fx != "U") {
fx="D";
}else if(keyCode==KeyEvent.VK_LEFT && fx != "R"){
fx="L";
}else if(keyCode==KeyEvent.VK_RIGHT && fx != "L"){
fx="R";
}
}
//事件监听--固定事件来刷新
@Override
public void actionPerformed(ActionEvent e) {
if(isstart && !isFail){
//右移
for(int i = length-1;i>0;i--){
snakeX[i]= snakeX[i-1];
snakeY[i]= snakeY[i-1];
}
//走向
if (fx.equals("R")) {
snakeX[0] = snakeX[0] + 25;
if (snakeX[0] > 850) {
snakeX[0] = 25;
}
}else if (fx.equals("L")) {
snakeX[0] = snakeX[0] - 25;
if (snakeX[0] < 25) {
snakeX[0] = 850;
}
}else if (fx.equals("U")) {
snakeY[0] = snakeY[0] - 25;
if (snakeY[0] <75) {
snakeY[0] = 650;
}
}else if (fx.equals("D")) {
snakeY[0] = snakeY[0] + 25;
if (snakeY[0] > 650) {
snakeY[0] = 75;
}
}
//吃食物
if(snakeX[0]==foodx && snakeY[0]==foody){
length++;//长度加一
foodx=25+25*random.nextInt(34);
foody=75+25*random.nextInt(24);
score++;
}
//失败判断
for (int i = 1; i < length; i++) {
if(snakeX[0]==snakeX[i] && snakeY[0]==snakeY[i] ){
isFail=true;
}
}
repaint();//重置页面
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
package gui.game;
import javax.swing.*;
//主启动类
public class Game {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setBounds(10,10,900,750);//计算得到
frame.setResizable(false);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//正常游戏应该在面板上
frame.add(new GamePanel());
frame.setVisible(true);
}
}
只是浅显的笔记 为了自己以后复习用