1

所以我有一个明天到期的程序,只要我能找出我的代码有什么问题,我就会坚持下去。第一个也是最大的问题让我沉默了一段时间是我的重置按钮动作监听器。它只是无法正确编译并说找不到值?我把它注释掉了,所以我至少可以运行你应该能够清楚地看到我注释掉它们的地方的程序。第二个问题是我必须让一个文本字段在移动它们时显示两个滑块之间的 MAX 值,另一个给我两个滑块之间的 TOTAL 值。老实说,我不知道该怎么做,只是简单地将两个 jtextfields 附加到左侧滑块。为此,我将不胜感激朝着正确的方向前进?如果您给我代码,我将不胜感激,但我也想解释它为什么有效/我的代码有什么问题。谢谢!

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

2 回答 2

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));
    }
}
于 2012-12-03T07:00:02.047 回答
2

我改变的是向两个滑块添加一个监听器-

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获得。

为了将来参考,我建议您将问题分成几个部分。

于 2012-12-03T07:07:12.927 回答