![](/img/trans.png)
[英]MoveToElement doesn't work to mouse hover and hold the click on an element
[英]How to consume JRadioButton select on mouse click doesn't seem to work
我正在創建一個非常簡單的類來覆蓋Swing JRadioButton,該類允許用戶設置一個字段,該字段確定單選按鈕是否可選。
public class SelectableRadio extends JRadioButton implements MouseListener
private boolean selectable = true;
public SelectableRadio()
{
super();
addMouseListener(this);
}
public void setSelectable(boolean select)
{
selectable = select;
}
@Override
public void mousePressed(MouseEvent e)
{
if (!selectable)
{
e.consume();
}
}
所有其他方法均已實現。 這是行不通的。 將SelectableRadio按鈕設置為NOT selectable時,單擊時仍會選擇單選按鈕。
有什么幫助嗎?
您需要更改setSelectable代碼,並添加以下內容:
if (editable) {
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
super.enableEvents(Event.MOUSE_DOWN | Event.MOUSE_UP);
} else {
this.setCursor(CursorFactory.createUnavailableCursor());
super.disableEvents(Event.MOUSE_DOWN | Event.MOUSE_UP);
}
通常,您將JRadioButton
放入ButtonGroup
。
這是Oracle教程中的示例。
//Create the radio buttons.
JRadioButton birdButton = new JRadioButton(birdString);
birdButton.setMnemonic(KeyEvent.VK_B);
birdButton.setActionCommand(birdString);
birdButton.setSelected(true);
JRadioButton catButton = new JRadioButton(catString);
catButton.setMnemonic(KeyEvent.VK_C);
catButton.setActionCommand(catString);
JRadioButton dogButton = new JRadioButton(dogString);
dogButton.setMnemonic(KeyEvent.VK_D);
dogButton.setActionCommand(dogString);
JRadioButton rabbitButton = new JRadioButton(rabbitString);
rabbitButton.setMnemonic(KeyEvent.VK_R);
rabbitButton.setActionCommand(rabbitString);
JRadioButton pigButton = new JRadioButton(pigString);
pigButton.setMnemonic(KeyEvent.VK_P);
pigButton.setActionCommand(pigString);
//Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(birdButton);
group.add(catButton);
group.add(dogButton);
group.add(rabbitButton);
group.add(pigButton);
//Register a listener for the radio buttons.
birdButton.addActionListener(this);
catButton.addActionListener(this);
dogButton.addActionListener(this);
rabbitButton.addActionListener(this);
pigButton.addActionListener(this);
...
public void actionPerformed(ActionEvent e) {
picture.setIcon(new ImageIcon("images/"
+ e.getActionCommand()
+ ".gif"));
}
您是否嘗試過jRadioButton的enabled屬性? 它使它成為可選擇的或不可選擇的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.