运行 不同的 JButton ActionListener class
Run JButton ActionListener in different class
我正在尝试 运行 我的 JButton ActionListener 在与我的 GUI 不同的 class 中。所以我让按钮工作,但我不能 "access" TextFields,当我想访问它们时它们只是 "empty"。
这就是我的 GUI Class:
public class CaeserGUI extends JFrame {
JPanel contentPane;
private JTextField txt_input;
private JTextField number_input;
private JButton btn_translate;
private JTextArea txt_output;
private JTextField txt_input2;
private JTextField number_input2;
private JTextArea txt_output2;
/**
* Launch the application.
* Author: Paul Viehmann
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CaeserGUI frame = new CaeserGUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CaeserGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
txt_input = new JTextField();
txt_input.setBounds(10, 38, 363, 20);
contentPane.add(txt_input);
txt_input.setColumns(10);
number_input = new JTextField();
number_input.setBounds(383, 38, 41, 20);
contentPane.add(number_input);
number_input.setColumns(10);
JTextPane txtpnZubersetzenderSatz = new JTextPane();
txtpnZubersetzenderSatz.setText("Zu \u00FCbersetzender Satz:");
txtpnZubersetzenderSatz.setBounds(10, 11, 126, 20);
contentPane.add(txtpnZubersetzenderSatz);
JTextPane txtpnVerschiebung = new JTextPane();
txtpnVerschiebung.setText("Verschiebung:");
txtpnVerschiebung.setBounds(349, 11, 75, 20);
contentPane.add(txtpnVerschiebung);
txt_output = new JTextArea();
txt_output.setBounds(10, 69, 414, 40);
contentPane.add(txt_output);
JTextPane txtpnZuDechiffrierenderSatz = new JTextPane();
txtpnZuDechiffrierenderSatz.setText("Zu dechiffrierender Satz:");
txtpnZuDechiffrierenderSatz.setBounds(10, 120, 126, 20);
contentPane.add(txtpnZuDechiffrierenderSatz);
txt_input2 = new JTextField();
txt_input2.setColumns(10);
txt_input2.setBounds(10, 147, 363, 20);
contentPane.add(txt_input2);
number_input2 = new JTextField();
number_input2.setColumns(10);
number_input2.setBounds(383, 147, 41, 20);
contentPane.add(number_input2);
JTextPane textPane_1 = new JTextPane();
textPane_1.setText("Verschiebung:");
textPane_1.setBounds(349, 120, 75, 20);
contentPane.add(textPane_1);
txt_output2 = new JTextArea();
txt_output2.setBounds(10, 176, 414, 40);
contentPane.add(txt_output2);
ActionListener actionListener = new ButtonListener(number_input, number_input2, btn_translate, txt_output, txt_input, txt_input2, txt_output2);
btn_translate = new JButton("\u00DCbersetzen");
btn_translate.addActionListener(actionListener);
btn_translate.setBounds(10, 227, 414, 23);
contentPane.add(btn_translate);
}
}
那是我的听众 class:
public class ButtonListener implements ActionListener{
private JTextField txt_input;
private JTextField number_input;
private JButton btn_translate;
private JTextArea txt_output;
private JTextField txt_input2;
private JTextField number_input2;
private JTextArea txt_output2;
public ButtonListener(JTextField txt_input, JTextField number_input,
JButton btn_translate, JTextArea txt_output,
JTextField txt_input2, JTextField number_input2,
JTextArea txt_output2){
this.txt_input = txt_input;
this.number_input = number_input;
this.btn_translate = btn_translate;
this.txt_output = txt_output;
this.txt_input2 = txt_input2;
this.number_input2 = number_input2;
this.txt_output2 = txt_output2;
}
public void actionPerformed(ActionEvent e){
if(txt_input.getText().equals("")){
System.exit(0);
}else{
}
}
}
当我按下按钮时,即使我在 textField 中写了一些东西,我的程序也会退出。
我阅读了很多其他帖子,但无法正常工作。
感谢每一个回答。
您似乎以错误的顺序传递参数(据我所知,根据您的命名约定)
+---------------------------+--------------------------+
| ActionListener Parameters | The Order You're Passing |
+---------------------------+--------------------------+
| txt_input | number_input |
| number_input | number_input2 |
| btn_translate | btn_translate |
| txt_output | txt_output |
| txt_input2 | txt_input |
| number_input2 | txt_input2 |
| txt_output2 | txt_output2 |
+---------------------------+--------------------------+
这意味着,当您在 ActionListener
中检查 txt_input
的值时,您实际上是在查看 number_input
值……这让我尖叫起来
我的 "general" 建议是创建一个 interface
来提供 getter 和 setter,其名称比 "text" 和 "text2" 更好,因此您可以更好地区分它们意义。我会让 CaeserGUI
实现这个 interface
,然后将它传递给 ActionListener
,它只引用了上述 interface
的一个实例
避免使用 null
布局,像素完美布局是现代 ui 设计中的一种错觉。影响组件个体大小的因素太多,none 是您可以控制的。 Swing 旨在与核心的布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正
如果您认为我反应过度,当我 运行 您的代码在我的电脑上时会发生这种情况
看看第二个字段的"caret"是怎么坏的?
我建议使用 JLabel
s 为字段提供提示,而不是设置它们的文本,因为那只会让人感到困惑。有关详细信息,请参阅 How to Use Labels
像 JTextPane
这样的组件真正受益于包裹在 JScrollPane
中,这将允许用户在文本扩展到组件的视觉边界之外时看到文本。有关详细信息,请参阅 How to Use Scroll Panes
我正在尝试 运行 我的 JButton ActionListener 在与我的 GUI 不同的 class 中。所以我让按钮工作,但我不能 "access" TextFields,当我想访问它们时它们只是 "empty"。
这就是我的 GUI Class:
public class CaeserGUI extends JFrame {
JPanel contentPane;
private JTextField txt_input;
private JTextField number_input;
private JButton btn_translate;
private JTextArea txt_output;
private JTextField txt_input2;
private JTextField number_input2;
private JTextArea txt_output2;
/**
* Launch the application.
* Author: Paul Viehmann
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CaeserGUI frame = new CaeserGUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CaeserGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
txt_input = new JTextField();
txt_input.setBounds(10, 38, 363, 20);
contentPane.add(txt_input);
txt_input.setColumns(10);
number_input = new JTextField();
number_input.setBounds(383, 38, 41, 20);
contentPane.add(number_input);
number_input.setColumns(10);
JTextPane txtpnZubersetzenderSatz = new JTextPane();
txtpnZubersetzenderSatz.setText("Zu \u00FCbersetzender Satz:");
txtpnZubersetzenderSatz.setBounds(10, 11, 126, 20);
contentPane.add(txtpnZubersetzenderSatz);
JTextPane txtpnVerschiebung = new JTextPane();
txtpnVerschiebung.setText("Verschiebung:");
txtpnVerschiebung.setBounds(349, 11, 75, 20);
contentPane.add(txtpnVerschiebung);
txt_output = new JTextArea();
txt_output.setBounds(10, 69, 414, 40);
contentPane.add(txt_output);
JTextPane txtpnZuDechiffrierenderSatz = new JTextPane();
txtpnZuDechiffrierenderSatz.setText("Zu dechiffrierender Satz:");
txtpnZuDechiffrierenderSatz.setBounds(10, 120, 126, 20);
contentPane.add(txtpnZuDechiffrierenderSatz);
txt_input2 = new JTextField();
txt_input2.setColumns(10);
txt_input2.setBounds(10, 147, 363, 20);
contentPane.add(txt_input2);
number_input2 = new JTextField();
number_input2.setColumns(10);
number_input2.setBounds(383, 147, 41, 20);
contentPane.add(number_input2);
JTextPane textPane_1 = new JTextPane();
textPane_1.setText("Verschiebung:");
textPane_1.setBounds(349, 120, 75, 20);
contentPane.add(textPane_1);
txt_output2 = new JTextArea();
txt_output2.setBounds(10, 176, 414, 40);
contentPane.add(txt_output2);
ActionListener actionListener = new ButtonListener(number_input, number_input2, btn_translate, txt_output, txt_input, txt_input2, txt_output2);
btn_translate = new JButton("\u00DCbersetzen");
btn_translate.addActionListener(actionListener);
btn_translate.setBounds(10, 227, 414, 23);
contentPane.add(btn_translate);
}
}
那是我的听众 class:
public class ButtonListener implements ActionListener{
private JTextField txt_input;
private JTextField number_input;
private JButton btn_translate;
private JTextArea txt_output;
private JTextField txt_input2;
private JTextField number_input2;
private JTextArea txt_output2;
public ButtonListener(JTextField txt_input, JTextField number_input,
JButton btn_translate, JTextArea txt_output,
JTextField txt_input2, JTextField number_input2,
JTextArea txt_output2){
this.txt_input = txt_input;
this.number_input = number_input;
this.btn_translate = btn_translate;
this.txt_output = txt_output;
this.txt_input2 = txt_input2;
this.number_input2 = number_input2;
this.txt_output2 = txt_output2;
}
public void actionPerformed(ActionEvent e){
if(txt_input.getText().equals("")){
System.exit(0);
}else{
}
}
}
当我按下按钮时,即使我在 textField 中写了一些东西,我的程序也会退出。
我阅读了很多其他帖子,但无法正常工作。
感谢每一个回答。
您似乎以错误的顺序传递参数(据我所知,根据您的命名约定)
+---------------------------+--------------------------+
| ActionListener Parameters | The Order You're Passing |
+---------------------------+--------------------------+
| txt_input | number_input |
| number_input | number_input2 |
| btn_translate | btn_translate |
| txt_output | txt_output |
| txt_input2 | txt_input |
| number_input2 | txt_input2 |
| txt_output2 | txt_output2 |
+---------------------------+--------------------------+
这意味着,当您在 ActionListener
中检查 txt_input
的值时,您实际上是在查看 number_input
值……这让我尖叫起来
我的 "general" 建议是创建一个 interface
来提供 getter 和 setter,其名称比 "text" 和 "text2" 更好,因此您可以更好地区分它们意义。我会让 CaeserGUI
实现这个 interface
,然后将它传递给 ActionListener
,它只引用了上述 interface
避免使用 null
布局,像素完美布局是现代 ui 设计中的一种错觉。影响组件个体大小的因素太多,none 是您可以控制的。 Swing 旨在与核心的布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正
如果您认为我反应过度,当我 运行 您的代码在我的电脑上时会发生这种情况
看看第二个字段的"caret"是怎么坏的?
我建议使用 JLabel
s 为字段提供提示,而不是设置它们的文本,因为那只会让人感到困惑。有关详细信息,请参阅 How to Use Labels
像 JTextPane
这样的组件真正受益于包裹在 JScrollPane
中,这将允许用户在文本扩展到组件的视觉边界之外时看到文本。有关详细信息,请参阅 How to Use Scroll Panes