0

私は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);
            }
        }
    }
}
4

2 に答える 2

2

radio変数を宣言するfinalか、クラスのプライベート メンバーとして宣言すると、機能します。

final JRadioButton radioそれ以外のJRadioButton radio

于 2013-07-25T20:59:58.277 に答える
1

つまり、JRadioButtonアプリケーションには含まれていませんJCheckbox

finalコンパイラは、内部クラスで非変数にアクセスすることを許可しません。変数を作るradio final

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

また、レイアウト マネージャーSwingを使用するように設計されています。アプリケーションにはまだコンポーネントが比較的少ないため、コンポーネントを使用するように移行するのは簡単です。

于 2013-07-25T20:59:49.563 に答える