2015-09-27 37 views
0

我想取消選中我的JFrame程序中的單選按鈕,它存在一些方法可以通過一個名爲Clean的按鈕取消選擇所有單選按鈕?此按鈕將清理放射按鈕和文本。如何取消選擇JFrame中的單選按鈕

行動碼來進行,這將使清潔所有認爲:

JButton btnClean = new JButton("Clean"); 
     btnClean.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 


      } 
     }); 
     btnClean.setBounds(208, 184, 126, 28); 
     contentPane.add(btnClean); 

回答

5

通常JradioButton將在ButtonGroup的對象,邏輯對象,只允許一個一個JRadioButton組合在一起,在一個時間來選擇。你想要做的就是在這個ButtonGroup對象上調用clearSelection()

順便說一句,你看起來在你的GUI上使用null佈局。雖然Swing的新手可能看起來像是創建複雜GUI的最簡單也是最好的方式,但更多Swing GUI的創建使用它們時會遇到更嚴重的困難。它們不會在GUI大小調整時調整組件的大小,它們是增強或維護的皇室女巫,當它們放在滾動窗格中時它們會完全失敗,在所有平臺或屏幕分辨率與原始視圖不同時,它們看起來會非常糟糕。

例如

import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import javax.swing.*; 

@SuppressWarnings("serial") 
public class BunchOButtons extends JPanel { 
    private static final String[] RADIO_TEXTS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; 
    private ButtonGroup weekDayBtnGroup = new ButtonGroup(); 

    public BunchOButtons() { 
     // JRadioButtons love being held in JPanels that use GridLayout 
     JPanel radioPanel = new JPanel(new GridLayout(1, 0)); 
     radioPanel.setBorder(BorderFactory.createTitledBorder("Week Days")); 
     for (String radioText : RADIO_TEXTS) { 
      JRadioButton radioButton = new JRadioButton(radioText); // create radiobtn 
      radioButton.setActionCommand(radioText); // set its action command text 
      weekDayBtnGroup.add(radioButton); // add to ButtonGroup 
      radioPanel.add(radioButton); // add to a GridLayout-using JPanel 
     } 

     JPanel buttonPanel = new JPanel(); 
     buttonPanel.add(new JButton(new AbstractAction("Show Selection") { 

      @Override 
      public void actionPerformed(ActionEvent evt) { 
       ButtonModel model = weekDayBtnGroup.getSelection(); 
       if (model != null) { 
        // if something is selected 
        System.out.println(model.getActionCommand()); 
       } else { 
        System.out.println("No weekday selected"); 
       } 
      } 
     })); 
     buttonPanel.add(new JButton(new AbstractAction("Clear Selection") { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       weekDayBtnGroup.clearSelection(); 
      } 
     })); 

     setLayout(new BorderLayout()); 
     add(radioPanel, BorderLayout.CENTER); 
     add(buttonPanel, BorderLayout.PAGE_END); 
    } 

    private static void createAndShowGui() { 
     BunchOButtons mainPanel = new BunchOButtons(); 

     JFrame frame = new JFrame("BunchOButtons"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGui(); 
      } 
     }); 
    } 
} 
+0

我沒有在Components中找到ButtonGroup對象,他可以找到他? PS:我是JFrame的新手,我正在編程,但我們在JOptionPane中只使用Java。 –

+0

@Novatec javax.swingx.ButtonGroup – MadProgrammer

+0

@Novatec:作爲瘋狂的國家 - 查看ButtonGroup的API。還有一個關於'Java ButtonGroup Tutorial'的Google首先命中:[The ButtonGroup Tutorial](https://docs.oracle.com/javase/tutorial/uiswing/components/buttongroup.html)。也看看上面的代碼示例。 –

1

當你的JFrame可能包含許多JRadioButton成分任意嵌套在框架的佈局,您可能需要使用Container.getComponents遞歸下降組件層次結構,並檢查各組件是一個instanceof JRadioButton,在哪種情況下你應該投下並且.setSelected(false)它或者Container,在這種情況下你應該繼續遞歸。要獲得JFrame的頂級容器,請使用JFrame.getContentPane

爲了說明一個簡單的例子這種方法(包含由OP要求Clean方法的實現):

public class FrameWithRadios extends JFrame { 

    public FrameWithRadios() { 

     setDefaultCloseOperation(EXIT_ON_CLOSE); 

     JPanel panelCenter = new JPanel(); 
     panelCenter.setLayout(new GridLayout(1, 2)); 

     JRadioButton rb11 = new JRadioButton("Option 1.1"); 
     JRadioButton rb12 = new JRadioButton("Option 1.2"); 
     JRadioButton rb13 = new JRadioButton("Option 1.3"); 

     JPanel panelLeft = new JPanel(); 
     panelLeft.setLayout(new GridLayout(3, 1)); 

     panelLeft.add(rb11); 
     panelLeft.add(rb12); 
     panelLeft.add(rb13); 

     panelCenter.add(panelLeft); 

     JRadioButton rb21 = new JRadioButton("Option 2.1"); 
     JRadioButton rb22 = new JRadioButton("Option 2.2"); 
     JRadioButton rb23 = new JRadioButton("Option 2.3"); 

     JPanel panelRight = new JPanel(); 
     panelRight.setLayout(new GridLayout(3, 1)); 

     panelRight.add(rb21); 
     panelRight.add(rb22); 
     panelRight.add(rb23); 

     panelCenter.add(panelRight); 

     JButton bClean = new JButton("Clean"); 
     bClean.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       clean(getContentPane()); 
      } 
     }); 
     JPanel panelBottom = new JPanel(); 
     panelBottom.add(bClean); 

     Container cp = getContentPane(); 
     cp.setLayout(new BorderLayout()); 

     cp.add(panelCenter, BorderLayout.CENTER); 
     cp.add(panelBottom, BorderLayout.SOUTH); 
    } 

    protected void clean(Component comp) { 
     if (comp instanceof JRadioButton) { 
      ((JRadioButton) comp).setSelected(false); 
     } 
     else if (comp instanceof Container) { 
      for (Component child: ((Container) comp).getComponents()) { 
       clean(child); 
      }; 
     } 
    } 

    public static void main(String[] args) { 
     FrameWithRadios frame = new FrameWithRadios(); 
     frame.setSize(320, 200); 
     frame.setVisible(true); 
    } 
} 

當然,正如其他人的建議,你很可能會到將單選按鈕的選擇狀態管理分配到ButtonGroup,並使用ButtonGroup.clearSelection方法取消選擇所有內容(上述內容對於ButtonGroup中的單選按鈕不起作用)。

+0

BurpttinGroup不是組件或容器,不會添加到UI中。相反,它管理着一組按鈕 – MadProgrammer

+0

我的不好。我的意思是說,檢查每個組件是否是'JRadioButton',並用'.setSelected(false)'取消選擇它。 – madanasta

+0

但是,只有當'JRadioButton'組件不在任何'ButtonGroup'中時,這才起作用。 – madanasta