我想在啟用或禁用 JRadioButton 時更改它的文本。如果按鈕被禁用,它應該顯示一個文本,當它被禁用時,它應該顯示另一個文本。不知何故,我設法讓它在啟用時更改文本。但我的問題是,即使按鈕被禁用,它也會在啟用時顯示文本。我嘗試使用 JCheckBox 而不是 JRadioButton 問題沒有解決。這是代碼;
'''
radiobtn() {
this.setSize(500, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setBackground(new Color(0x00CCFFAAFF));
this.setTitle("Radio Button example");
this.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 52));
loveIcon = new ImageIcon("radio_icon\\icon (1).png");
sadIcon = new ImageIcon("radio_icon\\icon (2).png");
angryIcon = new ImageIcon("radio_icon\\icon (4).png");
wowIcon = new ImageIcon("radio_icon\\icon (3).png");
hahaIcon = new ImageIcon("radio_icon\\icon (6).png");
careIcon = new ImageIcon("radio_icon\\icon (5).png");
love = new JRadioButton("Loved");
love.setBackground(null);
love.setFocusable(false);
love.setForeground(new Color(0x00000));
love.setFont(new Font("Comic Sans MS", Font.PLAIN, 20));
love.setIcon(loveIcon);
love.setSelectedIcon(wowIcon);
love.addItemListener(this);
sad = new JRadioButton("Sad");
sad.setBackground(null);
sad.setFocusable(false);
sad.setForeground(new Color(0x00000));
sad.setFont(new Font("Comic Sans MS", Font.PLAIN, 20));
sad.setIcon(sadIcon);
sad.setSelectedIcon(hahaIcon);
sad.addItemListener(this);
angry = new JRadioButton("Angry");
angry.setBackground(null);
angry.setFocusable(false);
angry.setForeground(new Color(0x00000));
angry.setFont(new Font("Comic Sans MS", Font.PLAIN, 20));
angry.setIcon(angryIcon);
angry.setSelectedIcon(careIcon);
angry.addItemListener(this);
this.add(love);
this.add(angry);
this.add(sad);
this.setVisible(true);
}
public static void main(String[] args) {
new radiobtn();
}
@Override
public void itemStateChanged(ItemEvent e) {
boolean ok = e.getStateChange()==ItemEvent.SELECTED;
System.out.println(ok);
if(ok=true)
love.setText("Wow");
}
}
'''
在上面的代碼中,當按鈕啟用時,我想將愛按鈕的文本從 Loved 更改為 Wow,當按鈕禁用時,我想從 Wow 更改為 Loved。當我單擊該按鈕時,它確實將文本更改為 Wow,但是當我再次單擊它以禁用它時,文本不會更改回它的原始文本,即 Loved。它設定了哇。
uj5u.com熱心網友回復:
使用 SELECTED 或 DESELECTED 狀態并在那里做任何事情(參考)
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
love.setText("Wow");
}
else if (e.getStateChange() == ItemEvent.DESELECTED) {
love.setText("Loved");
}
}
更新:
另外,請確保ItemListener()為每個單選按鈕添加不同的按鈕(不要this全部使用),如下所示:
love.addItemListener(new ItemListener(){
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
System.out.println("wow");
}
else if (e.getStateChange() == ItemEvent.DESELECTED) {
System.out.println("loved");
}
}
});
uj5u.com熱心網友回復:
你會想看看:
- 在容器中布置組件
- 如何撰寫屬性更改偵聽器
可運行示例
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
JRadioButton btn = new JRadioButton("Hello");
btn.addPropertyChangeListener("enabled", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (btn.isEnabled()) {
btn.setText("Hello");
} else {
btn.setText("Good by");
}
}
});
JButton toggle = new JButton("Toggle");
toggle.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
btn.setEnabled(!btn.isEnabled());
}
});
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(btn, gbc);
add(toggle, gbc);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.dispose();
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/418483.html
標籤:
上一篇:JSVGCanvassvg模糊
下一篇:如何用行號突出顯示行尾
