お世話になります。 Q1)下記のコードに於きまして、JTextFieldにマウスフォーカス が当りますと、その旨、System.out.println("JTextField_tt")と表示する方法をお教えください。 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.LineBorder; public class EObjectTest extends JFrame implements ActionListener { public static JButton b1; public static JButton b2; public static JTextField tt; static EObjectTest frame; public static void main(String args[]){ frame=new EObjectTest("AAAA"); frame.setVisible(true); } EObjectTest(String title) { setTitle(title); setBounds(100, 100, 300, 250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel p = new JPanel(); // ボタン作成・追加 b1 = new JButton("One"); b2 = new JButton("Two"); tt = new JTextField("aaa"); LineBorder border = new LineBorder(Color.RED, 2, true); tt.setBorder(border); tt.setColumns(15); p.add(b1); p.add(b2); p.add(tt); Container contentPane = getContentPane(); contentPane.add(p, BorderLayout.CENTER); // リスナ登録 b1.addActionListener(this); b2.addActionListener(this); tt.addActionListener(this); } public void actionPerformed(ActionEvent e) { // getSource() でイベントソースのオブジェクトを獲得し // Button クラスにキャストする JButton b = (JButton)e.getSource(); // JTextField ttt = (JTextField)e.getSource(); if (b==b1) { System.out.println("Oneのボタン"); } if (b==b2) { System.out.println("Twoのボタン"); } /* if (ttt==tt) { System.out.println("JTextField_tt"); } */ } } 以上
↧