gpt4 book ai didi

java - String.format 不适用于计时器应用程序中的参数

转载 作者:太空宇宙 更新时间:2023-11-04 07:32:47 26 4
gpt4 key购买 nike

我正在尝试为我的项目设计一个基于计时器的应用程序(在线测试)。我必须在标签中显示剩余时间。所以我使用了 String.format。但 Eclipse 显示这是一个错误:

The method format(String, Object[]) in the type String is not applicable for the arguments (String, int)).

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Formatter;
import javax.swing.*;


public class RadioButton {
JFrame frame=new JFrame("RadioRadio");
JLabel timerl = new JLabel("Press Button to start");
JPanel butp = new JPanel();
JButton button = new JButton("Start Exam");
Timer mytimer;
String ss="Time Remaining %02d Seconds!";
int elapsedSeconds = 0;
int total=10;
public void radioButton()
{
frame.setSize(300,300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(butp);
butp.add(button);
butp.add(timerl);

frame.setVisible(true);
button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {
if (mytimer != null && mytimer.isRunning()) {
mytimer.stop();
mytimer = null;
timerl.setText("Exam Terminated");
} else {

mytimer = new Timer(1000, new TimerListener());
mytimer.start();
String t = String.format(ss, total);
timerl.setText(t);
}
}
});
}
private class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
elapsedSeconds++;

if (elapsedSeconds == total) {
mytimer.stop();
timerl.setText("Time Up");
} else {
String t = String.format(ss, total - elapsedSeconds);
timerl.setText(t);
}
}
}
public static void main(String args[])
{
RadioButton r=new RadioButton();
r.radioButton();
}


}

最佳答案

请不要告诉任何人

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class MyRadioButton {

private JFrame frame = new JFrame("RadioRadio");
private JLabel timerl = new JLabel("Press Button to start");
private JPanel butp = new JPanel();
private JButton button = new JButton("Start Exam");
private Timer mytimer;
private String ss = "Time Remaining %02d Seconds!";
private int elapsedSeconds = 0;
private int total = 10;

public MyRadioButton() {
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (mytimer != null && mytimer.isRunning()) {
mytimer.stop();
elapsedSeconds = 0;
timerl.setText("Exam Terminated");
} else {
mytimer = new Timer(1000, new TimerListener());
mytimer.start();
String t = String.format(ss, total);
timerl.setText(t);
}
}
});
butp.add(button);
butp.add(timerl);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(butp);
frame.setSize(400, 300);
frame.setVisible(true);
}

private class TimerListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
elapsedSeconds++;
if (elapsedSeconds == total) {
mytimer.stop();
elapsedSeconds = 0;
timerl.setText("Time Up");
} else {
String t = String.format(ss, total - elapsedSeconds);
timerl.setText(t);
}
}
}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MyRadioButton r = new MyRadioButton();
}
});
}
}

关于java - String.format 不适用于计时器应用程序中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17441022/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com