我有一个使用 Swing 作为 UI 的应用程序。我想要一个按钮来切换应用程序正在使用的通信类型。我想使用切换按钮来识别所选的通信类型。
我的问题是我不希望按钮的颜色在单击后发生变化。目前该按钮看起来像这样...未选中
然后点击后是这样的...
已选中
文本更改是我想要的,但我希望它们具有相同的颜色/样式。
这是我的代码...
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);