[英]How do I modify the text of a JTextfield from a different class? (Java)
I'm trying to make a calculator as practice (I'm pretty bad at Java), but now I face a problem that I can't fix.我正在尝试制作一个计算器作为练习(我在 Java 方面非常糟糕),但现在我面临一个我无法解决的问题。 I have a frame with a CardLayout, and on one of those cards is a JTextfield.我有一个带有 CardLayout 的框架,其中一张卡片上有一个 JTextfield。 I made the textfield in my main class: JTextField textfield = new JTextField();
我在我的主要 class 中创建了文本字段: JTextField textfield = new JTextField();
, and did some things with it: ,并用它做了一些事情:
textfield.setBounds(50, 130, 380, 60);
textfield.setEditable(false);
textfield.setFont(font);
Now, I want to modify the text on the textfield from a different class (which makes the calculator work): main.textfield.setText(main.textfield.getText() + "ans");
现在,我想从不同的 class 修改文本字段上的文本(这使计算器工作): main.textfield.setText(main.textfield.getText() + "ans");
, but that doesn't work. ,但这不起作用。 I am trying to modify the text in an ActionPerformed method.我正在尝试修改 ActionPerformed 方法中的文本。 Does anyone know what I am doing wrong?有谁知道我做错了什么?
I tried doing it via a method in my main class, and repainting and revalidating the frame and the panel, which both didn't work.我尝试通过我的主要 class 中的一种方法进行此操作,并重新绘制和重新验证框架和面板,但均无效。
@DanielJunglas asked for more information so here we go:p. @DanielJunglas 询问更多信息,所以我们在这里 go:p。 I'll all the code that could possibly have effect on my problem.我将列出可能对我的问题产生影响的所有代码。
frame.setBounds(700, 250, 500, 700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setResizable(false);
contPanel.setLayout(cards); //set the cardlayout to the container panel
contPanel.setBounds(0, 0, 500, 700);
createBegPanel(); //creating the panels
createNorPanel();
createTriPanel();
//createEquPanel();
contPanel.add(beginPanel, "p1");
contPanel.add(normalCalcPanel, "p2");
contPanel.add(triCalcPanel, "p3");
//contPanel.add(equCalcPanel, "p4");
cards.show(contPanel, "p1");
frame.add(contPanel);
frame.setVisible(true);
public void createNorPanel() { //these are the things i do with the textfield
textfield.setBounds(50, 130, 380, 60);
textfield.setEditable(false);
textfield.setFont(font);
public void actionPerformed(ActionEvent e) {
JButton source = (JButton)e.getSource();
main.textfield.setText("test");
So any button is pressed, it should set the text of the textfield to 'text', but it doesnt.所以按下任何按钮,它应该将文本字段的文本设置为“文本”,但它没有。 Is this enough info?这是足够的信息吗?
Thanks in advance!提前致谢!
I guess you never call actionPerformed
.我猜你永远不会打电话给actionPerformed
。
If you have a button then you need to add an ActionListener
to it that implements the method actionPerformed(ActionEvent e)
.如果你有一个按钮,那么你需要向它添加一个实现方法actionPerformed(ActionEvent e)
的ActionListener
。
It looks like you have a class that implements an ActionListener
(which is generally not a good solution).看起来您有一个实现ActionListener
的 class (这通常不是一个好的解决方案)。 In that case you have to add this
as ActionListener
to the button.在这种情况下,您必须将this
作为ActionListener
添加到按钮中。
JButton b = new JButton();
b.addActionListener(this);
A better solution would be to add a separate ActionListener
to each button.更好的解决方案是为每个按钮添加一个单独的ActionListener
。
JButton b = new JButton();
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
main.textfield.setText("test");
}
});
ActionListener
is a functional interface (it has only one method) wich means you could also use a Lambda expression. ActionListener
是一个功能接口(它只有一个方法),这意味着您也可以使用 Lambda 表达式。
b.addActionListener(e -> main.textfield.setText("test"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.