如果选择了 b 按钮,请显示 joptionpane 要求用户确认。请参阅如何进行对话。 Joptionpane 也可以通过击中 esc 键或按右上角的顶部, x 按钮来关闭。
如果用户通过单击是按钮来关闭 joptionpane ,我们什么也不做,因为实际上已经选择了按钮 b 。如果用户关闭 joptionpane 任何其他方式,那么我们需要将选择重置为按钮 a 。
import java.awt.EventQueue;
import java.awt.event.ItemEvent;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class Intercep {
private JFrame frame;
private JRadioButton aRadioButton;
private JRadioButton bRadioButton;
private void buildAndDisplayGui() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createButtons());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createButtons() {
JPanel panel = new JPanel();
ButtonGroup bg = new ButtonGroup();
aRadioButton = new JRadioButton("A");
bRadioButton = new JRadioButton("B");
bRadioButton.addItemListener(this::handleItem);
bg.add(aRadioButton);
bg.add(bRadioButton);
panel.add(aRadioButton);
panel.add(bRadioButton);
return panel;
}
private void handleItem(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED) {
int result = JOptionPane.showConfirmDialog(frame,
"Are you sure?",
"Confirm",
JOptionPane.YES_NO_OPTION);
switch (result) {
case 0:
// YES
break;
case -1:
// <ESC> or 'X'
case 1:
// NO
default:
aRadioButton.setSelected(true);
}
}
}
public static void main(String args[]) {
EventQueue.invokeLater(() -> new Intercep().buildAndDisplayGui());
}
}
请注意以上代码中的这一行:
bRadioButton.addItemListener(this::handleItem);
该行被称为。
Add an ItemListener to the B radio button. Refer to How to Use Radio Buttons. The listener is invoked after the selection has changed.
If the B button is selected, display a JOptionPane asking the user to confirm. Refer to How to Make Dialogs. A JOptionPane can also be closed by hitting the ESC key or pressing the X button in the top, right corner.
If the user closes the JOptionPane by clicking the YES button, we do nothing since button B is actually already selected. If the user closes the JOptionPane any other way, then we need to reset the selection to button A.
import java.awt.EventQueue;
import java.awt.event.ItemEvent;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class Intercep {
private JFrame frame;
private JRadioButton aRadioButton;
private JRadioButton bRadioButton;
private void buildAndDisplayGui() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createButtons());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createButtons() {
JPanel panel = new JPanel();
ButtonGroup bg = new ButtonGroup();
aRadioButton = new JRadioButton("A");
bRadioButton = new JRadioButton("B");
bRadioButton.addItemListener(this::handleItem);
bg.add(aRadioButton);
bg.add(bRadioButton);
panel.add(aRadioButton);
panel.add(bRadioButton);
return panel;
}
private void handleItem(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED) {
int result = JOptionPane.showConfirmDialog(frame,
"Are you sure?",
"Confirm",
JOptionPane.YES_NO_OPTION);
switch (result) {
case 0:
// YES
break;
case -1:
// <ESC> or 'X'
case 1:
// NO
default:
aRadioButton.setSelected(true);
}
}
}
public static void main(String args[]) {
EventQueue.invokeLater(() -> new Intercep().buildAndDisplayGui());
}
}
发布评论
评论(2)
将
itemListener
添加到 b 广播按钮。请参阅如何使用无线电按钮。选择更改后调用听众。如果选择了 b 按钮,请显示
joptionpane
要求用户确认。请参阅如何进行对话。Joptionpane
也可以通过击中 esc 键或按右上角的顶部, x 按钮来关闭。如果用户通过单击是按钮来关闭
joptionpane
,我们什么也不做,因为实际上已经选择了按钮 b 。如果用户关闭joptionpane
任何其他方式,那么我们需要将选择重置为按钮 a 。请注意以上代码中的这一行:
该行被称为 。
Add an
ItemListener
to the B radio button. Refer to How to Use Radio Buttons. The listener is invoked after the selection has changed.If the B button is selected, display a
JOptionPane
asking the user to confirm. Refer to How to Make Dialogs. AJOptionPane
can also be closed by hitting the ESC key or pressing the X button in the top, right corner.If the user closes the
JOptionPane
by clicking the YES button, we do nothing since button B is actually already selected. If the user closes theJOptionPane
any other way, then we need to reset the selection to button A.Note this line in the above code:
This is referred to as a method reference.
您想听听 /code> s知道何时选择第一个按钮(请参阅 this )。您可以使用 检查是否选择了另一个按钮。最后,您可以使用
joptionpane#showconfirmdialog(component,object,object,string,int)
提示用户“是”或“否,“使用 AbstractButton#SetSelected(boolean) 或buttongroup#clearSelection
控制选择了哪些按钮。这应该让您沿着正确的轨道开始。
You want to listen for
ItemEvent
s to know when the first button is selected (see this). You can useAbstractButton#isSelected
to check that the other button is selected. Finally, you can useJOptionPane#showConfirmDialog(Component, Object, String, int)
to prompt the user for "Yes" or "No," and use something likeAbstractButton#setSelected(boolean)
orButtonGroup#clearSelection
to control which buttons are selected.This should get you started down the right track.