Java 检查JRadioButton是否已选中
我从Java开始,我有一个简单的问题,我想知道我的JCheckBox是否被选中。为此,我知道我必须使用comboBox.isSelected(),但在我想要使用它的方法中,我无法引用对象JCheckBox。代码如下:Java 检查JRadioButton是否已选中,java,swing,object,jradiobutton,Java,Swing,Object,Jradiobutton,我从Java开始,我有一个简单的问题,我想知道我的JCheckBox是否被选中。为此,我知道我必须使用comboBox.isSelected(),但在我想要使用它的方法中,我无法引用对象JCheckBox。代码如下: import java.awt.BorderLayout; public class AgregarPlato extends JDialog { private final JPanel contentPanel = new JPanel(); public
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);
}
}
}
}
您是指
JRadioButton
-应用程序不包含JCheckbox
编译器不允许在内部类中访问非final
变量。使变量为单选
最终
final JRadioButton radio = new JRadioButton("\u00BFDesea llevar Stock?");
此外,
Swing
设计用于。应用程序仍然有相对较少的组件,因此向使用组件的过渡应该很容易。您是指JRadioButton
-应用程序不包含JCheckbox
编译器不允许在内部类中访问非final
变量。使变量为单选
最终
final JRadioButton radio = new JRadioButton("\u00BFDesea llevar Stock?");
此外,
Swing
设计用于。应用程序仍然有相对较少的组件,因此过渡到使用组件应该很容易。声明您的收音机变量final
,或者作为您班上的私人成员,它将起作用
final JRadioButton radio
而不是JRadioButton radio
声明您的radio
变量final
或作为您班上的私人成员,它将起作用
最终JRadioButton收音机
代替了JRadioButton收音机
它可以工作!非常感谢你!另外,我想制作一个组合框,其中的项目必须是sql查询的结果,例如,从类别中选择名称,并且组合框必须显示该表的所有名称,它会是什么样子?对不起,我是编程和Java方面的新手!再次感谢你!这听起来像是一个新帖子的主题!非常感谢你!另外,我想制作一个组合框,其中的项目必须是sql查询的结果,例如,从类别中选择名称,并且组合框必须显示该表的所有名称,它会是什么样子?对不起,我是编程和Java方面的新手!再次感谢你!这听起来像是一篇新文章的主题