如何在 java 事件后更新帧?

How to update frames after event in java?

背景:我是 Java 的新手。

我正在尝试构建一个有 4 个框架的小程序,一个向用户询问两个数字,然后是三个不同的框架,(1) 显示总和,(2) 显示差值,(3) 显示显示数字的产品。用户单击 'Calculate' 按钮后,我能够在所需变量 (i,j) 中获取值 我在其他框架中显示,但我不知道其他框架如何在 [= 之后显示更新后的值17=] 被按下。相反,在我当前的代码中,它们一直显示默认值。

import java.awt.*;
import javax.swing.JApplet;
import javax.swing.*;
import java.awt.event.*;

public class Applet extends JFrame implements ActionListener 
 {

 JTextField txtdata,txdatas;
    JButton calbtn = new JButton("Calculate");
    String c,d;
    int i=0,j=0;
public Applet() {

    JTabbedPane tabbedPane = new JTabbedPane ();
    JPanel tabonepanel = new JPanel();
    JPanel tabtwopanel = new JPanel ();
    JPanel tabtthreepanel = new JPanel();
    JPanel tabfourpanel = new JPanel();
    JLabel sumlabel = new JLabel ("The Sum is "+(i+j)); 


    tabonepanel.add(sumlabel);
    tabtwopanel.add(new JLabel("The Difference is "+ (i-j)));
    tabtthreepanel.add(new JLabel("The Product is "+ (i*j)));
    tabfourpanel.add(new JLabel("Enter two numbers"));
    txtdata= new JTextField();
    txdatas=new JTextField();
    tabfourpanel.add(txtdata);
    tabfourpanel.add(txdatas);
    tabfourpanel.add(calbtn); 
    calbtn.addActionListener(this);

    tabbedPane.addTab("Enter",tabfourpanel);
    tabbedPane.addTab("Sum",tabonepanel);
    tabbedPane.addTab("Dif",tabtwopanel);
    tabbedPane.addTab("Mul",tabtthreepanel);  

    this.getContentPane().add(tabbedPane);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setTitle("Tabbed Frames Demo");
    this.setVisible(true);
    this.setSize(300,450);

}

  public void actionPerformed(ActionEvent e)
{
    if (e.getSource() == calbtn) {
        c =   txtdata.getText()  ; 
        d =   txdatas.getText() ;
        i = Integer.parseInt(c);
        j = Integer.parseInt(c);




    }

}
   public static void main(String[] args)
 { 
  Applet x = new Applet();
 }
     }

enter code here您的和、差和乘积标签应声明为实例变量:

JTextField txtdata,txdatas;
JButton calbtn = new JButton("Calculate");
JLabel sumLabel;
JLabel differenceLabel;
JLabel productLabel;
String c,d;
int i=0,j=0;

然后在构造函数中创建它们:

sumLabel = new JLabel("The Sum is "+(i+j));
differenceLabel = new JLabel("The Difference is "+(i-j));
productLabel = new JLabel("The Product is "+(i*j));

然后在您的 actionPerformed 方法中,更改标签文本:

   sumLabel.setText("("The Sum is "+(i+j));
   //etc.

请注意,如果标签字符串的大小发生变化,您可能需要重新验证标签的父级。

I am trying to build a small applet

它不是 "applet"。您正在构建和应用。不要调用您的 class "Applet",因为 AWT class 的名称令人困惑。给你class一个更好描述的名字

which has 4 frames,

您有 1 个 JFrame。您在 JTabbedPane 中有 4 个选项卡。

After user clicks on 'Calculate' button I'm able to get values inside the required variables (i,j)

因为您对这些变量使用了 getText() 方法。

but I don't know how will other frames show the updated values after 'calculate button' is pressed.

您使用上面的 "getter" 方法获取值。所以可以使用"setter"的方法来设置值变化时组件上的文字

JLabel sumlabel = new JLabel ("The Sum is "+(i+j)); 

上面的标签(以及您想要更改文本的任何其他标签)需要在 class 中定义为实例变量,就像您定义文本字段(txtdata 和 txdatas)一样作为实例变量。

tabtwopanel.add(new JLabel("The Difference is "+ (i-j)));
tabtthreepanel.add(new JLabel("The Product is "+ (i*j)));

这些也需要是实例变量。文本没有改变,因为 i, j 的值改变了。每次计算都需要手动重置文本。