//程序作用:运行程序弹出一个窗口,窗口上有个按钮,点击按钮,跳转到另一个窗口,另一个窗口上有个文本框,在文本框中输入字符串,转换成ACSII,显示输入的字符串和转换完之后的acsii,(表达能力有限有看不懂的可以在下面留言)
package com;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class wangyvxin {
static JFrame frame = new JFrame();
public static void main(String[] args) {
//窗体大小
frame.setSize(500,500);
frame.setLocation(700,200);
//按钮
JButton button =new JButton("点击我");
//在窗体上添加按钮
frame.add(button);
//显示窗体
frame.setVisible(true);
//添加点击事件监听器(你可以使用任何其他监听,看你想在什么情况下创建新的窗口了)
button.addActionListener(new ActionListener(){
//单击按钮执行的方法
public void actionPerformed(ActionEvent e) {
closeThis();
//创建新的窗口
JFrame f = new JFrame("新窗口");
f.setLayout(new FlowLayout());
//设置在屏幕的位置
f.setLocation(700,200);
//窗体大小
f.setSize(500,500);
JTextField tf=new JTextField(30);//创建文本框对象
//在窗口中添加文本框组件
f.add(tf);
//显示窗体
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//为文本框添加键盘事件监听器
tf.addKeyListener (new KeyAdapter (){
public void keyPressed (KeyEvent e) {
//获取对应的键盘字符
char keyChar =e.getKeyChar();
//获取对应的键盘字符代码
int keyCode =e.getKeyCode();
System.out.print("键盘输入的字符内容为:"+keyChar+"");
System.out.println("转换成ACSII为:"+keyCode);
}
});
public static void main(String[] args){
//使用SwingUtilities工具类调用createAndShowGUI()方法并显示GUI程序
SwingUtilities.invokeLater(wangyvxin::createAndShowGUI);
}
});
}
public static void closeThis(){
frame.dispose();
}
}