标签:窗口 sed wing 图片 ted pen bsp type font
第十三周课程总结
通过WindowAdapter实现监听
package dishisanzhou; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class MyWindowListener implements WindowListener { public void windowOpened(WindowEvent e) { System.out.println("窗口打开"); } public void windowClosing(WindowEvent e) { System.out.println("窗口关闭中"); System.exit(1); } public void windowClosed(WindowEvent e) { System.out.println("窗口已关闭"); } public void windowIconified(WindowEvent e) { System.out.println("窗口最小化"); } public void windowDeiconified(WindowEvent e) { System.out.println("窗口最小化恢复"); } public void windowActivated(WindowEvent e) { System.out.println("窗口被选中"); } public void windowDeactivated(WindowEvent e) { System.out.println("窗口被取消"); } }
import javax.swing.*; import java.awt.*; public class MyWindowListenertext { public static void main(String[] args) { JFrame f=new JFrame("ONE OF A KIND"); f.addWindowListener(new MyWindowListener()); f.setSize(460,315); f.setVisible(true); } }
二.键盘处理及监听事件:
在Swing的事件中也可以对键盘的操作进行监听,直接用KeyListener接口即可。
KeyListener接口方法
void keyPressed(KeyEvent e):键入某个键时调用
void keyReleased(KeyEvent e) :键盘按下时调用
void keyTyped(KeyEvent e):键盘松开时调用
package dishisanzhou; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class Jkey extends JFrame { JTextArea area; JScrollPane scr; public Jkey(){ area=new JTextArea(); scr=new JScrollPane(area); scr.setBounds(5,5,300,200); super.setTitle("键盘事件"); area.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent e) { area.append("输入的内容是:"+e.getKeyChar()+"\n"); } }); super.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(1); } }); super.add(scr); super.setSize(400,300); super.setVisible(true); } }
package dishisanzhou; import javax.swing.*; import java.awt.*; public class test { public static void main(String[] args) { new Jkey(); } }
三.菜单组件:
若在java中实现此菜单,则可使用JMenu组件,不过,如果要使用JMenu则首先要了解JMenuBar组件。
JMenuBar的常用方法
1.
标签:窗口 sed wing 图片 ted pen bsp type font
原文地址:https://www.cnblogs.com/lx1943661721/p/11919682.html