GUI编程
组件
窗口
弹窗
面板
文本框
列表框
按钮
图片
监听事件
鼠标
键盘事
破解工具
简介
GUI的核心技术:Swing AWT 界面不美观 需要jre环境
为了了解MVC架构 了解监听。
AWT
包含很多类和接口
元素:窗口,按钮,文本框
java.awt
组件和容器
Frame
是一个顶级窗口
import java.awt.*;
//GUI第一个界面
public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("我的第一个界面");
//需要设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色
frame.setBackground(new Color(22,44,66));
//设置窗口初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}
尝试回顾封装
import java.awt.*;
public class TestFrame1 {
public static void main(String[] args) {
MyFrame m1 = new MyFrame(100,100,200,200,Color.BLACK);
MyFrame m2 = new MyFrame(300,100,200,200,Color.red);
MyFrame m3 = new MyFrame(100,300,200,200,Color.yellow);
MyFrame m4 = new MyFrame(300,300,200,200,Color.BLUE);
}
}
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);
}
}
Panel面板
Panel无法单独显示,必须添加到某个容器中
解决了关闭事件
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//坐标和颜色
frame.setBounds(400,400,500,500);
frame.setBackground(new Color(13, 24, 56));
//panel设置坐标,相当于frame
panel.setBounds(100,100,300,300);
panel.setBackground(new Color(144, 11, 44));
//frame.add();
frame.add(panel);
//设置可见性
frame.setVisible(true);
//监听事件,监听窗口关闭事件 System.exit(0);
//适配器模式
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
布局管理器
流式布局
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestButton {
public static void main(String[] args) {
Frame frame = new Frame();
//组件-按钮
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
//设置为流式布局
//frame.setLayout(new FlowLayout());
//frame.setLayout(new FlowLayout(FlowLayout.LEFT) );
frame.setLayout(new FlowLayout(FlowLayout.RIGHT) );
frame.setVisible(true);
frame.setBounds(200,200,400,400);
//把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}东西南北中
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 east = new Button("East");
Button west = new Button("West");
Button south = new Button("South");
Button north = new Button("North");
Button center = new Button("Center");
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(south,BorderLayout.SOUTH);
frame.add(north,BorderLayout.NORTH);
frame.add(center,BorderLayout.CENTER);
frame.setSize(400,300);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}表格布局
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame();
Button btn1 = new Button("btn1");
Button btn2 = new Button("btn2");
Button btn3 = new Button("btn3");
Button btn4 = new Button("btn4");
Button btn5 = new Button("btn5");
Button btn6 = new Button("btn6");
frame.setLayout(new GridLayout(3,2));
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.add(btn6);
frame.pack();//java函数
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
练习
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Test {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
frame.setSize(400,400);
frame.setLocation(200,200);
frame.setBackground(new Color(1,4,6));
frame.setLayout(new GridLayout(2,1));
//new四个面板
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);
for (int i = 0; i < 4; i++) {
p4.add(new Button("for-"+i));
}
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
事件监听
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 ac = new MyActionListener();
button.addActionListener(ac);
windowClose(frame);//关闭窗口
frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
//关闭窗体事件
public 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("aa");
}
}
多个按钮实现同一监听事件
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; public class TestActionEvent2 {
public static void main(String[] args) {
//两个按钮实现同一监听
Frame frame = new Frame("开始-停止");
Button b1 = new Button("start");
Button b2 = new Button("stop");MyActionListener1 ac = new MyActionListener1();
b1.addActionListener(ac);
b2.addActionListener(ac);
//可以显示的定义触发会返回的命令,不显示走默认的值
//多个按钮使用一个监听类
b2.setActionCommand("b2-->stop");
frame.add(b1,BorderLayout.NORTH);
frame.add(b2,BorderLayout.SOUTH); frame.pack();
frame.setVisible(true);
}
}
class MyActionListener1 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//e.getActionCommand()获得按钮信息
System.out.println("按钮被点击了-->"+e.getActionCommand());
}
}
输入框监听事件
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestText01 {
public static void main(String[] args) {
new MyFrame();
//启动
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);
//监听这个文本框输入的文字
MyActionListener03 mal = new MyActionListener03();
//按下enter,就会触发文本框事件
textField.addActionListener(mal);
//设置替换编码
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListener03 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField textField= (TextField) e.getSource();//获取资源,返回一个对象
System.out.println(textField.getText());//获得输入框的文本
textField.setText("");//null ""
}
}
简易计算器
优化前
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
new MyCalculator();
}
}
//计算器类
class MyCalculator extends Frame{
public MyCalculator() {
//3个文本框
TextField text1 = new TextField(10);
TextField text2 = new TextField(10);
TextField text3 = new TextField(20);
//1个按钮
Button button = new Button("=");
//点击事件
button.addActionListener(new MyCalculatorListener(text1,text2,text3));
//1个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(text1);
add(label);
add(text2);
add(button);
add(text3);
setVisible(true);
pack();
}
}
//监听事件类
class MyCalculatorListener implements ActionListener{
private TextField text1,text2,text3;
//获取三个变量
public MyCalculatorListener(TextField text1, TextField text2, TextField text3) {
this.text1 = text1;
this.text2 = text2;
this.text3 = text3;
}
@Override
public void actionPerformed(ActionEvent e) {
//获得加数和被加数
Integer i1 = Integer.parseInt(text1.getText());
Integer i2 = Integer.parseInt(text2.getText());
//将这个值+之和放第三框
text3.setText(""+(i1+i2));
//清除前两个框
text1.setText("");
text2.setText("");
}
}
优化为面向对象
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
MyCalculator myCalculator = new MyCalculator();
myCalculator.loadFrame();
}
}
//计算器类
class MyCalculator extends Frame{
//属性
TextField text1,text2,text3;
//方法
public void loadFrame(){
//3个文本框
text1 = new TextField(10);//字符数
text2 = new TextField(10);
text3 = new TextField(20);
//1个按钮
Button button = new Button("=");
//1个标签
Label label = new Label("+");
//点击事件
button.addActionListener(new MyCalculatorListener(this));
//布局
setLayout(new FlowLayout());
add(text1);
add(label);
add(text2);
add(button);
add(text3);
setVisible(true);
pack();
}
}
//监听事件类
class MyCalculatorListener implements ActionListener{
//获取计算器这个对象,在一个类中组合另一个类
MyCalculator calculator=null;
public MyCalculatorListener(MyCalculator calculator) {
this.calculator=calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//获得加数和被加数
//将这个值+之和放第三框
//清除前两个框
int n1 = Integer.parseInt(calculator.text1.getText());
int n2 = Integer.parseInt(calculator.text2.getText());
calculator.text3.setText(""+(n1+n2));
calculator.text1.setText("");
calculator.text2.setText("");
}
}
内部类好处是可以更好的访问外部类的方法和属性
更好的包装
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
MyCalculator myCalculator = new MyCalculator();
myCalculator.loadFrame();
}
}
//计算器类
class MyCalculator extends Frame{
//属性
TextField text1,text2,text3;
//方法
public void loadFrame(){
//3个文本框
text1 = new TextField(10);//字符数
text2 = new TextField(10);
text3 = new TextField(20);
//1个按钮
Button button = new Button("=");
//1个标签
Label label = new Label("+");
//点击事件
button.addActionListener(new MyCalculatorListener());
//布局
setLayout(new FlowLayout());
add(text1);
add(label);
add(text2);
add(button);
add(text3);
setVisible(true);
pack();
}
//监听事件类
//内部类
private class MyCalculatorListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//获得加数和被加数
//将这个值+之和放第三框
//清除前两个框
int n1 = Integer.parseInt(text1.getText());
int n2 = Integer.parseInt(text2.getText());
text3.setText(""+(n1+n2));
text1.setText("");
text2.setText("");
}
}
}
画笔
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(100,100,100,100);//实心圆
//用完画笔还原颜色
}
}
鼠标监听
实现画点
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 MyFrame("画图");
}
}
class MyFrame extends Frame{
//画画需要画笔需要监听鼠标当前的位置,需要集合存储这个点
ArrayList points;
public MyFrame(String title) {
super(title);
setBounds(200,200,300,500);
//存鼠标点击的点
points=new ArrayList<>();
//鼠标监听器,是针对这个窗口
this.addMouseListener(new MyMouseListener());
setVisible(true);
}
@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);
}
}
//添加一个点到界面上
public void addPaint(Point point){
points.add(point);
}
//适配器模式
private class MyMouseListener extends MouseAdapter{
//鼠标 按下 弹起 按住不放
@Override
public void mousePressed(MouseEvent e) {
MyFrame myFrame = (MyFrame) e.getSource();
//我们点击时候,就会在界面产生一个点
myFrame.addPaint(new Point(e.getX(),e.getY()));
//每次点击鼠标都需要重新画一下
myFrame.repaint();//刷新
}
}
}
窗口监听
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new MyWindow();
}
}
class MyWindow extends Frame{
public MyWindow() {
setVisible(true);
setBounds(100,200,300,400);
setBackground(Color.BLUE);
//addWindowListener(new MyWindowListener());
this.addWindowListener(
//匿名内部类
new WindowAdapter() {
//关闭窗口
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
System.exit(0);
}
//激活窗口
@Override
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");
}
});
}
}
键盘监听
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(1,2,100,200);
setVisible(true);
addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
super.keyTyped(e);
}
@Override
public void keyPressed(KeyEvent e) {
//按下的键是哪一个
int keyCode = e.getKeyCode();
System.out.println(keyCode);//不需要记录这个值,直接使用静态属性VK_xx
if (keyCode==KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
}
@Override
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
}
});
}
}
Swing
JFrame
import javax.swing.*;
import java.awt.*;
public class TestJFrame {
//init()初始化
public void init(){
JFrame jframe = new JFrame("第一个JFrame窗口");
jframe.setVisible(true);
jframe.setBackground(Color.BLUE);
jframe.setBounds(100,200,200,200);
//设置文字JLabel
JLabel jLabel = new JLabel("欢迎光临");
jframe.add(jLabel);
//关闭事件
jframe.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new TestJFrame().init();
}
}
弹窗
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class TestDialog extends JFrame {
public TestDialog() {
this.setVisible(true);
this.setBounds(200,300,400,500);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
//容器
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("点击弹出一个对话框");//创建
button.setBounds(30,20,100,50);
//点击按钮的时候,弹出一个弹窗
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialog();
}
});
container.add(button);
}
public static void main(String[] args) {
new TestDialog();
}
}
//设置一个弹窗
class MyDialog extends JDialog{
public MyDialog() {
this.setVisible(true);
this.setBounds(100,50,100,100);
//this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
container.add(new Label("嘿嘿嘿,我来了"));
}
}
标签
图标Icon
import javax.swing.*;
import java.awt.*;
//图标,需要实现类,Frame继承
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 testIcon = new TestIcon(100,100);
//图标放在标签上,也可以放在按钮上
JLabel label = new JLabel("testIcon", testIcon, SwingConstants.CENTER);
Container container = this.getContentPane();
container.add(label);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return width;
}
@Override
public int getIconHeight() {
return height;
}
public static void main(String[] args) {
new TestIcon().init();
}
}
图片Image
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame{
public ImageIconDemo() {
//获取图片的地址
JLabel label = new JLabel("图片");
URL url = ImageIconDemo.class.getResource("1.jpg");
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = this.getContentPane();
container.add(label);
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setBounds(100,200,50,50);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
面板
JPanel
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 jPanel = new JPanel(new GridLayout(2,2,5,5));
jPanel.add(new Button("1"));
jPanel.add(new Button("1"));
jPanel.add(new Button("1"));
jPanel.add(new Button("1"));
container.add(jPanel);
this.setVisible(true);
this.setSize(200,100);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new TestJPanel();
}
}
import javax.swing.*;
import java.awt.*;
public class TestJScroll extends JFrame {
public TestJScroll() {
TextArea textArea = new TextArea("欢迎光临");//文本域
Container container = this.getContentPane();
//scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,150,250);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new TestJScroll();
}
}
按钮
图片按钮
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class TestJButton extends JFrame {
public TestJButton() {
Container container = this.getContentPane();
//将图片变为一个图标
URL resource = TestJButton.class.getResource("1.jpg");
Icon icon = new ImageIcon(resource);
//把图标放在按钮上
JButton jButton = new JButton();
jButton.setIcon(icon);
jButton.setToolTipText("图片按钮");
container.add(jButton);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setBounds(100,100,200,200);
}
public static void main(String[] args) {
new TestJButton();
}
}
单选按钮
import javax.swing.*;
import java.awt.*;
public class TestJButton1 extends JFrame {
public TestJButton1() {
Container container = this.getContentPane();
//单选框
JRadioButton radioButton1 = new JRadioButton("radioButton1");
JRadioButton radioButton2 = new JRadioButton("radioButton2");
JRadioButton radioButton3 = new JRadioButton("radioButton3");
//由于单选框只能选一个,分组
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
group.add(radioButton3);
container.add(radioButton1,BorderLayout.NORTH);
container.add(radioButton2,BorderLayout.CENTER);
container.add(radioButton3,BorderLayout.SOUTH);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setBounds(100,100,200,200);
}
public static void main(String[] args) {
new TestJButton1();
}
}
复选按钮
import javax.swing.*;
import java.awt.*;
public class TestJButton2 extends JFrame {
public TestJButton2() {
Container container = this.getContentPane();
//复选框
Checkbox checkbox1 = new Checkbox("checkbox1");
Checkbox checkbox2 = new Checkbox("checkbox1");
container.add(checkbox1,BorderLayout.NORTH);
container.add(checkbox2,BorderLayout.SOUTH);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setBounds(100,100,200,200);
}
public static void main(String[] args) {
new TestJButton2();
}
}
列表
下拉框
import javax.swing.*;
import java.awt.*;
public class TestComboBox extends JFrame {
public TestComboBox() {
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,100);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboBox();
}
}
列表框
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestComboBox1 extends JFrame {
public TestComboBox1() {
Container container = this.getContentPane();
//生成列表的内容
//String [] strings = {"1","2","3"};
//列表放入的内容
Vector vector = new Vector();
JList jList = new JList(vector);
vector.add("yier");
vector.add("sas");
container.add(jList);
this.setVisible(true);
this.setSize(300,100);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboBox1();
}
}
应用场景:选择地区,或者一些单选项
列表:展示信息,一般是动态扩容。
文本框
文本框
import javax.swing.*;
import java.awt.*;
public class Test01 extends JFrame {
public Test01() {
Container container = this.getContentPane();
TextField textField = new TextField("heheh",10);
container.add(textField);
this.setVisible(true);
this.setSize(300,100);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new Test01();
}
}
密码框
import javax.swing.*;
import java.awt.*;
public class Test01 extends JFrame {
public Test01() {
Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
container.add(passwordField);
this.setVisible(true);
this.setSize(300,100);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new Test01();
}
}
文本域
import javax.swing.*;
import java.awt.*;
public class TestJScroll extends JFrame {
public TestJScroll() {
TextArea textArea = new TextArea("欢迎光临");//文本域
Container container = this.getContentPane();
//scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,150,250);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new TestJScroll();
}
}
贪吃蛇
帧 30,60帧,拆开细分就是一些静态得图片
需要知识;键盘监听,定时器Timer
import javax.swing.*;
//游戏主启动类
public class StartGame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setBounds(100,100,900,730);
frame.setResizable(false);//窗口大小不可变
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
//正常游戏面板都在这
frame.add(new GamePanel());
}
}
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];//蛇的X坐标 25*25
int[] snakeY = new int[500];//蛇的Y坐标 25*25
String fx;
//食物的坐标
int foodx;
int foody;
int score;//fens
Random random= new Random();
//游戏当前的状态,停止-->开始
boolean isStart = false;//默认停止
boolean isFail = false;//游戏失败状态
//定时器 以毫秒为单位 1000ms=1s
Timer timer = new Timer(100,this);//100毫秒执行一次
//构造器
public GamePanel() {
init();
//获得焦点和键盘事件
this.setFocusable(true);//获得焦点事件
this.addKeyListener(this);//获得键盘监听事件
}
//初始化方法
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";//初始化方向向右
//把食物随机分配在界面上
foodx=25+25*random.nextInt(34);
foody=75+25*random.nextInt(24);
timer.start();//游戏一开始定时器就启动
score=0;
}
//绘制面板,我们游戏中所以得东西都是用这个画笔画
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);//清屏
//绘制静态的画板
this.setBackground(Color.WHITE);
Data.header.paintIcon(this,g,25,11);//头部广告栏画上去
g.fillRect(25,75,850,600);//默认的游戏界面
//画积分
g.setColor(Color.white);
g.setFont(new Font("微软雅黑",Font.BOLD,18));
g.drawString("长度 "+length,750,35);
g.drawString("分数 "+score,750,55);
//画食物
Data.food.paintIcon(this,g,foodx,foody);
//把小蛇画上去
if (fx.equals("R")){
Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);//蛇头初始化向右
}else if (fx.equals("L")){
Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);//蛇头初始化向左
}else if (fx.equals("U")){
Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);//蛇头初始化向上
}else if (fx.equals("D")){
Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);//蛇头初始化向下
}
for (int i = 1; i <length ; i++) {
Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);//身体坐标
}
//游戏状态
if (isStart==false){
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("失败,按下空格重新开始",300,300);
}
}
//键盘监听器
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();//获得键盘按键哪一个
if (keyCode==KeyEvent.VK_SPACE){//按下空格键
if (isFail){
//重新开始
isFail=false;
init();
}else {
isStart = !isStart;//取反
}
repaint();
}
//小蛇移动
if (keyCode==KeyEvent.VK_RIGHT){
fx="R";
}else if (keyCode==KeyEvent.VK_LEFT){
fx="L";
}else if (keyCode==KeyEvent.VK_UP){
fx="U";
}else if (keyCode==KeyEvent.VK_DOWN){
fx="D";
}
}
//事件监听--需要通过固定事件刷新 1s=10次
@Override
public void actionPerformed(ActionEvent e) {
if (isStart&&isFail==false) {//如果游戏开始状态,就让小蛇动起来
if (snakeX[0]==foodx&&snakeY[0]==foody){
length++;//长度+1
score=score+10;//吃一次加10
//再次随机食物
foodx=25+25*random.nextInt(34);
foody=75+25*random.nextInt(24);
}
//右移
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;}
}
//失败判断 撞到自己就是失败
for (int i = 1; i <length ; i++) {
if (snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]){
isFail=true;
}
}
repaint();//重画页面
}
timer.start();//定时器开启
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}
GUI编程的更多相关文章
- JAVA GUI编程学习笔记目录
2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...
- 1.JAVA之GUI编程概述
下列内容为本人看毕向东老师java视频教程学习笔记! JAVA GUI图形用户界面编程: Windows 操作系统提供两种操作方式: ...
- 2.JAVA之GUI编程布局
布局管理器 容器中的组件排放方式,就是布局 常见的布局管理器: **************************************************** 1.FlowLayout(流式 ...
- 3.JAVA之GUI编程Frame窗口
创建图形化界面思路: 1.创建frame窗体: 2.对窗体进行基本设置: 比如大小.位置.布局 3.定义组件: 4.将组件通过add方法添加到窗体中: 5.让窗体显示,通过setVisible(tur ...
- 4.JAVA之GUI编程事件监听机制
事件监听机制的特点: 1.事件源 2.事件 3.监听器 4.事件处理 事件源:就是awt包或者swing包中的那些图形用户界面组件.(如:按钮) 事件:每一个事件源都有自己特点有的对应事件和共性事件. ...
- 5.JAVA之GUI编程窗体事件
我们回顾下第三篇时的内容: 在3.JAVA之GUI编程Frame窗口中窗体是无法直接关闭的,想要关闭须进程管理器结束进程方式关掉. 现在我们就来解决下这个问题. ******************* ...
- 6.JAVA之GUI编程Action事件
功能:单击一个按钮实现关闭窗口: import java.awt.*; import java.awt.event.*; public class StudyAction { // 定义该图形所需的组 ...
- 7.JAVA之GUI编程鼠标事件
鼠标事件: 功能: 1.基本窗体功能实现 2.鼠标移动监听,当鼠标移动到按钮上时,触发打印事件. 3.按钮活动监听,当按钮活动时,触发打印事件. 4.按钮被单击时触发打印事件. 源码如下: impor ...
- 8.JAVA之GUI编程键盘码查询器
程序使用说明: 1.本程序由于是java代码编写,所以运行需安装jdk并配置好环境变量. 2. 复制java代码到记事本内,另存为Keyboard_events.java: 3.复制批处理代码到记事本 ...
- 9.JAVA之GUI编程列出指定目录内容
代码如下: /*列出指定目录内容*/ import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; import ...
随机推荐
- Django - WebSocket:dwebsocket
Django - WebSocket:dwebsocket 什么是WebSocket WebSocket是一种在单个TCP连接上进行全双工通信的协议 WebSocket使得客户端和服务器之间的数据交换 ...
- linux串口编程
按照对linux系统的理解,串口编程的顺序无非就是open,read,write,close,而串口有波特率.数据位等重要参数需要设置,因此还应该用到设置函数,那么接下来就带着这几个问题去学习linu ...
- Centos 安装postgreSQL9.4.3
rpm -ivh http://download.postgresql.org/pub/repos/yum/9.4/redhat/rhel-7.2-x86_64/pgdg-centos94-9.4-3 ...
- Redis布隆过滤器与布谷鸟过滤器
大家都知道,在计算机中,IO一直是一个瓶颈,很多框架以及技术甚至硬件都是为了降低IO操作而生,今天聊一聊过滤器,先说一个场景: 我们业务后端涉及数据库,当请求消息查询某些信息时,可能先检查缓存中是否有 ...
- MySQL设计之Schema与数据类型优化
一.数据类型优化 1.更小通常更好 应该尽量使用可以正确存储数据的最小数据类型,更小的数据类型通常更快,因为它们占用更少的磁盘.内存和CPU缓存,并且处理时需要的CPU周期更少,但是要确保没有低估需要 ...
- # Functions are First-Class Citizens in Python 一等公民
# Functions are First-Class Citizens in Python 一等公民https://cn.bing.com/search?form=MOZSBR&pc=MOZ ...
- Weblogic漏洞利用
Weblogic漏洞 Weblogic任意文件上传(CVE-2018-2894) 受影响版本 weblogic 10.3.6.0.weblogic 12.1.3.0.weblogic 12.2.1.2 ...
- Scala:case class
Scala:case class 1.Scala中class.object.case class.case object区别 1.1 class 和 object 关系 1.2 case class ...
- java判断是否为整数
/** * 判断是否为整数 * * @param str 传入的字符串 * @return 是整数返回true,否则返回false */ public static boolean isInteger ...
- docker启动脚本
#!/bin/bash # 定义环境变量 export LANG="en_US.UTF-8" #统一格式化打印输出信息 printMsg(){ echo "$(date ...