gpt4 book ai didi

java - 如何使用不同导入的双定时器

转载 作者:行者123 更新时间:2023-11-30 03:32:52 25 4
gpt4 key购买 nike

如何在同一个java文件中使用timer()和timer1(1000,actionListener)如果我导入java.util.timer,那么我会在timer1上得到错误,如果我不导入它,我会在另一个上得到错误。

有什么办法可以解决这个问题吗?

我使用timer()作为倒计时计划,并使用timer1来计算秒数直到我希望它停止

公共(public)类 MainFrame 扩展 JFrame {

JPanel mPanel;
JTextField outgoing, portField;
JTextArea incoming;
JButton sendButton;
ArrayList clientOutputStreams;
PrintWriter writer;
BufferedReader reader;
Socket socket;
Socket clientSocket;
String message;
int port;
JMenuBar myMenu;
JRadioButtonMenuItem portList1, portList2,
portList3, portList4;
JLabel lb_timer,
lb_bug;
java.util.Timer timerr;
javax.swing.Timer timer;
int counter,
cnt,
timerEvents;
Random random;
int x,y;
int tmClicked;
//int score;
JMenuItem score;
int point;


public void Start() {
setBounds(300, 80, 600, 600);
setResizable(true);
setTitle("FatLlama");
mainPanel();
addMenuBar();




setVisible(true);


}

public void mainPanel(){
mPanel = new JPanel();
getContentPane().add(mPanel);
mPanel.setBackground(Color.LIGHT_GRAY);
mPanel.setBounds(0,0,100,20);
mPanel.setLayout(null);
mPanel.setVisible(false);
setBackground(Color.WHITE);
ImageIcon imgThisImg = new ImageIcon("images/cookie.png");
lb_bug = new JLabel("here");
//lb_bug.setIcon(imgThisImg);
lb_bug.addMouseListener(new MouseClickHandler());
lb_bug.setSize(200,200);

random = new Random();
mPanel.add(lb_bug);
x = random.nextInt(500-50);
y = random.nextInt(500-50);
lb_bug.setLocation(x, y);

}
private class MouseClickHandler extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent event) {
System.out.println("bug clicked!");
x = random.nextInt(500-50);
y = random.nextInt(500-50);
lb_bug.setLocation(x, y);
tmClicked += 1;
score.setText("Score :" + Integer.toString(tmClicked));


setScore(tmClicked);
System.out.println(score.getText());
}
}
public void setScore(int tmClicked){
this.tmClicked = tmClicked;
}
public int getScore(){
return tmClicked;
}
private String createEventText() {
return String.format("%d seconds", getTimerEvents());
}

private Object getTimerEvents() {
return timerEvents;
}

public void addMenuBar() {
myMenu = new JMenuBar();

JMenuItem lb_timer = new JMenuItem("timer");
myMenu.add(lb_timer);
JMenuItem newAction = new JMenuItem("New game");
final JMenuItem srvStart = new JMenuItem();
srvStart.setText("Start");
myMenu.add(newAction);
score = new JMenuItem("Score");
myMenu.add(score);

ActionListener actListner = new ActionListener() {

public void actionPerformed(ActionEvent event) {
cnt += 1;
counter += 1;
revalidate();
System.out.println(cnt + " Seconds");

increaseTimerEvents();
lb_timer.setText(createEventText());

if(counter == 10){
System.out.println("Game Over");
mPanel.setVisible(false);
timer.stop();
revalidate();
repaint();

}else{

}
}

public void increaseTimerEvents() {
++timerEvents;
}
};

newAction.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
score.setText("Score: 0");
remainingTime(2);
System.out.println("start start start");
mPanel.setVisible(true);
revalidate();
repaint();

//timer = new Timer(1000, actListner);
timer.start();

}
});



JMenuItem quit = new JMenuItem("Quit");
myMenu.add(quit);


ActionListener quitListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("quit quit quit quit");
System.exit(0);
}
};

quit.addActionListener(quitListener);

setVisible(true);
setJMenuBar(myMenu);
}

public void remainingTime(int seconds) {
//Toolkit toolkit = Toolkit.getDefaultToolkit();
timerr = new Timer();
timerr.schedule(new RandomPos(), seconds * 1000);
}

class RandomPos extends TimerTask {
public void run() {
System.out.println("New pos");
//toolkit.beep();
x = random.nextInt(500-50);
y = random.nextInt(500-50);
lb_bug.setLocation(x, y);
remainingTime(2);
}
}

}

最佳答案

如果您对两者都有导入,则在使用它来解决歧义时需要完全限定类型。

new java.util.Timer()
new javax.swing.Timer(1000, actListner);

或者,您可以导入一个并完全限定另一个

import java.util.Timer
...
new Timer(); // <== will be java.util.Timer()

new javax.swing.Timer(1000, actListner); // <== have to fully qualify the swing variant

import javax.swing.Timer
...
new java.util.Timer(); // <== have to fully qualify the swing variant

new Timer(1000, actListner); // <== will be javax.swing.Timer()

关于java - 如何使用不同导入的双定时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28593448/

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