使用JSlider实时更新JLabel
Use JSlider to update JLabel in real time
我试图让 JSlider 实时更新 JLabel,但它只是出现错误。到目前为止,这是我的代码:
slider.addChangeListener(new ChangeListener() {
public void stateChanged() {
label.setText(String.valueOf(slider.getValue()));
}
});
谁能看出我哪里出错了?
谢谢,
我错了,你的代码中有一个非常基本的错误:你的 stateChanged 方法是错误的,因为它需要一个 ChangeEvent 参数。更改
public void stateChanged() {
到
public void stateChanged(ChangeEvent e) {
编译器错误消息应该会告诉您所有这一切,并且在未来,您会想要批判性地阅读这些消息,因为如果您这样做,它们会为您省去很多麻烦。
例如:
import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class SliderFoo extends JPanel {
private JSlider slider = new JSlider(0, 100, 50);
private JLabel label = new JLabel(" ", SwingConstants.CENTER);
public SliderFoo() {
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
label.setText(String.valueOf(slider.getValue()));
}
});
slider.setMajorTickSpacing(20);
slider.setMinorTickSpacing(5);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
JPanel topPanel = new JPanel();
topPanel.add(new JLabel("Slider Value:"));
topPanel.add(label);
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setLayout(new BorderLayout(5, 5));
add(topPanel, BorderLayout.PAGE_START);
add(slider);
}
private static void createAndShowGui() {
SliderFoo mainPanel = new SliderFoo();
JFrame frame = new JFrame("SliderFoo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
createAndShowGui();
});
}
}
我试图让 JSlider 实时更新 JLabel,但它只是出现错误。到目前为止,这是我的代码:
slider.addChangeListener(new ChangeListener() {
public void stateChanged() {
label.setText(String.valueOf(slider.getValue()));
}
});
谁能看出我哪里出错了? 谢谢,
我错了,你的代码中有一个非常基本的错误:你的 stateChanged 方法是错误的,因为它需要一个 ChangeEvent 参数。更改
public void stateChanged() {
到
public void stateChanged(ChangeEvent e) {
编译器错误消息应该会告诉您所有这一切,并且在未来,您会想要批判性地阅读这些消息,因为如果您这样做,它们会为您省去很多麻烦。
例如:
import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class SliderFoo extends JPanel {
private JSlider slider = new JSlider(0, 100, 50);
private JLabel label = new JLabel(" ", SwingConstants.CENTER);
public SliderFoo() {
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
label.setText(String.valueOf(slider.getValue()));
}
});
slider.setMajorTickSpacing(20);
slider.setMinorTickSpacing(5);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
JPanel topPanel = new JPanel();
topPanel.add(new JLabel("Slider Value:"));
topPanel.add(label);
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setLayout(new BorderLayout(5, 5));
add(topPanel, BorderLayout.PAGE_START);
add(slider);
}
private static void createAndShowGui() {
SliderFoo mainPanel = new SliderFoo();
JFrame frame = new JFrame("SliderFoo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
createAndShowGui();
});
}
}