格式化 JLabel/JTextField 文本?

Formatting JLabel/JTextField text?

所以我正在尝试制作一个非常基本的应用程序,当您点击一个按钮然后将其显示在标签和文本字段中时,它会计数(每秒将一个数字添加到 0)。由于我使用的是双打文本输出看起来像这样,例如:2.34555555555555。我希望它在点后仅显示 2 个数字。我该怎么做?

这是我的代码,正如我所说,只有一个按钮、标签和文本字段以及一个小计时器:

public class Frame1 {

    private JFrame frame;
    private JTextField txtbox1;
    double fiz = 1500;
    double mpfiz = fiz/60/60;
    int secondsPassed = 0;
    double penz = 0;
    Timer timer = new Timer();

    TimerTask task = new TimerTask() {
        public void run() {
            secondsPassed++;
            penz += mpfiz;
            lbpenz.setText("Ennyi: "+ penz);
            txtbox1.setText("Ennyi: " + penz);
        }};
        private JLabel lbpenz;


        public void start() {
            timer.scheduleAtFixedRate(task, 1000, 1000);
        }

        public static void main(String[] args) {
            Frame1 peldany = new Frame1();
            peldany.start();

            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        Frame1 window = new Frame1();
                        window.frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

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

        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {

            JButton btnStart = new JButton("Start!");
            btnStart.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    start();
                }
            });
        }
}

提前致谢。

试试下面的代码

public void run() {
    DecimalFormat df = new DecimalFormat(".##");

    secondsPassed++;
    penz += mpfiz;
    lbpenz.setText("Ennyi: "+ df.format(penz));
    txtbox1.setText("Ennyi: " + df.format(penz));
}};