トップ
新規
一覧
単語検索
最終更新
ヘルプ
ログイン
アールメカブ
JTextFieldの簡単な例
をテンプレートにして作成
開始行:
[[Eclipse]]
import java.awt.Headl
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;essException;
public class TestTextField extends JFrame
implements ActionListener {
JTextField tf1;
JTextField tf2;
public TestTextField() throws HeadlessException {
super();
// TODO Auto-generated constructor stub
setSize(300,200);
setTitle("Swing サンプル");
setLocation(210,415);
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
tf1 = new JTextField("");
tf2 = new JTextField("");
tf1.addActionListener(this);
tf2.addActionListener(this);
p.add(tf1);
p.add(tf2);
getContentPane().add(p);
setVisible(true);
}
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getSource() == tf1){
tf1.setText("textfield1");
}else if(arg0.getSource() == tf2){
tf2.setText("textfield2");
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new TestTextField();
}
終了行:
[[Eclipse]]
import java.awt.Headl
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;essException;
public class TestTextField extends JFrame
implements ActionListener {
JTextField tf1;
JTextField tf2;
public TestTextField() throws HeadlessException {
super();
// TODO Auto-generated constructor stub
setSize(300,200);
setTitle("Swing サンプル");
setLocation(210,415);
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
tf1 = new JTextField("");
tf2 = new JTextField("");
tf1.addActionListener(this);
tf2.addActionListener(this);
p.add(tf1);
p.add(tf2);
getContentPane().add(p);
setVisible(true);
}
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getSource() == tf1){
tf1.setText("textfield1");
}else if(arg0.getSource() == tf2){
tf2.setText("textfield2");
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new TestTextField();
}
ページ名: