因此,我有一個程序,明天到期,只要我可以試着弄清楚我的代碼出了什麼問題,我就堅持下去。首先也是最大的問題,讓我沉默了一陣子,是我的重置按鈕動作監聽器。它只是不會正確編譯,並說沒有找到價值?我評論說,所以我至少可以運行程序,你應該能夠清楚地看到我評論他們的地方。另外第二個問題是我必須在移動它們時使兩個滑塊之間的一個文本字段顯示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();
}
}
我在問爲什麼行動監聽器先不工作。然後我要求向正確的方向推動滑塊值。不會說我不想要代碼,但我沒有說全部代碼和給我。我儘可能禮貌地問我可以幫忙嗎?對不起,如果你覺得這是我要求你做我的作業。 – yoshometsu
可以理解。我不會拖累爲什麼我剛纔尋求幫助,但然後我的問題是:「有什麼不對我的行動私人resetbutton監聽器類我有不同的方案,Jbuttons中和他們的行動本完全相同的編碼他們跑就好了聽衆。我不明白爲什麼它給我一個錯誤,當它編譯。能告訴我是什麼原因造成這個錯誤,所以我能解決這個問題?謝謝。」 – yoshometsu