2014-03-31 96 views
-1

我的教授給了我這個家庭作業任務來實現兩個按鈕並控制一個風扇。我對GUI很新,我沒有絲毫的線索如何調用我的定時器,以便使用timer.setDelay();來增加它。任何幫助將不勝感激。我的代碼如下:使用JButton控制風扇的速度

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

什麼也沒有在你試着工作嗎? – MadProgrammer

+0

對於[示例](http://stackoverflow.com/questions/3405799/how-to-rotate-an-image-gradually-in-swing/3420651#3420651)。 – trashgod

回答

3
  1. JPanel testPanel = new MyPanel();將限制testPanelJPanel方法(所以你不能使用testPanel.getTimer())。而是使用MyPanel testPanel = new MyPanel();,那麼你將能夠使用testTimer.getTimer();

  2. if(cmd == "Speed Up"){。不要將字符串與==進行比較。請使用equals。所以if ("Speed Up".equals(cmd)) {}

  3. 如果你想「加快」動畫,你應該減少延遲,而不是增加。反之亦然。


旁註

  • 運行搖,在事件指派線程的應用程序。您可以通過將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(); 
     } 
    } 
}