< 今日知识点 >
·
·
·
·
—01 窗口监听事件
- 大多数时候是用关闭和激活窗口的操作
- 代码实例:
import com.sun.xml.internal.ws.policy.EffectiveAlternativeSelector;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame {
public WindowFrame() {
setBackground(Color.pink);
setBounds(100, 100, 300, 300);
setVisible(true);
// addWindowListener(new MyWindowListener());
//匿名内部类
this.addWindowListener(
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// super.windowClosing(e);
System.out.println("你点击了关闭");
}
}
);
}
class MyWindowListener extends WindowAdapter {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0); //正常退出
}
}
}
- 运行结果:
点击关闭按钮:
·
·
·
·
—02 键盘监听事件
- 代码实例:
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestKeyListener {
public static void main(String[] args) {
KeyFrame keyFrame = new KeyFrame();
WindowClose(keyFrame);
}
private static void WindowClose (Frame frame){ //关闭窗口的方法
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class KeyFrame extends Frame {
public KeyFrame(){
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
//获得键盘按下的键是哪一个,当前按下的键盘的码
//不需要去记住这个数值,直接使用静态属性 VK_XXX
int keyCode = e.getKeyCode();
System.out.println(keyCode);
if (keyCode == KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
}
});
}
}
- 运行结果:
–窗口打开
–按下上键
·
·
·
·
—03 Swing之JFrame窗体
— 代码实例01
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//建立一个窗口
public void init(){
//顶级窗口
JFrame frame = new JFrame("这是一个JFrame窗口");
frame.setVisible(true);
frame.setBounds(100,100,200,200);
frame.setBackground(Color.orange);
//设置文字JLabel
JLabel label = new JLabel("欢迎光临");
label.setHorizontalTextPosition(SwingConstants.CENTER);
frame.add(label);
// frame.getC
//关闭事件
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo().init();
}
}
- 运行结果:
—代码实例02:
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(20,20,200,200);
JLabel label = new JLabel("欢迎光临");
this.add(label);
//让文本标签居中
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = this.getContentPane();
container.setBackground(Color.orange);
}
}
- 运行结果:
·
·
·
·
—04 JDialog弹窗
(弹窗自带有关闭功能,不用重复写)
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.setSize(700,700);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //关闭
//JFrame 放东西,容器
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("点击弹出一个对话框"); //创建
button.setBounds(30,30,400,50);
//点击这个按钮的时候,弹出一个弹窗
button.addActionListener(new ActionListener() { //监听器
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
}); //这是一条语句
container.add(button);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗的窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo(){ //构造方法
this.setVisible(true);
this.setBounds(100,100,500,500);
// this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //关闭
Container container1 = this.getContentPane();
// container1.setLayout(null);
container1.add(new Label("BlackPink in your area!"));
}
}
运行结果:
点击按钮
·
·
·
·