简体   繁体   English

如何从不同的 class 修改 JTextfield 的文本? (爪哇)

[英]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.

相关问题 如何修改其他班级的arraylist? Java的 - How do I modify an arraylist from a different class? Java 对于JTextField,如何设置其他类的文本? - For a JTextField, how do I set text from another class? 如何从 JTextField 中检索文本? - how do I retrive a text, from a JTextField? Java:我如何从不同的 class 调用数组,我要修改它们吗? - Java: How do I call the array from different class, and I do I modify them? 如何从JTextField获取文本,转换为字符串并在其他java公共类中使用此字符串 - How to get text from JTextField, convert to a string, and use this string in a different java public class 如何从一个类中的JtextField输入到另一个类中的ArrayList的输入。 爪哇 - How do I get input from a JtextField in one class to an ArrayList in another class. Java 如何将文本输入从一个JTextField传递到另一个类中的变量? - How do I pass text input from one JTextField to a variable in another class? 如何从另一个面板对象更新JTextField文本 - How do I Update JTextField text from another panel object 如何绑定jtextfield以从Netbeans中的数据库中获取文本? - How do i bind a jtextfield to get text from the database in Netbeans? 如何将用户输入从 JTextfield 添加到文本文件 - How do I add user input from JTextfield to Text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM