2013-07-25 77 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

相關問題