📄 focusadapterdemo.java
字号:
import javax.swing.*;import java.awt.*;import java.awt.event.*;public class FocusAdapterDemo extends JFrame{ private JTextField jtf1, jtf2, jtf3; private JLabel label1, label2; public FocusAdapterDemo() {/* Two JLabels and three JTextFields are created. *//* Two of the textfields register a FocusListener */ jtf1 = new JTextField(20); jtf1.setBorder(BorderFactory.createLineBorder(Color.black)); jtf1.setName("TextField 1"); jtf1.addFocusListener(new FocusHandler()); jtf2 = new JTextField(20); jtf2.setBorder(BorderFactory.createLineBorder(Color.black)); jtf2.setName("TextField 2"); jtf2.addFocusListener(new FocusHandler()); jtf3 = new JTextField(20); jtf3.setEditable(false); label1 = new JLabel("Line 1"); label1.setForeground(Color.black); label2 = new JLabel("Line 2"); label2.setForeground(Color.black);/* The components are placed on the JFrame */ JPanel p1 = new JPanel(); p1.add(label1); p1.add(jtf1); JPanel p2 = new JPanel(); p2.add(label2); p2.add(jtf2); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.add(p1); panel.add(p2); getContentPane().add(panel, BorderLayout.CENTER); getContentPane().add(jtf3, BorderLayout.SOUTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 400, 200); setVisible(true); }/* The FocusListener is implemented as an inner class that extends *//* the FocusAdapter class. Whenever one of the JTextField objects *//* gains keyboard focus, a FocusEvent is generated and sent to the *//* focusGained() method. The JTextField at the bottom of the *//* JFrame is used to indicate which component has focus. */ class FocusHandler extends FocusAdapter { public void focusGained(FocusEvent event) { JTextField tf = (JTextField)event.getComponent(); jtf3.setText(tf.getName()+" has focus"); } } public static void main(String args[]) { FocusAdapterDemo demo = new FocusAdapterDemo(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -