2013-07-25 76 views
0

我开始与Java和我有一个简单的问题,我想得到更多我的JCheckBox被选中或没有。为此,我知道我必须使用comboBox.isSelected(),但是在我想使用它的方法中,我无法引用对象JCheckBox。下面的代码:检查JRadioButton是否被选中

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

回答

1

你的意思是JRadioButton - 应用程序不包含JCheckbox

编译器不允许非final变量在一个内部类可以访问。使可变radiofinal

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

而且Swing被设计为使用layout managers。该应用程序仍然有相对较少的组件,因此转换到使用应该很容易。

+0

问题纠正:) –

+0

它的工作原理!非常感谢你! 此外,我想制作一个组合框,其中的项目必须是sql查询的结果,例如 SELECT name FROM category,并且ComboBox必须显示该表的所有名称,它将如何? 对不起,我是全新的编程和Java! 非常感谢你! –

+0

这听起来像是一个新帖子的主题 – Reimeus

2

声明您的radio变量final或作为您班级中的私人成员,它将起作用。

final JRadioButton radio代替JRadioButton radio

相关问题