我从 Java 开始,我遇到了一个简单的问题,我想知道我的 JCheckBox 是否被选中。为此,我知道我必须使用 comboBox.isSelected(),但在我想使用它的方法中,我无法引用对象 JCheckBox。这是代码:
import java.awt.BorderLayout;
public class AgregarPlato extends JDialog {
private final JPanel contentPanel = new JPanel();
public static void main(String[] args) {
try {
AgregarPlato dialog = new AgregarPlato();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public AgregarPlato() {
setBounds(100, 100, 546, 459);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
JRadioButton radio = new JRadioButton("\u00BFDesea llevar Stock?");
radio.setFont(new Font("Tahoma", Font.PLAIN, 11));
radio.setBounds(91, 207, 168, 23);
contentPanel.add(radio);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton aceptarButton = new JButton("Aceptar");
aceptarButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (radio.isSelected()) {
System.out.println("It doesnt work");
}
}
});
aceptarButton.setActionCommand("OK");
buttonPane.add(aceptarButton);
getRootPane().setDefaultButton(aceptarButton);
}
{
JButton cancelarButton = new JButton("Cancelar");
cancelarButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
cancelarButton.setActionCommand("Cancel");
buttonPane.add(cancelarButton);
}
}
}
}