我做了一个简单的点击测试应用程序(你可能已经在网上看到了一些,你有一个很大的按钮。一旦你点击它,一个10秒计时器启动,目标是点击按钮上的大多数时间)我认为这是练习Swing
API
和基本threads
的好方法。它只是基本的东西,我知道我可以使用API
作为计时器和东西,但让我们现在保持简单。
问题似乎是我无法修改JLabel
内部的setTime()
内部Thread
方法,该方法是从Timer
class
内Main
调用的class
1}}。
以下是package com.tremblayalex.clicktest;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main extends JFrame
{
private static final long serialVersionUID = 1L;
// Initialize Variables
private final JPanel contentPane = new JPanel();
private JLabel topText = new JLabel();
private JButton btnClick = new JButton();
private int clicks = 0;
private boolean isStarted = false;
public static void main(String[] args)
{
Main m = new Main();
m.setVisible(true);
}
public Main()
{
if (!isStarted)
{
initComponents();
registerEvents();
}
}
///////////////////////////////////////////////////////////////////////////////
// Initialize Components
///////////////////////////////////////////////////////////////////////////////
private void initComponents()
{
// JFrame Settings
setTitle("Input Box");
setSize(1000, 400);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(contentPane);
// Content Pane (BorderLayout)
contentPane.setLayout(new BorderLayout());
contentPane.add(topText, BorderLayout.NORTH);
contentPane.add(btnClick, BorderLayout.CENTER);
// Top Text
topText.setFont(new Font("Arial", Font.PLAIN, 16));
topText.setText("Click to Start");
// Button Click
btnClick.setFont(new Font("Arial", Font.BOLD, 18));
btnClick.setText("CLICK TO START");
}
///////////////////////////////////////////////////////////////////////////////
// Register Events
///////////////////////////////////////////////////////////////////////////////
private void registerEvents()
{
btnClick.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (isStarted == false)
{
isStarted = true;
new Thread(new Timer()).start();
}
clicks++;
btnClick.setText("[Clicks] " + clicks);
}
});
}
///////////////////////////////////////////////////////////////////////////////
// Getters and Setters
///////////////////////////////////////////////////////////////////////////////
public void setTime(double time)
{
System.out.println(time);
topText.setText("Time: " + time);
if (time >= 10)
{
if (clicks < 50)
{
topText.setText("Your average CPS is " + (clicks / 10) + "." + " Try to do better next time!");
} else if (clicks >= 50 || clicks < 100){
topText.setText("Your average CPS is " + (clicks / 10) + "." + " You are in the average!");
} else if (clicks >= 100 || clicks < 150){
topText.setText("Your average CPS is " + (clicks / 10) + "." + " You click very fast!");
} else if (clicks >= 150 || clicks < 250){
topText.setText("Your average CPS is " + (clicks / 10) + "." + " Clicking is your destiny!");
} else if (clicks >= 250){
topText.setText("Your average CPS is " + (clicks / 10) + "." + " Hack? :D");
} else {
topText.setText("Your average CPS is " + (clicks / 10) + "." + " Internal Error.");
}
isStarted = false;
btnClick.setText("CLICK TO RESTART");
}
}
}
Timer
:
class
这是package com.tremblayalex.clicktest;
public class Timer implements Runnable
{
private final Main main = new Main();
public void run()
{
for (double time = 0; time <= 10; time += 0.1)
{
main.setTime(time);
try {
Thread.sleep(100);
} catch (InterruptedException e)
{}
}
}
}
Timer
:
package com.tremblayalex.clicktest;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Main extends JFrame
{
private static final long serialVersionUID = 1L;
// Initialize Variables
private final JPanel contentPane = new JPanel();
private JLabel topText = new JLabel();
private JButton btnClick = new JButton();
private JButton btnRestart = new JButton();
private double clicks = 0;
private double time;
private boolean isStarted = false;
private Timer timer = new Timer(100, new ActionListener()
{
@Override
public void actionPerformed(ActionEvent paramActionEvent)
{
if (time < 100)
{
time++;
topText.setText("Time: " + (time / 10));
}
else {
if (clicks < 50)
{
topText.setText("Your average CPS is " + (clicks / 10) + " Try to do better next time!");
} else if (clicks >= 50 && clicks < 100){
topText.setText("Your average CPS is " + (clicks / 10) + " You are in the average!");
} else if (clicks >= 100 && clicks < 150){
topText.setText("Your average CPS is " + (clicks / 10) + " You click very fast!");
} else if (clicks >= 150 && clicks < 250){
topText.setText("Your average CPS is " + (clicks / 10) + " Clicking is your destiny!");
} else if (clicks >= 250){
topText.setText("Your average CPS is " + (clicks / 10) + " Hack? :D");
} else {
topText.setText("Your average CPS is " + (clicks / 10) + " Internal Error.");
}
timer.stop();
isStarted = false;
btnClick.setEnabled(false);
btnRestart.setEnabled(true);
}
}
});
public static void main(String[] args)
{
Main m = new Main();
m.setVisible(true);
}
public Main()
{
initComponents();
registerEvents();
}
///////////////////////////////////////////////////////////////////////////////
// Initialize Components
///////////////////////////////////////////////////////////////////////////////
private void initComponents()
{
// JFrame Settings
setTitle("Click Test");
setSize(1000, 400);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(contentPane);
// Content Pane (BorderLayout)
contentPane.setLayout(new BorderLayout());
contentPane.add(topText, BorderLayout.NORTH);
contentPane.add(btnClick, BorderLayout.CENTER);
contentPane.add(btnRestart, BorderLayout.SOUTH);
// Top Text
topText.setFont(new Font("Arial", Font.PLAIN, 16));
topText.setText("Click to Start");
// Button Click
btnClick.setFont(new Font("Arial", Font.BOLD, 18));
btnClick.setText("CLICK TO START");
// Button Restart
btnRestart.setFont(new Font("Arial", Font.PLAIN, 15));
btnRestart.setText("Restart");
btnRestart.setEnabled(false);
}
///////////////////////////////////////////////////////////////////////////////
// Register Events
///////////////////////////////////////////////////////////////////////////////
private void registerEvents()
{
btnClick.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (isStarted == false)
{
isStarted = true;
clicks = 0;
time = 0;
topText.setText("Time: " + time);
timer.start();
}
clicks++;
btnClick.setText("[Clicks] " + (int)clicks);
}
});
btnRestart.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
btnClick.setEnabled(true);
btnClick.setText("CLICK TO RESTART");
btnRestart.setEnabled(false);
}
});
}
}
编辑:
感谢您的帮助,我使用node(X,L,R)
制作了新版本:
X