【问题标题】:Jlabel not updating when setText is called调用 setText 时 Jlabel 不更新
【发布时间】:2021-05-10 07:28:40
【问题描述】:

虽然构建一个简单的货币转换器应用程序 setText 并没有在 JLabel 中设置值。(我在 windows 中使用 eclipse ide)。我已经调用 Action Listener 来设置按钮,并且还在 parseInt 的帮助下将 getText 转换为 int .

我的代码如下。

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;


public class CurrencyConverter implements ActionListener {
    JButton button1,button2;
    JLabel display2,display3;
    JTextField display;
    
    public CurrencyConverter() {
    var frame=new JFrame();
    frame.setLayout(null);
    frame.setBounds(300, 300, 400, 300);
    frame.getContentPane().setBackground(Color.BLACK);

    
    
    display=new JTextField();
    display.setBounds(50, 30, 300, 50);
    display.setBackground(Color.yellow);
    display.setForeground(Color.black);
    frame.add(display);
    
    
    
    button1=new JButton("TO INR");
    button1.setBounds(50, 100, 135, 50);
    frame.add(button1);
    
    button2=new JButton("TO USD");
    button2.setBounds(215, 100, 135, 50);
    frame.add(button2);
    
    display2=new JLabel();
    display2.setBounds(50, 170, 300, 50);
    display2.setBackground(Color.GREEN);
    display2.setForeground(Color.BLACK);
    display2.setOpaque(true);
    frame.add(display2);
    
    
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
public static void main(String[] args) {
    new CurrencyConverter();
}
@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource()==button1) {
        int noInDisplay=Integer.parseInt(display.getText())*70;
        display2.setText(""+noInDisplay);
    }
    else if(e.getSource()==button2) {
        int noInDisplay=Integer.parseInt(display.getText())/70;
        display2.setText(""+noInDisplay);
        }
}
}

【问题讨论】:

    标签: java swing actionlistener jlabel settext


    【解决方案1】:

    您在JLabel 上看不到任何可见更新的主要问题是,从未在按钮上添加ActionListener,因此从未调用过actionPerformed()

    要解决此问题,您应该在 CurrencyConverter() 构造函数中添加这两行,以便在您的按钮上添加 ActionListener

    button1.addActionListener(this);
    button2.addActionListener(this);
    

    您的代码中的一些旁注:

    • swing中一般不建议使用null布局。查看Visual Guide to Layout Managers 了解挥杆布局管理器的概述。使用布局管理器将为您完成调整组件大小和布局的工作。因此您不必自己手动设置每个组件的大小和边界。

    • 您可以为每个JButton 实现一个匿名ActionListener,而不是检查actionPerformed() 中的操作源。这样,您在组件和操作之间就有了清晰的映射。例如

      button1.addActionListener(new ActionListener() {
      
          @Override
          public void actionPerformed(ActionEvent e) {
              int noInDisplay = Integer.parseInt(display.getText()) * 70;
              display2.setText("" + noInDisplay);
          }
      });
      
    • 下一步应该考虑某种输入验证。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-22
      • 1970-01-01
      • 2015-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多