Visual Editor そのままのコーディング |
|
|
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JTextField;
import java.awt.Rectangle;
import javax.swing.JButton;
public class EnterKeyBase extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JTextField jTextField1 = null;
private JTextField jTextField2 = null;
private JTextField jTextField3 = null;
private JButton jButton = null;
/**
* This method initializes jTextField1
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField1() {
if (jTextField1 == null) {
jTextField1 = new JTextField();
jTextField1.setBounds(new Rectangle(122, 77, 184, 27));
jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent e) {
System.out.println("jTextField1");
// lf
if ( e.getKeyCode() == 10 ) {
// システムが決めた次のフィールド
jTextField1.transferFocus();
}
}
});
}
return jTextField1;
}
/**
* This method initializes jTextField2
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField2() {
if (jTextField2 == null) {
jTextField2 = new JTextField();
jTextField2.setBounds(new Rectangle(122, 126, 184, 27));
jTextField2.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent e) {
System.out.println("jTextField2");
// lf
if ( e.getKeyCode() == 10 ) {
// システムが決めた次のフィールド
jTextField2.transferFocus();
}
}
});
}
return jTextField2;
}
/**
* This method initializes jTextField3
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField3() {
if (jTextField3 == null) {
jTextField3 = new JTextField();
jTextField3.setBounds(new Rectangle(122, 175, 184, 27));
jTextField3.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent e) {
System.out.println("jTextField3");
// lf
if ( e.getKeyCode() == 10 ) {
// 最初のフィールドに戻る
jTextField1.requestFocus();
}
}
});
}
return jTextField3;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(120, 23, 185, 28));
jButton.setText("初期フォーカスは↓");
}
return jButton;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
SwingUtilities.invokeLater(new Runnable() {
public void run() {
EnterKeyBase thisClass = new EnterKeyBase();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This is the default constructor
*/
public EnterKeyBase() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(431, 286);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent e) {
System.out.println("windowOpened()");
// 初期フォーカス
jTextField1.requestFocus();
}
});
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJTextField1(), null);
jContentPane.add(getJTextField2(), null);
jContentPane.add(getJTextField3(), null);
jContentPane.add(getJButton(), null);
}
return jContentPane;
}
} // @jve:decl-index=0:visual-constraint="10,10"
| |
|
|
|
LboxText.java
|
package testcontrol;
import javax.swing.*;
public class LboxText extends JTextField {
private java.awt.Component _target = null;
// コンストラクタ
public LboxText() {
Initialize();
}
// 初期化
private void Initialize() {
this.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent e) {
System.out.println("LboxText");
if ( e.getKeyCode() == 10 ) {
if ( ((LboxText)e.getSource())._target == null ) {
LboxText.this.transferFocus();
}
else {
LboxText.this._target.requestFocus();
}
}
}
});
}
public void ChangeNext( java.awt.Component target ) {
this._target = target;
}
}
| |
|
LboxText.this の使い方に注意。
この場所で、ただの this は、java.awt.event.KeyAdapter のインスタンス。
EnterKey.java
|
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import testcontrol.LboxText;
import javax.swing.JTextField;
import java.awt.Rectangle;
import javax.swing.JButton;
public class EnterKey extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private LboxText jTextField1 = null;
private LboxText jTextField2 = null;
private LboxText jTextField3 = null;
private JButton jButton = null;
/**
* This method initializes jTextField1
*
* @return testcontrol.LboxText
*/
private LboxText getJTextField1() {
if (jTextField1 == null) {
jTextField1 = new LboxText();
jTextField1.setBounds(new Rectangle(122, 77, 184, 27));
}
return jTextField1;
}
/**
* This method initializes jTextField2
*
* @return testcontrol.LboxText
*/
private LboxText getJTextField2() {
if (jTextField2 == null) {
jTextField2 = new LboxText();
jTextField2.setBounds(new Rectangle(122, 126, 184, 27));
}
return jTextField2;
}
/**
* This method initializes jTextField3
*
* @return testcontrol.LboxText
*/
private LboxText getJTextField3() {
if (jTextField3 == null) {
jTextField3 = new LboxText();
jTextField3.setBounds(new Rectangle(122, 175, 184, 27));
}
return jTextField3;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(120, 23, 185, 28));
jButton.setText("初期フォーカスは↓");
}
return jButton;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
SwingUtilities.invokeLater(new Runnable() {
public void run() {
EnterKey thisClass = new EnterKey();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This is the default constructor
*/
public EnterKey() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(431, 286);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent e) {
System.out.println("windowOpened()");
jTextField1.requestFocus();
}
});
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJTextField1(), null);
jContentPane.add(getJTextField2(), null);
jContentPane.add(getJTextField3(), null);
jContentPane.add(getJButton(), null);
}
jTextField3.ChangeNext(jTextField1);
return jContentPane;
}
} // @jve:decl-index=0:visual-constraint="10,10"
| |
|
|
|