Ich experimentiere JButton Aktion und ich versuche, ein Textfeld in Klasse Test2 mithilfe einer Schaltfläche in Klasse Test1 zu löschen. Hier ist der CodeÜbergeben von JButton-Aktion von einer Klasse zu JTextfield in einer anderen Klasse
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);
}
}
Im Moment, als ich den Knopf in Test1 Klasse klicken, wird das Textfeld in Test2 Klasse wird nicht gelöscht. Hoffe, Rat von allen Senioren hier zu haben. Lassen Sie es mich auch wissen, wenn meine Fragen irgendwelche Mängel haben. Ich danke dir sehr.
Hallo Ganesh, schätzen Ihre Beratung. Ich entferne die Instanziierung und ändere Reset() zu Reset(), aber ich kann immer noch nicht den JBUtton das Textfeld löschen. – lamchiomeng
Sie verwenden zwei Instant von 'Test2' Klasse, so dass Sie es eins machen müssen. Sie können das Update sehen. –