gpt4 book ai didi

java - 禁用 JToggleButton 上的启用效果

转载 作者:行者123 更新时间:2023-11-30 07:25:38 25 4
gpt4 key购买 nike

我有一个使用 Swing 作为 UI 的应用程序。我想要一个按钮来切换应用程序正在使用的通信类型。我想使用切换按钮来标识所选的通信类型。

我的问题是我不希望按钮的颜色在单击后发生变化。目前该按钮看起来像这样......未选择

http://i.stack.imgur.com/Ccdie.png

然后当点击它时,它看起来像这样......

已选择

http://i.stack.imgur.com/Q5yp4.png

文本更改是我想要的,但我希望它们具有相同的颜色/样式。

这是我的代码...

    JToggleButton tglbtnCommunicationType = new JToggleButton("AlwaysOn");
tglbtnCommunicationType.setFocusPainted(false);
tglbtnCommunicationType.addChangeListener(new ChangeListener( ) {
public void stateChanged(ChangeEvent tgl) {
System.out.println("ChangeEvent!");
if(tglbtnCommunicationType.isSelected()){
tglbtnCommunicationType.setText("REST");
tglbtnCommunicationType.setBackground(UIManager.getColor("Button.background"));
}
else
{
tglbtnCommunicationType.setText("AlwaysOn");
};
}
});

我的想法是,在选择背景时将其设置为标准背景颜色可以解决此问题,但看起来并非如此。有什么想法吗?

谢谢!

答案:我改用了 JButton,谢谢大家的帮助!

JButton btnCommunicationType = new JButton("AlwaysOn");
btnCommunicationType.setFocusPainted(false);
btnCommunicationType.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(btnCommunicationType.getText().equals("AlwaysOn"))
{
btnCommunicationType.setText("REST");
//TODO: Insert Code for Switching Communication to REST here
}
else if(btnCommunicationType.getText().equals("REST")){
btnCommunicationType.setText("AlwaysOn");
//TODO: Insert Code for Switching Communication to AlwaysOne here
}
}
});
btnCommunicationType.setBounds(275, 199, 97, 25);
thingWorxConnectionPanel.add(btnCommunicationType);

最佳答案

您可以仅使用 JButton 而不是 JToggleButton 来做到这一点,

JButton showButton = new JButton("AlwaysOn");
showButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String currentText = showButton.getText();
if("AlwaysOn".equals(currentText)){
showButton.setText("REST");
}else{
showButton.setText("AlwaysOn");
}
}
});

关于java - 禁用 JToggleButton 上的启用效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36844113/

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