0
如何閱讀JSlider的價值?閱讀JSlider的價值
我在我的程序中使用了滑塊,這是我以前沒有使用的東西,但我正在努力顯示對其所做的更改!
問題是,當您打開程序(50)時,JLabel
顯示滑塊的值,但是當您更改標籤不包含的值時。
下面是代碼:
設置滑塊(在setUpMenuItems類):
sensitivitySlider2 = new JSlider();//direction , min , max , current
sensitivitySlider2.setFont(new Font("Calibri",Font.BOLD,10));
sensitivitySlider2.setMajorTickSpacing(10);
sensitivitySlider2.setMinorTickSpacing(1);
sensitivitySlider2.setPaintLabels(true);
sensitivitySlider2.setPaintTicks(true);
sensitivitySlider2.setPaintTrack(true);
sensitivitySlider2.setAutoscrolls(true);
sensitivitySlider2.setBackground(Color.WHITE);
sensitivitySlider2.setBounds (15,200,250,100);
propertiesPanel.add(sensitivitySlider2);
建立監聽器:
private void registerComponentsForEvents()
{
// Register all the JButton objects for action events
miniButton.addActionListener (this);
applyButton.addActionListener (this);
exitButton.addActionListener (this);
sensitivitySlider2.addChangeListener(this);
}
窗口監聽器,並宣佈方法做出具體:
void addWindowListener(Window w) {
w.addWindowListener(this);
}
//React to window events.
public void windowOpened(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
的ChangeEvent:
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider)e.getSource();
if (!source.getValueIsAdjusting()) {
int sleepSense = (int)source.getValue();
}
}
,然後該行設置一個JLabel作爲值:
sleepSense2 = String.valueOf(sleepSense);
*「這裏是代碼:」*「WindowListener」與地球有什麼關係?爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –
@AndrewThompson我從建立Jslider的教程中學到了它,我用我苗條的知識抓着吸管 – Phil
通過閱讀鏈接使你的知識變得更加豐富。 ..和'JSlider'有兩個大寫字母。如果你不能拼寫它,複製/粘貼。 –