当 JButton ActionEvent 执行时,我如何通知具有 GUI 生成器类实例的类



好的,这是我的问题。B类是构建GUI的类,它有一个文本字段和按钮。类 A 有一个类 B 的实例.现在我在文本字段中输入一些值,当我单击按钮时,在类 A 中我想打印出我刚刚在文本字段中输入的值,我该如何实现?

下面的代码可以更好地解释我想要实现的目标:

    public class A
    {
        B myB = new B();
        (when the JButton was clicked, 
         how can I get the new textfield value here?)
    }
    public class B
    {
        JLabel myLabel;
        JButton myButton;
        public B()
        {
            getContentPane().setLayout(null);
            myLabel = new JLabel();
            myLabel.setLocation(0,0);
            myLabel.setSize(100,30);
            myLabel.setBackground( new Color(-6710887) );
            myLabel.setText("");
            getContentPane().add(myLabel);
            myButton = new JButton();
            myButton.setLocation(0,50);
            myButton.setSize(100,30);
            myButton.setBackground( new Color(-16737895) );
            myButton.setText("Submit");
            getContentPane().add(myButton);
            myButton.addActionListener(this);
            setSize(400,400);
            setVisible(true);
            setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        public void actionPerformed(ActionEvent e)
        { 
              (how can I pass this "myLabel.getText()" value to class A when 
              this action performed?)
        }
    }

谁能帮我完成这个小程序?提前感谢!

您需要使用类 B 中的方法公开文本字段中的值。 然后类 A 可以调用该方法。 实际上听起来像是A类(或其他东西)应该是按钮的ActionListener

但是,更大的问题是您没有文本字段,您只有 B 类中的标签。 这段代码是你不应该使用 GUI 构建器的一个很好的理由,尤其是在学习 Swing 时。

一些阅读:http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.htmlhttp://docs.oracle.com/javase/tutorial/uiswing/events/

我经常创建一个"App"类,将所有 GUI 构建器构建的组件联系在一起。 任何有价值的 GUI 构建器都允许您将 getter 添加到生成的源代码中。 向 GUI 构建的组件添加一些 getter 以检索 GUI 的关键元素,然后让 App 类根据需要使用 getter 与组件进行交互。 这不会赢得任何MVC/MVVM/MVP设计奖,但它可以完成工作,这应该很重要。

public class App {
    private B _b;
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                App app = new App();
                app.run();
            }
        });
    }
    void run() {
        _b = new B();
        _b.getMainButton().addActionListener(new MainButtonListener());
        _b.setVisible(true);
    }
    private void handleMainButtonClicked() {
        String mainText = _b.getMainTextArea().getText();
        System.out.println("Button clicked; main text = " + mainText);
    }
    public class MainButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            handleMainButtonClicked();
        }
    }
}
public class B extends JFrame {
    private JPanel _contentPane;
    private JTextArea _jTextArea;
    private JButton _jButton;
    public B() {
        initComponents();
    }
    private void initComponents() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 400);
        _contentPane = new JPanel();
        setContentPane(_contentPane);
        _jTextArea = new JTextArea();
        _contentPane.add(_jTextArea, BorderLayout.CENTER);
        _jButton = new JButton("My Button");
        _contentPane.add(_jButton, BorderLayout.SOUTH);
    }
    public JButton getMainButton() {
        return _jButton;
    }
    public JTextComponent getMainTextArea() {
        return _jTextArea;
    }
}

最新更新