gpt4 book ai didi

Java - 是否可以将 setEditable(boolean b) 应用于 JSlider/

转载 作者:行者123 更新时间:2023-12-01 06:38:49 25 4
gpt4 key购买 nike

在我的程序中,我需要在某些情况下禁用我的 JSlider,但不知道如何操作。我尝试了 setFocusable(false) 但没有成功...提前致谢!

最佳答案

您可以通过使用 enabled 属性来更改/限制用户与 JSlider 的交互。

SliderState

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SliderTest {

public static void main(String[] args) {
new SliderTest();
}

public SliderTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

final JSlider slider = new JSlider();
final JCheckBox checkBox = new JCheckBox();
checkBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
slider.setEnabled(checkBox.isSelected());
}
});
checkBox.setSelected(true);

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
frame.add(slider, gbc);
frame.add(checkBox, gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

}

关于Java - 是否可以将 setEditable(boolean b) 应用于 JSlider/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21418632/

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