2017-06-24 97 views
0

我试图在与我的GUI不同的类中运行我的JButton ActionListener。所以我得到按钮的工作,但我不能“访问”TextFields,他们只是“空”,当我想要访问它们。在不同的类中运行JButton ActionListener

所以这是我的GUI类:

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); 
    } 
} 

这就是我的Listener类:

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{ 
     } 
    } 
} 

当我按下按钮,我的程序退出,即使我写在文本框的东西。

我读了很多其他帖子,但无法让它工作。

我很感谢每一个答案。

回答

1

你似乎在传递参数的顺序不正确(据我所知,根据您的命名约定)

+---------------------------+--------------------------+ 
| 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值......这让尖叫声问题,我

我的“一般”的建议是建立一个interface其提供getter和setter方法,比“文本”和“文本2”更好的名字,所以你可以更好地区分它们的含义。我不得不CaeserGUI实现这个interface,然后将它传递给ActionListener,历时一个参考实例的使用null布局说interface

避免,像素完美的布局是现代的UI设计中的错觉。影响组件的个体大小的因素太多,其中没有一个可以控制。 Swing旨在与布局经理一起工作,放弃这些将导致问题和问题的终结,您将花费越来越多的时间来尝试纠正

如果您认为我过度反应,这是当我在我的电脑上运行代码

Bad Layout

参见“插入符号”在第二场怎么坏了会发生什么?

我会推荐使用JLabel s来提示字段而不是设置他们的文本,因为这只是令人困惑。有关更多详细信息,请参阅How to Use Labels

JTextPane这样的组件真的受益于包装在JScrollPane中,这将允许用户在组件的可视边界之外展开时看到文本。见How to Use Scroll Panes更多细节

+0

TY,是的,我通过他们错了... d:但我会尝试的界面藏汉:d – itsPauV

+0

这不会是最后一次,你混淆参数:P – MadProgrammer

+0

谢谢你,这些标签工作方式更好:D – itsPauV

相关问题