1
JButtonのアクションを試していて、Test1クラスのボタンを使ってTest2クラスのテキストフィールドをクリアしようとしています。私はTest1のクラスのボタンをクリックすると、ここでは、コードあるクラスから別のクラスのJTextfieldにJButtonアクションを渡す
public class Test2 {
private JFrame frame;
private JTextField t1;
private JTextField t2;
/**
* Launch the application.
*/
public void start() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test2 window = new test2();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public test2() {
initialize();
}
public void Reset(){
t1 = new JTextField();
t1.setText("");
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t1 = new JTextField();
t1.setColumns(10);
t1.setText("Start");
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(35)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(t2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(t1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addContainerGap(281, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(38)
.addComponent(t1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(75)
.addComponent(t2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap(98, Short.MAX_VALUE))
);
frame.getContentPane().setLayout(groupLayout);
}
}
public class Test1 {
private JFrame frame;
/**
* Launch the application.
*/
static test1 window = new test1();
static test2 window2 = new test2();
private JTextField textField;
private JTextField ownText;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
window2.start();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
* @wbp.parser.entryPoint
*/
public test1() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String test ="";
window2.Reset(test);
}
});
frame.getContentPane().add(btnNewButton, BorderLayout.WEST);
ownText = new JTextField();
frame.getContentPane().add(ownText, BorderLayout.EAST);
ownText.setColumns(10);
}
}瞬間
で、Test2をクラス内のテキストフィールドはクリアされません。ここにすべての高齢者からアドバイスがあることを願っています。私の質問に欠点がある場合は、私にも教えてください。どうもありがとうございます。
こんにちはガネーシュよう
Test2
であなたのstart()
方法を作る、あなたのアドバイスに感謝。インスタンス化を取り除いてReset()をreset()に変更しますが、JBUttonにはまだテキストフィールドをクリアできません。 – lamchiomengあなたは 'Test2'クラスの2つの瞬間を使います。あなたはそれを1にする必要があります。あなたはアップデートを見ることができます。 –