我的教授给了我这个家庭作业,实现了两个按钮并控制了一个风扇。我是GUI的新手,我没有丝毫的线索如何调用我的计时器以使用timer.setDelay();
来增加它。任何帮助都将非常感激。我的代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Fan extends JFrame implements ActionListener{
JButton speedup;
JButton slowdown;
JPanel testPanel = new MyPanel();//trying to inherit properties of MyPanel
public Fan() {
add(testPanel);
GridLayout f = new GridLayout(1,2);
setLayout(f);
JButton speedup = new JButton("Speed Up");
speedup.addActionListener(this);
add(speedup);
JButton slowdown = new JButton("Slow Down");
slowdown.addActionListener(this);
add(slowdown);
}
/* public void actionPerformed(ActionEvent event){
int delay;
String cmd = event.getActionCommand();
if(cmd == "Speed Up" ){
delay = testPanel.getTimer().getDelay();
delay++;
testPanel.getTimer().setDelay(delay);
}
else{
delay = testPanel.getTimer().getDelay();
delay--;
testPanel.getTimer().setDelay(delay);
*/
//My attempt at getting timer to work commented out
}
public class MyPanel extends JPanel {
private static final long serialVersionUID = 1L;
public Timer timer = new Timer(10, new TimerListener());
private int alpha = 0; //angle
public Timer getTimer(){
return timer; //getter method for timer
}
public MyPanel() {
timer.start();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
alpha = alpha + 1;
int xc = getWidth()/2;
int yc = getHeight()/2;
int rad = (int)(Math.min(getWidth(), getHeight())*0.4);
int x = xc - rad;
int y = yc - rad;
g.fillArc(x, y, 2*rad, 2*rad, 0+alpha, 30);
g.fillArc(x, y, 2*rad, 2*rad, 90+alpha, 30);
g.fillArc(x, y, 2*rad, 2*rad, 180+alpha, 30);
g.fillArc(x, y, 2*rad, 2*rad, 270+alpha, 30);
}
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e){
repaint();
}
}
public static void main(String[] args) {
JFrame fan = new Fan();
fan.setSize(700, 700);
fan.setLocationRelativeTo(null);
fan.setDefaultCloseOperation(EXIT_ON_CLOSE);
fan.setTitle("Spinning Fan");
fan.setVisible(true);
}
}
答案 0 :(得分:3)
JPanel testPanel = new MyPanel();
会将testPanel
限制为JPanel
的方法(因此您无法使用testPanel.getTimer()
)。而是使用MyPanel testPanel = new MyPanel();
,然后将能够使用testTimer.getTimer();
if(cmd == "Speed Up" ){
。不要将字符串与==
进行比较。而是使用equals
。所以if ("Speed Up".equals(cmd)) {}
如果你想“加速”一个动画,你应该减少延迟,而不是增加它。反之亦然。
附注
main
中的代码包装在SwingUtilities.invokeLater(...)
中来实现此目的。请点击Initial Threads 这是一个固定的例子
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class Fan extends JFrame implements ActionListener {
JButton speedup;
JButton slowdown;
MyPanel testPanel = new MyPanel();// trying to inherit properties of MyPanel
public Fan() {
add(testPanel);
JButton speedup = new JButton("Speed Up");
speedup.addActionListener(this);
JButton slowdown = new JButton("Slow Down");
slowdown.addActionListener(this);
JPanel panel = new JPanel();
panel.add(speedup);
panel.add(slowdown);
add(panel, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Spinning Fan");
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
int delay;
String cmd = event.getActionCommand();
if ("Speed Up".equals(cmd)) {
delay = testPanel.getTimer().getDelay();
delay--;
testPanel.getTimer().setDelay(delay);
} else {
delay = testPanel.getTimer().getDelay();
delay++;
testPanel.getTimer().setDelay(delay);
}
}
// My attempt at getting timer to work commented out
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Fan();
}
});
}
}
class MyPanel extends JPanel {
private static final long serialVersionUID = 1L;
public Timer timer = new Timer(10, new TimerListener());
private int alpha = 0; // angle
public Timer getTimer() {
return timer; // getter method for timer
}
public MyPanel() {
timer.start();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
alpha = alpha + 1;
int xc = getWidth() / 2;
int yc = getHeight() / 2;
int rad = (int) (Math.min(getWidth(), getHeight()) * 0.4);
int x = xc - rad;
int y = yc - rad;
g.fillArc(x, y, 2 * rad, 2 * rad, 0 + alpha, 30);
g.fillArc(x, y, 2 * rad, 2 * rad, 90 + alpha, 30);
g.fillArc(x, y, 2 * rad, 2 * rad, 180 + alpha, 30);
g.fillArc(x, y, 2 * rad, 2 * rad, 270 + alpha, 30);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 600);
}
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
repaint();
}
}
}