通过动作事件更改 JPanel 组件的属性

Change the properties of a JPanel component through an action event

我正在尝试设置一个系统,当我按下一个按钮时,JLabel 文本会发生变化,但我似乎无法让它工作。我已经通过 'system.out.println("test");' 测试了动作侦听器的工作原理。它工作正常,但在尝试更改 JComponent 文本时它不起作用。我已经搜索了答案,但没有找到有效的答案。

主要class:

package com.fcs.app;

public class A {
   public static void main(String args[]) {

    window w = new window();

    w.drawWindow();
   }
}

JFrame 和 JPanel class:

package com.fcs.app;

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

public class window extends JPanel {

JFrame jf = new JFrame();
JPanel jp = new JPanel();
JButton b1 = new JButton();
JTextField tf1 = new JTextField();
JTextField tf2 = new JTextField();
JLabel plus = new JLabel();
JLabel equals = new JLabel();
JLabel rt = new JLabel();

int Result = 10;

public void drawWindow() {

    //JFrame setup
    jf.setSize(400, 400);
    jf.setUndecorated(true);
    jf.setLayout(null);
    jf.setContentPane(jp);
    jf.setLocation(100, 100);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setVisible(true);

    //JPanel setup
    jp.setSize(400, 400);
    jp.setLocation(0, 0);
    jp.setBackground(Color.WHITE);
    jp.add(b1);
    jp.add(tf1);
    jp.add(tf2);
    jp.add(plus);
    jp.add(equals);
    jp.add(rt);
    jp.setLayout(null);
    jp.setVisible(true);

    //JButton setup
    b1.setFont(new Font("Times", Font.PLAIN, 15));
    b1.setText("Calculate!");
    b1.setSize(100, 40);
    b1.setLocation(150, 350);
    b1.addActionListener(new Listener());
    b1.setVisible(true);

    //TextField 1 setup
    tf1.setSize(120, 50);
    tf1.setLocation(140, 20);
    tf1.setFont(new Font("Times", Font.PLAIN, 25));
    tf1.setHorizontalAlignment(JTextField.CENTER);
    tf1.setVisible(true);

    //TextField 2 setup
    tf2.setSize(120, 50);
    tf2.setLocation(140, 120);
    tf2.setFont(new Font("Times", Font.PLAIN, 25));
    tf2.setHorizontalAlignment(JTextField.CENTER);
    tf2.setVisible(true);

    //Plus sign Setup
    plus.setSize(120, 50);
    plus.setLocation(140, 70);
    plus.setHorizontalAlignment(JLabel.CENTER);
    plus.setFont(new Font("Times", Font.PLAIN, 40));
    plus.setText("+");
    plus.setVisible(true);

    //Equals sign Setup
    equals.setSize(120, 50);
    equals.setLocation(140, 170);
    equals.setHorizontalAlignment(JLabel.CENTER);
    equals.setFont(new Font("Times", Font.PLAIN, 40));
    equals.setText("=");
    equals.setVisible(true);

    //Result text
    rt.setSize(400, 50);
    rt.setLocation(0, 250);
    rt.setHorizontalAlignment(JLabel.CENTER);
    rt.setFont(new Font("Times", Font.PLAIN, 60));
    rt.setText("");
    rt.setVisible(true);
}
}

ActionListener class:

package com.fcs.app;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Listener implements ActionListener {

window w = new window();

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    w.rt.setText("Test");
    }
}

您正在创建 Window 的新引用,例如 window w = new window(); 它将创建 window 的新实例,而您正在尝试更改新创建的 window。 尝试将您之前在 window class 中创建的 window 对象传递给 class 或 在 window class.

中实现匿名 ActionListener
b1.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    rt.setText("Test");
    }

}

});