有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

用户界面JAVA GUI标签输出错误

import java.awt.EventQueue;

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

public class Stock {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Stock window = new Stock();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Stock() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblBrickStock = new JLabel("10");
        lblBrickStock.setBounds(48, 62, 46, 14);
        frame.getContentPane().add(lblBrickStock);

        JButton btnNewButton = new JButton("Bricks");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int bricks = Integer.parseInt(lblBrickStock.getText());
                bricks--;
                if (bricks <= 10) {
                    lblBrickStock.setText(String.valueOf(bricks));
                }
            }
        });
        btnNewButton.setBounds(38, 28, 89, 23);
        frame.getContentPane().add(btnNewButton);


    }

}

我创建了这个股票计划,它是我正在创建的未来计划的原型。这个程序的作用是当你按下按钮时,标签上的数字会减少。我不能做的是,在标签中,我希望它说“剩下10个”之类的话,而只是让数字减少。它只适用于数字,但当我添加文本时,会收到大量错误。有没有办法解决这个问题,或者我只需要使用一个单独的标签


共 (2) 个答案

  1. # 1 楼答案

    您可以使用实例成员counter来跟踪数字,而不是从标签文本中获取当前值

    public class Stock{
        private int counter = 10;
        ...
    }   
    

    你的行动听众可能是:

    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            counter ;
            if (counter <= 10) {
                lblBrickStock.setText(counter + " remaining");
            }
        }
    });
    

    这样,您就不必将lblBrickStock.getText解析为一个数值,并且如果这不再是一个数值,也不必冒得到解析异常的风险

    下面是一个小剪报,展示了如何在匿名内部类(ActionListener)中使用变量

    public class TestFrame extends JFrame{
        private int counter = 10;
    
        public TestFrame(){
            this.setTitle("Labo - TestFrame");
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    
            this.getContentPane().add(new JButton(new AbstractAction() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println(counter );
                }
            }));
    
            this.setVisible(true);
            this.pack();
        }
    }
    

    我点击了3次:

    10
    9
    8

  2. # 2 楼答案

    问题在于:

    int bricks = Integer.parseInt(lblBrickStock.getText());
    

    您尝试解析为包含字符串的整数值。要避免异常,可以使用:int bricks = Integer.parseInt(lblBrickStock.getText().replaceAll("\\D+",""));

    但更好的办法是使用静态计数器(正如@AxelH在评论中提到的),而不是从JLabel获取值