[英]How can I inform the class which have an instance of a GUI builder class, when the JButton ActionEvent performed
Ok, here is my problem. 好的,这是我的问题。 Class B is a class that build a GUI ,which has a textField and button. 类B是构建GUI的类,该GUI具有textField和按钮。 class A has an instance of class B.Now I enter some value in the textfield, when I click the button, in class AI want to print out the value I just enter in the textfield, how can I achieve that? A类有B类的实例。现在我在文本字段中输入一些值,当我单击按钮时,在AI类中要打印出我刚刚在文本字段中输入的值,我该如何实现?
Code below may better explain what I want to achieve: 下面的代码可能会更好地解释我想要实现的目标:
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?)
}
}
Can anybody help me finish this little program? 有人可以帮我完成这个小程序吗? Thanks in advance! 提前致谢!
You need to expose the value in text field with a method in class B. Then class A can call that method. 您需要使用类B中的方法公开文本字段中的值。然后,类A可以调用该方法。 What it actually sounds like though is that class A (or something else) should be a ActionListener
for your button. 实际上听起来像是类A(或其他类)应该是按钮的ActionListener
。
However, a bigger problem is that you don't have a text field you just have a label in class B. This code is a good reason why you shouldn't use a GUI builder, especially when learning Swing. 但是,更大的问题是您没有文本字段,而在类B中只有标签。此代码是您不应该使用GUI构建器的一个很好的理由,尤其是在学习Swing时。
Some reading: http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html http://docs.oracle.com/javase/tutorial/uiswing/events/ 一些阅读: http : //docs.oracle.com/javase/tutorial/uiswing/components/textfield.html http://docs.oracle.com/javase/tutorial/uiswing/events/
I often make an "App" class that ties all my GUI-builder-built components together. 我经常制作一个“ App”类,将所有由GUI生成器构建的组件联系在一起。 Any GUI builder worth anything lets you add getters to the generated source code. 任何有价值的GUI构建器都可以让您将getter添加到生成的源代码中。 Add some getters to the GUI-built components to retrieve key elements of the GUI, then let the App class use the getters to interact with the components as necessary. 将一些吸气剂添加到GUI构建的组件中以检索GUI的关键元素,然后让App类根据需要使用吸气剂与组件进行交互。 This won't win any MVC/MVVM/MVP design awards, but it gets the job done, which ought to count for something. 这不会获得任何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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.