所以我正在嘗試制作一個(gè)非?;镜膽?yīng)用程序,當(dāng)您點(diǎn)擊一個(gè)按鈕時(shí),它會(huì)計(jì)數(shù)(每秒將一個(gè)數(shù)字添加到 0),然后將其顯示在標(biāo)簽和文本字段中。因?yàn)槲沂褂玫氖?doubles,所以文本輸出看起來像這樣,例如:2.34555555555555。我希望它在點(diǎn)后只顯示 2 個(gè)數(shù)字。我怎么能那樣做?這是我的代碼,正如我所說的只是一個(gè)按鈕、標(biāo)簽和文本字段以及一個(gè)小計(jì)時(shí)器: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(); } }); }}提前致謝。
格式化 JLabel/JTextField 文本?
函數(shù)式編程
2021-08-13 15:37:57