简体   繁体   English

检查是否已选中JRadioButton

[英]Check if JRadioButton is checked

I'm begining with Java and I'm having a simple problem, I wanna get wether my JCheckBox is selected or no. 我从Java开始,遇到一个简单的问题,我想知道是否选择了JCheckBox或否。 For this I know I have to use comboBox.isSelected(), but in the method I wanna use it, I cannot make reference to the object JCheckBox. 为此,我知道我必须使用comboBox.isSelected(),但是在我要使用的方法中,我无法引用对象JCheckBox。 Here's the code: 这是代码:

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);
            }
        }
    }
}

Declare your radio variable final or as a private member in your class and it will work. 声明您的radio变量final或作为班上的私人成员,它将起作用。

final JRadioButton radio instead of JRadioButton radio final JRadioButton radio而不是JRadioButton radio

You mean the JRadioButton - The application does not contain a JCheckbox 您的意思是JRadioButton应用程序不包含JCheckbox

The compiler does not allow non- final variables to be accessed in an inner class. 编译器不允许在内部类中访问非final变量。 Make the variable radio final 将变量radio final

final JRadioButton radio = new JRadioButton("\u00BFDesea llevar Stock?");

Also Swing was designed to use layout managers . 另外, Swing旨在使用布局管理器 The application still has relatively few components so the transition to using one should be easy. 该应用程序的组件仍然相对较少,因此向使用一个组件的过渡应该很容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM