2016-08-16 52 views
0

我想使它成爲cardPanels不可見,直到cardPanel上沒有的動作完成。例如,當您在該窗口上選擇某個Jradiobutton時,會打開一個窗口。我想使用setVisible(boolean)來做到這一點。但是,setVisible由於某種原因不起作用。有什麼我失蹤?cardPanel的setVisible()不起作用

import java.awt.BorderLayout; 
import java.awt.CardLayout; 
import java.awt.Component; 
import java.awt.Container; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 

public class MainFrame extends JFrame { 

    private JFrame frame = new JFrame("Swing Refresh Bug?"); 
    private Container contentPane = frame.getContentPane(); 
    private JPanel cardPanel = new JPanel(); 
    private CardLayout cardLayout = new CardLayout(); 
    private Component currentComponent; 
    private JButton next; 

    MainFrame() { 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     // properties of the cardPanel 
     cardPanel.setLayout(cardLayout); 
     cardPanel.add(new JLabel("One"), "One"); 
     cardPanel.add(new JLabel("Two"), "Two"); 
     cardPanel.add(new JLabel("Three"), "Three"); 
     cardPanel.setVisible(false); 

     // Create a radio button 
     JRadioButton addNext = new JRadioButton("Add next"); 

     // Add the radio buttons listener 
     addNext.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       cardLayout.show(cardPanel, "One"); 
      } 
     }); 

     // Set the layout of the content pane. 
     contentPane.setLayout(new BorderLayout()); 
     contentPane.add(cardPanel, BorderLayout.CENTER); 
     contentPane.add(addNext, BorderLayout.SOUTH); 

     frame.pack(); 
     frame.setVisible(true); 
    } 

    public MainFrame(String title) { 

     MainFrame mf = new MainFrame(); 
    } 

} 
+3

*「調用setVisible不工作」 * - 您永遠不會調用'cardPanel.setVisible(...)'你貼的代碼......當我實現工作正常,我那。請更新您的示例,包括'setVisible'問題。但是,如果這實際上是產生你所描述問題的代碼,我真的不明白實際問題是什麼......順便說一下,你的類不需要擴展'JFrame',所以刪除它並更改類名稱,因爲它有點誤導你,因爲你在代碼中使用了一個單獨的'JFrame'實例。 –

+0

好的。你在哪裏實施它。哪條線。它不適合我。我摘下了延長線。 –

+0

那麼,假設我正確地理解了這個問題,它對我來說「起作用」。我瞭解它,因爲只要單選按鈕未被按下,您希望卡面板不可見。所以,如果這是正確的,我把'cardPanel.setVisible(false);'放在'cardLayout.show(cardPanel,「One」);'之前'並且在'cardPanel.setVisible(true) –

回答

0

在ActionListener中不在構造函數中初始化它。之後你還必須說setVisible(true)。我認爲你可以製作擴展JPannel的CardPannel類。這樣做有更多的意義。而不是在MainFrame構造函數中添加JLabels,請在CardPannel構造函數中執行。我正在使用手機,因此我現在無法向您顯示代碼。我希望我能幫上忙。

0

您的類不需要延伸的JFrame,你已經在類創建的JFrame,如果要移動翻過卡,然後更改的ActionListener如下

addNext.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
      cardLayout.next(cardPanel); 
      } 
     }); 
0

您可以通過setVisible(false)但你設置爲不可見的CardPanel都從未將它設置爲true,因此您的cardPanel(包含您的cardLayout)從不顯示!

如果您希望在按下單選按鈕時顯示cardLayout,則只需在偵聽器中添加setVisible(true)即可​​。

您也可以使用JRadioButton的isSelected()方法來檢查它是否被點擊。例如:

addNext.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      if(addNext.isSelected()){ 
       cardPanel.setVisible(true);     
       cardLayout.show(cardPanel, "One"); 
      } 
      else{ 
       cardPanel.setVisible(false); 
       //or : cardLayout.show(cardPanel, "Two"); 
      } 
     } 
    }); 

如果您希望以這種方式打開JFrame,只需先創建它,然後將其設置爲不可見。然後,您將其設置爲在偵聽器中可見。

我希望它幫助:)