Java 控制风扇';使用JButton可以提高速度

Java 控制风扇';使用JButton可以提高速度,java,swing,animation,graphics,timer,Java,Swing,Animation,Graphics,Timer,我的教授给我布置了这个家庭作业,让我操作两个按钮并控制一个风扇。我对GUI非常陌生,我一点也不知道如何调用我的计时器,以便使用timer.setDelay()增加它任何帮助都将不胜感激。我的代码如下: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Fan extends JFrame implements ActionListener{ JButton speedup;

我的教授给我布置了这个家庭作业,让我操作两个按钮并控制一个风扇。我对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);
    }
}

  • JPanel testPanel=newmypanel()
    testPanel
    限制为
    JPanel
    的方法(因此不能使用
    testPanel.getTimer()
    )。而是使用
    MyPanel testPanel=newmypanel(),然后您将能够使用
    testTimer.getTimer()

  • if(cmd==“Speed Up”){
    。不要将字符串与
    =
    进行比较。而是使用
    equals
    。因此
    if(“Speed Up”.equals(cmd)){}

  • 如果你想“加速”一个动画,你应该减少延迟,而不是增加它。反之亦然


  • 旁注

    • 在事件调度线程上运行Swings应用程序。您可以通过将代码包装到
      SwingUtilities.invokeLater(…)
      中的
      main
      中来实现这一点。请参阅

    这里有一个固定的例子

    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();
            }
        }
    }
    

    还有什么在你的尝试中不起作用呢。