gpt4 book ai didi

java - 计时器正在尝试转换为字符串

转载 作者:搜寻专家 更新时间:2023-11-01 02:44:35 26 4
gpt4 key购买 nike

谁能告诉我为什么 Timer 试图将 int 转换为 String。这是不断出现的错误。

CurrentTimePrinter.java:48: error: incompatible types: int cannot be converted to String time = new Timer(1000,listener);

CurrentTimePrinter.java:49: error: cannot find symbol time.start();

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.Date;
import java.util.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.BorderLayout;

public class CurrentTimePrinter extends JFrame
{
private JButton Exitbutton;
private JTextField textField;
private static final int FIELD_WIDTH = 10;
private static final int FRAME_WIDTH = 600;
private static final int FRAME_HEIGHT = 400;
private ActionListener listener;
public Timer time;

public CurrentTimePrinter()
{
//listener = new CtpListener();
createComponents();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
public void createComponents()
{
Color orange = new Color(255,165,0);
Font font = new Font("Times New Roman", Font.BOLD, 14);
textField = new JTextField(FIELD_WIDTH);
Exitbutton = new JButton("EXIT");
class CtpListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
textField.setText("" + new Date());
Date now = new Date();
System.out.println(now);
}
}
ActionListener listener = new CtpListener();
time = new Timer(1000,listener);
time.start();


Exitbutton.setFont(font);
Exitbutton.setForeground(Color.BLACK);
ExitButtonListener exitListener = new ExitButtonListener();
Exitbutton.addActionListener(exitListener);
JPanel panel1 = new JPanel();
time = new Timer(1000, listener);
time.start();

JPanel panel2 = new JPanel();
panel1.setBackground(orange);
panel1.add(Exitbutton);
panel2.add(textField);
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.add(panel1, BorderLayout.SOUTH);
contentPane.add(panel2, BorderLayout.CENTER);
setContentPane(contentPane);
}
class ExitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
}

public static void main(String[] args)
{
JFrame frame = new CurrentTimePrinter();
frame.setTitle("Current Time");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

}

最佳答案

您正在使用 java.util.Timer。您需要在此处使用 javax.swing.Timer

在导入中替换它,它应该是:

 import javax.swing.Timer;

关于java - 计时器正在尝试转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25755923/

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