2012-12-03 37 views
1

因此,我有一個程序,明天到期,只要我可以試着弄清楚我的代碼出了什麼問題,我就堅持下去。首先也是最大的問題,讓我沉默了一陣子,是我的重置按鈕動作監聽器。它只是不會正確編譯,並說沒有找到價值?我評論說,所以我至少可以運行程序,你應該能夠清楚地看到我評論他們的地方。另外第二個問題是我必須在移動它們時使兩個滑塊之間的一個文本字段顯示MAX值,另一個給出兩個滑塊之間的TOTAL值。我真的不知道該怎麼做的邏輯結束,只是簡單地將兩個jtextfields連接到左側滑塊。爲此,我希望推動正確的方向?如果你給我代碼,我會非常感激,但我也想解釋它爲什麼工作/我的代碼有什麼問題。謝謝!JButton動作監聽器問題。滑塊值顯示在字段中

import javax.swing.*; 
import javax.swing.event.*; 
import java.awt.*; 


/** 
    This class displays a window with a slider component. 
    The user can slide the left or right slider. As the 
    sliders are adjusted it displays the maximum sound 
    level coming from either slider as well as the total. 
*/ 

public class SoundLevels extends JFrame 
{ 
    private JLabel label1, label2, label3, label4;  // Message labels 
    private JTextField maxSound;  // Max Sound Level 
    private JTextField totalSound; // Total Sound Level 
    private JPanel mpanel;   // Max sound level panel 
    private JPanel tpanel;   // Total sound level panel 
    private JPanel sliderPanel1;  // Slider panel 1 
    private JPanel sliderPanel2;  // Slider panel 2 
    private JPanel resetpanel;  // Reset button panel 
    private JSlider slider1;   // Left sound adjuster 
    private JSlider slider2;   // Right sound adjuster 
    private JButton resetButton;  // Reset button 
    /** 
     Constructor 
    */ 

    public SoundLevels() 
    { 
     // Set the title. 
     setTitle("Sound Levels"); 

     // Specify an action for the close button. 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //Creates reset button 
     resetButton = new JButton("Reset"); 

     // Create the message labels. 
     label1 = new JLabel("Left: "); 
     label2 = new JLabel("Right: "); 
     label3 = new JLabel("Max: "); 
     label4 = new JLabel("Total: "); 

     // Create the read-only text fields. 
     maxSound = new JTextField("0", 10); 
     maxSound.setEditable(false); 
     totalSound = new JTextField("0", 10); 
     totalSound.setEditable(false); 

     // Create the slider. 
     slider1 = new JSlider(JSlider.HORIZONTAL, 0, 100, 0); 
     slider2 = new JSlider(JSlider.HORIZONTAL, 0, 100, 0); 
     slider1.addChangeListener(new SliderListener()); 

     // Create panels and place the components in them. 
     mpanel = new JPanel(); 
     resetpanel = new JPanel(); 
     tpanel = new JPanel(); 
     sliderPanel1 = new JPanel(); 
     sliderPanel2 = new JPanel(); 

     //Add components to panels 
     mpanel.add(label1); 
     mpanel.add(maxSound); 

     tpanel.add(label2); 
     tpanel.add(totalSound); 

     sliderPanel1.add(label1); 
     sliderPanel1.add(slider1); 

     sliderPanel2.add(label2); 
     sliderPanel2.add(slider2); 

     resetpanel.add(resetButton);   

     // Initialize event listener 
//  resetButton.addActionListener(new ResetButtonListener()); 


     // Sets window to a border layout format. 
     setLayout(new GridLayout(1, 5)); 


     // Add the panels to the content pane. 
     add(sliderPanel1); 
     add(sliderPanel2); 
     add(resetpanel); 
     add(mpanel); 
     add(tpanel); 


     // Pack and display the frame. 
     pack(); 
     setVisible(true); 
    } 

    /** 
     Private inner class that handles the event when 
     the user clicks the Reset button. 
    */ 

/* COMMENTED THIS OUT SO IT AT LEAST RUNS 
     private class ResetButtonListener implements ActionListener 
     { 
     public void actionPerformed(ActionEvent e) 
     { 
     // Set the panel's background to red. 
      max = 0; //should reset sliders to 0 
       total = 0; //should reset sliders to 0 
     } 
     } 
*/ 

    /** 
     Private inner class to handle the change events 
     that are generated when the slider is moved. 
    */ 

    private class SliderListener implements ChangeListener 
    { 
     public void stateChanged(ChangeEvent e) 
     { 
     int max, total;    

     // Get the slider value. 
     max = slider1.getValue(); 
      total = slider1.getValue(); 

     // Store the total sound level in its display field. 
     totalSound.setText(Integer.toString(total)); 

     // Store the max sound level in its display field. 
     maxSound.setText(Integer.toString(max)); 
     } 
    } 

    /* 
     The main method creates an instance of the 
     class, which displays a window with a slider. 
    */ 

    public static void main(String[] args) 
    { 
     new SoundLevels(); 
    } 
} 
+0

我在問爲什麼行動監聽器先不工作。然後我要求向正確的方向推動滑塊值。不會說我不想要代碼,但我沒有說全部代碼和給我。我儘可能禮貌地問我可以幫忙嗎?對不起,如果你覺得這是我要求你做我的作業。 – yoshometsu

+0

可以理解。我不會拖累爲什麼我剛纔尋求幫助,但然後我的問題是:「有什麼不對我的行動私人resetbutton監聽器類我有不同的方案,Jbuttons中和他們的行動本完全相同的編碼他們跑就好了聽衆。我不明白爲什麼它給我一個錯誤,當它編譯。能告訴我是什麼原因造成這個錯誤,所以我能解決這個問題?謝謝。」 – yoshometsu

回答

2

我改變是增加一個聽者都sliders-

slider1.addChangeListener(new SliderListener()); 
slider2.addChangeListener(new SliderListener()); 

,也使更改滑塊聽衆 -

private class SliderListener implements ChangeListener { 
     public void stateChanged(ChangeEvent e) { 
      int max = 0; 
      int total = 0; 
      // Get the slider value. 
      int slider1Val = slider1.getValue(); 
      int slider2Val = slider2.getValue(); 
      if (slider1Val > slider2Val) { 
       max = slider1Val; 
      } else { 
       max = slider2Val; 
      } 
      total = slider2Val + slider1Val; 
      // Store the total sound level in its display field. 
      totalSound.setText(Integer.toString(total)); 

      // Store the max sound level in its display field. 
      maxSound.setText(Integer.toString(max)); 
     } 
    } 

下面是整個編輯的方式代碼 -

package org.dchan.orm; 

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.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JSlider; 
import javax.swing.JTextField; 
import javax.swing.event.ChangeEvent; 
import javax.swing.event.ChangeListener; 

/** 
* This class displays a window with a slider component. The user can slide the 
* left or right slider. As the sliders are adjusted it displays the maximum 
* sound level coming from either slider as well as the total. 
*/ 

public class SoundLevels extends JFrame { 
    private JLabel label1, label2, label3, label4; // Message labels 
    private JTextField maxSound; // Max Sound Level 
    private JTextField totalSound; // Total Sound Level 
    private JPanel mpanel; // Max sound level panel 
    private JPanel tpanel; // Total sound level panel 
    private JPanel sliderPanel1; // Slider panel 1 
    private JPanel sliderPanel2; // Slider panel 2 
    private JPanel resetpanel; // Reset button panel 
    private JSlider slider1; // Left sound adjuster 
    private JSlider slider2; // Right sound adjuster 
    private JButton resetButton; // Reset button 

    /** 
    * Constructor 
    */ 

    public SoundLevels() { 
     // Set the title. 
     setTitle("Sound Levels"); 

     // Specify an action for the close button. 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     // Creates reset button 
     resetButton = new JButton("Reset"); 

     // Create the message labels. 
     label1 = new JLabel("Left: "); 
     label2 = new JLabel("Right: "); 
     label3 = new JLabel("Max: "); 
     label4 = new JLabel("Total: "); 

     // Create the read-only text fields. 
     maxSound = new JTextField("0", 10); 
     maxSound.setEditable(false); 
     totalSound = new JTextField("0", 10); 
     totalSound.setEditable(false); 

     // Create the slider. 
     slider1 = new JSlider(JSlider.HORIZONTAL, 0, 100, 0); 
     slider2 = new JSlider(JSlider.HORIZONTAL, 0, 100, 0); 
     slider1.addChangeListener(new SliderListener()); 
     slider2.addChangeListener(new SliderListener()); 
     // Create panels and place the components in them. 
     mpanel = new JPanel(); 
     resetpanel = new JPanel(); 
     tpanel = new JPanel(); 
     sliderPanel1 = new JPanel(); 
     sliderPanel2 = new JPanel(); 

     // Add components to panels 
     mpanel.add(label1); 
     mpanel.add(maxSound); 

     tpanel.add(label2); 
     tpanel.add(totalSound); 

     sliderPanel1.add(label1); 
     sliderPanel1.add(slider1); 

     sliderPanel2.add(label2); 
     sliderPanel2.add(slider2); 

     resetpanel.add(resetButton); 

     // Initialize event listener 
     // resetButton.addActionListener(new ResetButtonListener()); 

     // Sets window to a border layout format. 
     setLayout(new GridLayout(1, 5)); 

     // Add the panels to the content pane. 
     add(sliderPanel1); 
     add(sliderPanel2); 
     add(resetpanel); 
     add(mpanel); 
     add(tpanel); 

     // Pack and display the frame. 
     pack(); 
     setVisible(true); 
    } 

    /** 
    * Private inner class that handles the event when the user clicks the Reset 
    * button. 
    */ 

    /* 
    * COMMENTED THIS OUT SO IT AT LEAST RUNS 
    */private class ResetButtonListener implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 
      // Set the panel's background to red. 
      // max = 0; // should reset sliders to 0 
      // total = 0; // should reset sliders to 0 
      slider1.setValue(0); 
      slider2.setValue(0); 
     } 
    } 

    /* 
*/ 

    /** 
    * Private inner class to handle the change events that are generated when 
    * the slider is moved. 
    */ 

    private class SliderListener implements ChangeListener { 
     public void stateChanged(ChangeEvent e) { 
      int max = 0; 
      int total = 0; 
      // Get the slider value. 
      int slider1Val = slider1.getValue(); 
      int slider2Val = slider2.getValue(); 
      if (slider1Val > slider2Val) { 
       max = slider1Val; 
      } else { 
       max = slider2Val; 
      } 
      total = slider2Val + slider1Val; 
      // Store the total sound level in its display field. 
      totalSound.setText(Integer.toString(total)); 

      // Store the max sound level in its display field. 
      maxSound.setText(Integer.toString(max)); 
     } 
    } 

    /* 
    * The main method creates an instance of the class, which displays a window 
    * with a slider. 
    */ 

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

創建監聽器需要一點邏輯理解。但是你需要明白,爲了從兩個滑塊中獲得一個事件,應該將這個監聽器應用於兩者。整個代碼的要點在 - https://gist.github.com/4193279

以供將來參考,我建議你打破你的問題幾部分組成。

+0

非常感謝您的幫助! :)我對滑塊有一個總體的邏輯思路,幸運的是隻有幾行代碼。無論如何,我非常樂意爲你提供幫助,並且會盡力幫助我們班的其他人遇到同樣的問題!謝謝! – yoshometsu

+0

BoundedRangeModel – mKorbel

2

我建議2度的變化。

slider1.addChangeListener(new SliderListener()); 
slider2.addChangeListener(new SliderListener()); // newly added 

&聽者:

// Get the slider value. 
max = slider1.getValue(); 
total = slider2.getValue(); // change from 1 -> 2 

什麼是錯我的行動私人復位按鈕監聽器類?

這是關於兩個int屬性的範圍和可見性。考慮這段代碼,但請注意,執行的操作不會更改滑塊值,這可能會讓用戶感到困惑。

// class level attributes that are visible to both 
// ResetButtonListener & ResetButtonListener 
private int max, total;    

/** 
Private inner class that handles the event when 
the user clicks the Reset button. 
*/ 
private class ResetButtonListener implements ActionListener 
{ 
    public void actionPerformed(ActionEvent e) 
    { 
     // Set the panel's background to red. 
     max = 0; //should reset sliders to 0 
     total = 0; //should reset sliders to 0 
    } 
} 

/** 
Private inner class to handle the change events 
that are generated when the slider is moved. 
*/ 
private class SliderListener implements ChangeListener 
{ 
    public void stateChanged(ChangeEvent e) 
    { 

     // Get the slider value. 
     max = slider1.getValue(); 
     total = slider2.getValue(); 

     // Store the total sound level in its display field. 
     totalSound.setText(Integer.toString(total)); 

     // Store the max sound level in its display field. 
     maxSound.setText(Integer.toString(max)); 
    } 
}