gpt4 book ai didi

Java Swing slider 标签不根据 slider 最小值/最大值更新

转载 作者:搜寻专家 更新时间:2023-11-01 02:24:04 24 4
gpt4 key购买 nike

当这些值发生变化时,我的 JSlider 上的标签表不会根据 slider 的最小值和最大值进行更新。当 getMinimum() 和 getMaximum() 方法返回正确的值时,最小值/最大值会正确更新。 slider 下方的自动标签显示不正确的值。

下面是这个问题的独立示例。代码大部分由 Eclipse 窗口构建器/Swing 设计器自动生成。

按下 slider 两侧的按钮可更改 slider 的最大值。

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JSlider;
import javax.swing.JLabel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SliderTrouble implements ActionListener
{

private JFrame frame;
private JSlider slider;
private JLabel lblMin;
private JLabel lblMax;
private JLabel lblCurrent;
private int sliderMax = 100;
private int sliderMajorSpacing = sliderMax / 4;

/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
SliderTrouble window = new SliderTrouble();
window.frame.setVisible(true);
} catch (Exception e)
{
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public SliderTrouble()
{
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize()
{
frame = new JFrame();
frame.setTitle("Slider problems");
frame.setBounds(100, 100, 481, 122);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout(0, 0));

JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);

JButton btnNewButton = new JButton("100");
btnNewButton.setBounds(10, 22, 64, 23);
btnNewButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Pressed 100. Since this is the default value, everything is ok.");
sliderMax = 100;
sliderMajorSpacing = sliderMax / 4;
changeSlider(sliderMax, sliderMajorSpacing);
}
});
panel.setLayout(null);
panel.add(btnNewButton);

class SliderListener implements ChangeListener
{
public void stateChanged(ChangeEvent e)
{
JSlider slider = (JSlider) e.getSource();
if (slider.getValueIsAdjusting())
{
updateCurrent(slider.getValue());
}
}
}

slider = new JSlider();
slider.setBounds(79, 11, 300, 45);
slider.setMajorTickSpacing(sliderMajorSpacing);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.setMaximum(sliderMax);
slider.addChangeListener(new SliderListener());
panel.add(slider);

JButton btnNewButton_1 = new JButton("196");
btnNewButton_1.setBounds(389, 22, 62, 23);
btnNewButton_1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Pressed 196. The actual minimum and maximum values are updated correctly. getValue gets the correct value. The little gray tick lines are not updated accordingly. The text labels are done according to the default (button '100') settings, showing a max at 175 instead of 196. 175 is the closest multiple of 25 (which is the default major tick space).");
sliderMax = 196;
sliderMajorSpacing = sliderMax / 4;
changeSlider(sliderMax, sliderMajorSpacing);
}
});

panel.add(btnNewButton_1);

JPanel panel_1 = new JPanel();
frame.getContentPane().add(panel_1, BorderLayout.SOUTH);

JLabel lblSliderMin = new JLabel("Slider min:");
panel_1.add(lblSliderMin);

lblMin = new JLabel();
lblMin.setText(String.valueOf(slider.getMinimum()));
panel_1.add(lblMin);

JLabel lblMax_1 = new JLabel("max:");
panel_1.add(lblMax_1);

lblMax = new JLabel();
lblMax.setText(String.valueOf(slider.getMaximum()));
panel_1.add(lblMax);

JLabel lblCur = new JLabel("current:");
panel_1.add(lblCur);

lblCurrent = new JLabel();
lblCurrent.setText(String.valueOf(slider.getValue()));
panel_1.add(lblCurrent);

//frame.pack();
}

private void changeSlider(int max, int majspace)
{
slider.setMajorTickSpacing(majspace);
slider.setMaximum(max);

System.out.println("max: " + max + " major: " + sliderMajorSpacing);

updateText(slider.getMinimum(), slider.getMaximum(), slider.getValue());
}

private void updateText(int minimum, int maximum, int value)
{
lblMin.setText(String.valueOf(minimum));
lblMax.setText(String.valueOf(maximum));
lblCurrent.setText(String.valueOf(value));
}

private void updateCurrent(int value)
{
lblCurrent.setText(String.valueOf(value));
}

public void actionPerformed(ActionEvent arg0)
{
}
}

最佳答案

看起来 setMajorTickSpacing() 在具有先前一组标签的 slider 上调用时实际上并没有更新任何内容。这可以通过调用来解决

slider.setLabelTable(null);

在设置新的刻度间距以强制重新创建标签之前。

关于Java Swing slider 标签不根据 slider 最小值/最大值更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30006930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com