序言:GUI界面与SWING技术,作为JAVA的早期的界面展示技术,在一些智能交付设备与毕业设计上仍有必要应用。比如:树莓派4B可以作为语音交互的用户界面、贪吃蛇、坦克大战开发游戏界面,GUI五子棋、GUI操作系统与管理系统等。今天快速大家根据实际生活、工作需求让大家学会使用GUI设计页面。
一、如何实现GUI页面跳转(所有GUI系统设计的核心步骤)
1、AIMain函数运行入口(保证全局一个主容器)
public class AIMain {
public static SonFrame sonFrame; // 全局一个主容器
public static void main(String[] args) throws Exception {
sonFrame = new SonFrame(); // 容器入口
for (int i = 0; i < 2; i++) {
if (i % 2 == 0) {
SonFrame.sonPanel1.setVisible(false);
SonFrame.sonPanel2.setVisible(true);
sonFrame.setTitle("第二个页面");
} else {
SonFrame.sonPanel1.setVisible(true);
SonFrame.sonPanel2.setVisible(false);
sonFrame.setTitle("第一个页面");
}
Thread.sleep(20);
}
}
}
2、主容器的定义。包含两个公开的静态的Panel页面(这个就相当于HTML的两个页面)。
1)同时我们统一全局的点击函数注册addClickListener,只需要在面板Panel声明按钮,就能随时调用此方法注册,统一管理点击操作。(类似JS的click函数)。
2)定义画笔的退出方法。
public class SonFrame extends JFrame {
public static SonPanel1 sonPanel1 = new SonPanel1();
public static SonPanel2 sonPanel2 = new SonPanel2();
public static TextField textField_1 = new TextField(10);
public SonFrame() {
// 流式布局
this.setLayout(new FlowLayout());
this.add(sonPanel1);
this.add(sonPanel2);
this.setLocation(500, 250);
this.setSize(1024, 600);
this.setTitle("sleep");
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
textField_1.getText();
}
public static void addClickListener(JButton jButton) {
jButton.addActionListener(e -> { // lambda表达式
String buttonInfo = e.getActionCommand();
SonFrame temp = (SonFrame) jButton.getParent().getParent().getParent().getParent().getParent();
switch (buttonInfo) {
case "跳转到第二个页面":
sonPanel1.setVisible(false);
sonPanel2.setVisible(true);
temp.setTitle("第二个页面");
break;
case "跳转到第一个页面":
sonPanel1.setVisible(true);
sonPanel2.setVisible(false);
temp.setTitle("第一个页面");
break;
}
});
}
@Override
public void paint(Graphics g) {
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
3、第一个SonPanel1(子页面)的定义。重点添加了一个JButton点击事件,用于跳转页面二。
public class SonPanel1 extends JPanel {
JButton jButton;
ImageIcon originalIcon = new ImageIcon("src/main/resources/sleep.gif");
Image image = originalIcon.getImage();
Image resizedImage = image.getScaledInstance(1024, 600, Image.SCALE_DEFAULT);
ImageIcon resizedIcon = new ImageIcon(resizedImage);
JLabel jLabel = new JLabel(resizedIcon);
public SonPanel1() {
jButton = new JButton("跳转到第二个页面");
SonFrame.addClickListener(jButton);
this.add(jButton);
// this.add(jLabel);
this.setLocation(500, 0);
this.setPreferredSize(new Dimension(1024, 600));
this.setBackground(Color.black);
this.setVisible(true);
}
}
4、第二个SonPanel2(子页面)的定义。重点添加了一个JButton点击事件,用于跳转页面一。
public class SonPanel2 extends JPanel {
JButton touch;
ImageIcon originalIcon = new ImageIcon("src/main/resources/say.gif");
Image image = originalIcon.getImage();
Image resizedImage = image.getScaledInstance(1024, 600, Image.SCALE_DEFAULT);
ImageIcon resizedIcon = new ImageIcon(resizedImage);
JLabel jLabel = new JLabel(resizedIcon);
public SonPanel2() {
touch = new JButton("跳转到第一个页面");
SonFrame.addClickListener(touch);
this.add(touch);
//this.add(jLabel);
this.setLocation(500, 250);
this.setPreferredSize(new Dimension(1024, 600));
this.setBackground(Color.red);
this.setVisible(true);
}
}
5、完成以上代码,就已经掌握了所有GUI页面跳转与点击的核心。当然也包括窗口位置、大小的定义。这是所有的开发都要用到的,并且包含了全局管理的思想,这将使得我们GUI的开发变的简单易懂,不再陷入细节的逻辑。更详细的介绍大家可以点击下方评论B站视频进行学习。
二、GUI设计别的要素(非必要,但能锦上添花)。
1、面板的提示操作,类似VUE的this.$message.warn(),需要在SonFrame!!!里使用!!!这这时候就体现了全局管理,我们使用public static SonFrame sonFrame; // 定义一个全局主容器的重要性。
JOptionPane.showMessageDialog(this, "恭喜您全部答对!", "提示", JOptionPane.WARNING_MESSAGE);
2、文本的获取组件(获取用户文本输入)
// TextField比JTextField好用,JTextField会导致无法输入,程序卡不动。
public static TextField textField_1 = new TextField(10); //SonFrame中定义!!!
textField_1.getText();
3、布局。
FlowLayout(常用,能根据位置定位元素)。BorderLayout 东西南北中布局。表格布局GridLayout。
this.setLayout(new FlowLayout());
4、画笔的使用
public SonPanel1() {
jButton = new JButton("跳转到第二个页面");
SonFrame.addClickListener(jButton);
this.add(jButton);
this.setLocation(500, 0);
this.setPreferredSize(new Dimension(600, 600));
this.setBackground(Color.red);
this.setVisible(true);
}
@Override
protected void paintComponent(Graphics graphics) {
graphics.setColor(Color.white);
graphics.drawOval(100, 100, 100, 100);
graphics.fill3DRect(200, 200, 100, 100, false);
}
}
5、键盘监听事件(游戏的时候会用到)
public SonFrame() {
// 流式布局
this.setLayout(new FlowLayout());
sonPanel1.setFocusable(true); // 允许获取焦点
sonPanel1.addKeyListener(new KeyAdapter() { // 判断如果是Enter则打印或做相关动作
@Override
public void keyPressed(KeyEvent e) {
//System.out.println(e.getKeyCode()); // 不用记录数组,用KeyEvent.XXX即可
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
System.out.println("按下了Enter键盘");
}
}
});
this.add(sonPanel1);
this.add(sonPanel2);
this.setLocation(500, 250);
this.setSize(600, 600);
this.setTitle("主体视图");
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
textField_1.getText();
}
6、贪吃蛇、坦克大战游戏实现原理。面板Panel的刷新与变量位置的变化。
这就是动态页面实现原理,用不高于60ms的时间刷新Panel,JAVA只需要做好变量值的控制。重新渲染动画交给Panel频繁切换即可实现各种游戏画面开发。
for (int i = 0; i < 2; i++) {
if (i % 2 == 0) {
SonFrame.sonPanel1.setVisible(false);
SonFrame.sonPanel2.setVisible(true);
sonFrame.setTitle("第二个页面");
} else {
SonFrame.sonPanel1.setVisible(true);
SonFrame.sonPanel2.setVisible(false);
sonFrame.setTitle("第一个页面");
}
Thread.sleep(20); // 当睡眠时间小于60ms 人眼将觉察不到切换,就是动画的来源
}
三、语音交互设备GUI应用效果展示(唤醒成功展示say页面,没唤醒展示sleep页面)