2015-02-12 38 views
1

我使用eclipse和windowbuilder從用戶獲取數學表達式(例如ADD(MUL(X,Y),Z))並在某一點詢問用戶指定其中一個變量作爲時間。 然後用戶將被要求選擇該變量的起點和範圍。那麼程序將顯示一個框架(和一個面板),它具有一個jslider和一個播放按鈕。當用戶單擊該按鈕時,程序應該開始繪製表達式。 它應該看起來像一個Gapminder chart在java中製作可播放的jslider

我已經檢查了有關更新jsliders的其他問題,但它們通常是關於音樂播放器滑塊或僅隨時間變化的滑塊(不包含圖形部分)。

我的問題是關於更新滑塊(間隔可以是1) ,但如果您還可以指導我如何製作可以用於繪圖部分的可滑動滑塊,那將是非常好的。

enter image description here 這個到目前爲止的代碼:

package progGUI; 

import java.awt.BorderLayout; 

public class CanvasFrame extends JFrame { 

private JPanel contentPane; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 

       CanvasFrame frame = new CanvasFrame(); 
       frame.setVisible(true); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the frame. 
*/ 
public CanvasFrame() { 
    setTitle("Graph"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 700, 542); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 

    JSlider slider = new JSlider(); 
    slider.setPaintLabels(true); 
    slider.setMinorTickSpacing(1); 
    slider.setMajorTickSpacing(5); 
    slider.setPaintTicks(true); 


    JLabel lblEnteredExpression = new JLabel("Entered Expression"); 

    JLabel lblVaraibles = new JLabel("Variables"); 

    JFormattedTextField formattedTextField = new JFormattedTextField(); 

    JFormattedTextField formattedTextField_1 = new JFormattedTextField(); 

    JPanel panel = new JPanel(); 
    panel.setBackground(Color.WHITE); 

    JButton btnNewButton = new JButton("Play"); 
    GroupLayout gl_contentPane = new GroupLayout(contentPane); 
    gl_contentPane.setHorizontalGroup(
     gl_contentPane.createParallelGroup(Alignment.LEADING) 
      .addGroup(gl_contentPane.createSequentialGroup() 
       .addContainerGap() 
       .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) 
        .addGroup(gl_contentPane.createSequentialGroup() 
         .addComponent(panel, GroupLayout.PREFERRED_SIZE, 453, GroupLayout.PREFERRED_SIZE) 
         .addPreferredGap(ComponentPlacement.RELATED, 51, Short.MAX_VALUE) 
         .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) 
          .addComponent(lblEnteredExpression, Alignment.TRAILING) 
          .addComponent(lblVaraibles) 
          .addComponent(formattedTextField, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE) 
          .addComponent(formattedTextField_1, GroupLayout.PREFERRED_SIZE, 123, GroupLayout.PREFERRED_SIZE)) 
         .addGap(57)) 
        .addGroup(gl_contentPane.createSequentialGroup() 
         .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE) 
         .addPreferredGap(ComponentPlacement.RELATED) 
         .addComponent(slider, GroupLayout.PREFERRED_SIZE, 454, GroupLayout.PREFERRED_SIZE) 
         .addContainerGap()))) 
    ); 
    gl_contentPane.setVerticalGroup(
     gl_contentPane.createParallelGroup(Alignment.TRAILING) 
      .addGroup(gl_contentPane.createSequentialGroup() 
       .addGap(18) 
       .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) 
        .addGroup(gl_contentPane.createSequentialGroup() 
         .addComponent(lblEnteredExpression) 
         .addPreferredGap(ComponentPlacement.RELATED) 
         .addComponent(formattedTextField_1, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE) 
         .addPreferredGap(ComponentPlacement.UNRELATED) 
         .addComponent(lblVaraibles) 
         .addPreferredGap(ComponentPlacement.RELATED) 
         .addComponent(formattedTextField, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)) 
        .addComponent(panel, GroupLayout.PREFERRED_SIZE, 350, GroupLayout.PREFERRED_SIZE)) 
       .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) 
        .addGroup(gl_contentPane.createSequentialGroup() 
         .addGap(31) 
         .addComponent(btnNewButton)) 
        .addGroup(gl_contentPane.createSequentialGroup() 
         .addGap(18) 
         .addComponent(slider, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))) 
       .addGap(402)) 
    ); 
    contentPane.setLayout(gl_contentPane); 
    } 


} 
+0

你是什麼意思可玩見How to use Swing TimersHow to Use Sliders? – immibis 2015-02-12 08:18:44

+0

我的意思是它更新,所以當你按下播放按鈕,它從零開始,並增加了滑塊旋鈕的值(每秒1) 看看gapminder鏈接這就是我想寫的 – Vicarious 2015-02-12 08:26:09

+0

允許'JSlider'控制圖中繪製的內容,使用Swing'Timer'定期更新'JSlider' – MadProgrammer 2015-02-12 08:27:57

回答

5

用戶javax.swing.Timer安排定期回呼,其更新JSlider的價值。使用連接到JSlider一個ChangeListener驅動圖形,例如...

Slider

import java.awt.EventQueue; 
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.JSlider; 
import javax.swing.JTextField; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.event.ChangeEvent; 
import javax.swing.event.ChangeListener; 

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private Timer timer; 
     private JSlider slider; 
     private JButton button; 
     private JTextField field; 

     public TestPane() { 
      slider = new JSlider(); 
      field = new JTextField(4); 
      button = new JButton(">"); 
      add(slider); 
      add(button); 
      add(field); 

      slider.addChangeListener(new ChangeListener() { 
       @Override 
       public void stateChanged(ChangeEvent e) { 
        field.setText(Integer.toString(slider.getValue())); 
       } 
      }); 
      slider.setValue(0); 

      button.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        if (timer.isRunning()) { 
         stopTheClock(); 
        } else { 
         startTheClock(); 
        } 
       } 
      }); 
      timer = new Timer(500, new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        int value = slider.getValue() + 1; 
        if (value >= slider.getMaximum()) { 
         stopTheClock(); 
        } else { 
         slider.setValue(value); 
        } 
       } 
      }); 
     } 

     protected void startTheClock() { 
      slider.setValue(0); 
      timer.start(); 
      button.setText("[]"); 
     } 

     protected void stopTheClock() { 
      timer.stop(); 
      button.setText(">"); 
     } 

    } 

} 

更多細節

+0

謝謝您的寶貴答案! 但我不能實現你的代碼我的 我已經在最後一天嘗試了這麼多次,但代碼並沒有標識整個類中的滑塊和計時器對象 你能幫我把你的代碼添加到一個我有問題嗎? – Vicarious 2015-02-12 19:20:03