javaGUI学习
- javaswing的基本操作
package com.guistudy;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class study01 {
public static void main(String[] args) {
Frame frame = new Frame("我的第一个Java图像界面窗口"); // Frame需要写监听器才可实现可关闭
frame.setBounds(200,100,400,300); // setLocation()和setSize()的集合体
frame.setBackground(new Color(250,250,250)); // 设置背景颜色
frame.setVisible(true); // 设置窗口可见
frame.setResizable(false); // 设置窗口不可拉伸
Panel panel = new Panel();
frame.setLayout(null); // 设置布局,常用的布局有FlowLayout流式布局,BorderLayout边界布局和GridLayout网格布局
FlowLayout myflowlayout = new FlowLayout(FlowLayout.RIGHT); // 流式布局直接add组件
BorderLayout myborderlayout = new BorderLayout(); // 边界布局添加组件需指定位置,例frame.add(panel,BorderLayout.EAST)
GridLayout mygridlayout = new GridLayout(3,2); //网格布局设置行列数之后可直接add组件
panel.setBounds(0,50,400,200);
panel.setBackground(Color.CYAN);
frame.add(panel); // 添加组件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); //监听时间,监听窗口关闭时间(适配器模式)
MyFrame myFrame = new MyFrame(500,600,300,200); //JFrame是Frame的子类(swing是awt的升级,即带J的是不带J的升级版)
}
}
class MyFrame extends JFrame {
static int id = 0; //窗口计数器
public MyFrame(int x,int y,int w,int h){
super("Myframe"+(++id));
setBounds(x,y,w,h);
setVisible(true);
}
}
-
画笔
class MyPaint extends JFrame{ public MyPaint(){ setBounds(200,200,400,300); setVisible(true); } @Override public void paint(Graphics g) { g.setColor(new Color(32,76,14));// 画笔颜色 g.drawOval(100,100,100,100); //画一个空心的圆 g.fillRect(200,200,100,50); //画一个实心的正方形 } }
运行截图:
-
实现画板
public class study04 { public static void main(String[] args) { new MyPaint("point"); } } class MyPaint extends JFrame{ private ArrayList<Point> points; public MyPaint(String title){ setBounds(200,200,400,300); setTitle(title); setBackground(Color.WHITE); setVisible(true); this.addMouseListener(new MyMouseListener()); // 在构造函数中添加鼠标监听器 points = new ArrayList<>(); } @Override public void paint(Graphics g) { Iterator iterator = points.iterator(); while (iterator.hasNext()){ Point point = (Point) iterator.next(); g.setColor(new Color(65,156,43)); g.fillOval(point.x,point.y,10,10); } } private class MyMouseListener extends MouseAdapter{ @Override public void mousePressed(MouseEvent e) { Frame myFrame = (Frame) e.getSource(); Point point = new Point(e.getX(),e.getY()); points.add(point); // 在列表中添加新的点 myFrame.repaint(); // 刷新画板 } } }
-
-
窗口监听事件 (以下代码实现关闭窗口)
键盘监听事件 (以下代码实现删除点)
-
自定义弹窗
class MyDialog extends JDialog{ public MyDialog(){ this.setVisible(true); this.setBounds(100,100,300,200); this.add(new Label("A dialog")); } }
JOptionPane类
JOptionPane.showMessageDialog(null,"Are you sure to exit?"); int option = JOptionPane.showConfirmDialog(null,"Are you sure to exit?","Title",JOptionPane.YES_NO_OPTION); if (option == 0){ // 设置返回值得到按钮的状态 setVisible(false); }else { System.out.println(option); }
-
Icon,ImageIcon类
JLabel img = new JLabel("ImageIcon"); URL url = MyPaint.class.getResource("banner.png"); //当前类.class.getResource()方法定位的是当前类的路径 ImageIcon imageIcon = new ImageIcon(url); // 传入参数为URL类对象 img.setIcon(imageIcon); // Button对象也可以通过此方法设置图片按钮 img.setHorizontalAlignment(SwingConstants.CENTER); // 设置居中显示 Container container = getContentPane(); // JFrame不能直接添加组件,需要用getContentPane()函数获取内容面板,再在内容面板上进行添加组件 container.add(img); setVisible(true); // 在最后设置setVisible(true);
-
JScrollPane scrollPane = new JScrollPane(img); //Scroll面板,具有滑动条 container.add(scrollPane);
-
JRadioButton radioButton01 = new JRadioButton("1"); JRadioButton radioButton02 = new JRadioButton("2"); JRadioButton radioButton03 = new JRadioButton("3"); ButtonGroup bg = new ButtonGroup(); // 需要一个按钮组将单选框联系起来 bg.add(radioButton01); bg.add(radioButton02); bg.add(radioButton03); container.add(radioButton01); // 在容器中一个个添加单选框 container.add(radioButton02); container.add(radioButton03);
-
JCheckBox checkBox1 = new JCheckBox("1"); // 多选框 JCheckBox checkBox2 = new JCheckBox("2"); JCheckBox checkBox3 = new JCheckBox("3"); container.add(checkBox1); container.add(checkBox2); container.add(checkBox3);
-
JComboBox status = new JComboBox(); // 下拉框 status.addItem(null); status.addItem("1"); status.addItem("2"); status.addItem("3"); container.add(status);
-
String[] contents = {"1","2","3"}; JList list = new JList(contents); // 列表,用于展示动态信息 container.add(list);
-
极简版贪吃蛇
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; public class SnackGame { // 按下空格开始游戏 public static void main(String[] args) { new MainFrame(); } } class MainFrame extends JFrame { private Container container; private GamePanel gamePanel; public MainFrame(){ setTitle("SnackGame"); setBounds(500,400,500,500); setResizable(false); gamePanel = new GamePanel(this); container = getContentPane(); container.add(gamePanel); setVisible(true); } } class GamePanel extends JPanel implements ActionListener { // 实现ActionListener接口实现小蛇的动态效果 private Boolean isStart; private int length; int[] snakeX = new int[500]; int[] snakeY = new int[500]; int buyX; int buyY; private ImageIcon img_snakehead; private ImageIcon img_snakebody; private ImageIcon img_none; private ImageIcon img_bug; private String direction; private Timer timer; public GamePanel(JFrame frame){ init(); frame.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_SPACE){ isStart = !isStart; } if (key == KeyEvent.VK_W && !direction.equals("south")){ direction = "north"; }else if (key == KeyEvent.VK_D && !direction.equals("west")){ direction = "east"; }else if (key == KeyEvent.VK_S && !direction.equals("north")){ direction = "south"; }else if (key == KeyEvent.VK_A && !direction.equals("east")){ direction = "west"; } } }); } public void init(){ this.setBackground(Color.WHITE); this.setSize(500,500); isStart = false; img_snakehead = new ImageIcon(GamePanel.class.getResource("image/snakehead.png")); img_snakebody = new ImageIcon(GamePanel.class.getResource("image/snakebody.png")); img_none = new ImageIcon(GamePanel.class.getResource("image/imgnone.png")); img_bug = new ImageIcon(GamePanel.class.getResource("image/imgbug.png")); length = 4; direction = "east"; snakeX[0] = 100; //头X坐标 snakeY[0] = 100; //头Y坐标 snakeX[1] = 75; //第一节X坐标 snakeY[1] = 100; //第一节Y坐标 snakeX[2] = 50; snakeY[2] = 100; snakeX[3] = 25; snakeY[3] = 100; timer = new Timer(100,this); createbug(); timer.start(); } @Override protected void paintComponent(Graphics g) { img_snakehead.paintIcon(this,g,snakeX[0],snakeY[0]); for (int i = 1;i < length;i++){ img_snakebody.paintIcon(this,g,snakeX[i],snakeY[i]); } img_none.paintIcon(this,g,snakeX[length-1],snakeY[length-1]); img_bug.paintIcon(this,g,buyX,buyY); } @Override public void actionPerformed(ActionEvent e) { if (isStart){ for (int i=length-1;i>=1;i--){ if (snakeX[i]==snakeX[0]&&snakeY[i]==snakeY[0]){ isStart = false; JOptionPane.showConfirmDialog(null,"You are lost\nYour score is "+(length-1)); System.exit(0); } snakeX[i] = snakeX[i-1]; snakeY[i] = snakeY[i-1]; } if (direction.equals("east")){ snakeX[0] += 25; }else if(direction.equals("west")){ snakeX[0] -= 25; }else if(direction.equals("south")){ snakeY[0] += 25; }else if(direction.equals("north")){ snakeY[0] -= 25; } if (snakeX[0] >= 500){ snakeX[0] -= 500; } if (snakeX[0] < 0){ snakeX[0] += 500; } if (snakeY[0] >= 475){ snakeY[0] -=475; } if (snakeY[0] < 0){ snakeY[0] += 475; } if (snakeX[0] == buyX && snakeY[0] == buyY){ createbug(); length ++; } repaint(); } } public void createbug(){ Boolean flag = false; do{ buyX = (int)(Math.random()*19)*25; buyY = (int)(Math.random()*18)*25; for (int i=0;i<length;i++){ if (snakeX[i]==buyX&&snakeY[i]==buyY){ flag = false; break; } if (i == length-1){ flag = true; } } }while (!flag); } }