2012-07-17 86 views
1

我在面向對象的方式下設計GUI時遇到了麻煩。下面的代碼將幫助我表達我的問題更清楚:Java GUI設計的問題

import javax.swing; 
import java.awt.*; 
import java.awt.event.*; 

public class QuoteOptionsPanel extends JPanel 
{ 
    private JLabel quote; 
    private JRadioButton comedy, philosophy, carpentry; 
    private String comedyQuote, philosophyQuote, carpentryQuote; 
    //----------------------------------------------------------------- 
    // Sets up a panel with a label and a set of radio buttons 
    // that control its text. 
    //----------------------------------------------------------------- 
    public QuoteOptionsPanel() 
    { 
     comedyQuote = "Take my wife, please."; 
     philosophyQuote = "I think, therefore I am."; 
     carpentryQuote = "Measure twice. Cut once."; 

     quote = new JLabel (comedyQuote); 
     quote.setFont (new Font ("Helvetica", Font.BOLD, 24)); 

     comedy = new JRadioButton ("Comedy", true); 
     comedy.setBackground (Color.green); 

     philosophy = new JRadioButton ("Philosophy"); 
     philosophy.setBackground (Color.green); 

     carpentry = new JRadioButton ("Carpentry"); 
     carpentry.setBackground (Color.green); 

     ButtonGroup group = new ButtonGroup(); 
     group.add (comedy); 
     group.add (philosophy); 
     group.add (carpentry); 

     QuoteListener listener = new QuoteListener(); 
     comedy.addActionListener (listener); 
     philosophy.addActionListener (listener); 
     carpentry.addActionListener (listener); 

     add (quote); 
     add (comedy); 
     add (philosophy); 
     add (carpentry); 

     setBackground (Color.green); 
     setPreferredSize (new Dimension(300, 100)); 
    } 
    //***************************************************************** 
    // Represents the listener for all radio buttons. 
    //***************************************************************** 
    private class QuoteListener implements ActionListener 
    { 
     //-------------------------------------------------------------- 
     // Sets the text of the label depending on which radio 
     // button was pressed. 
     //-------------------------------------------------------------- 
     public void actionPerformed (ActionEvent event) 
     { 
      Object source = event.getSource(); 
      if (source == comedy) 
       quote.setText (comedyQuote); 
      else 
       if (source == philosophy) 
        quote.setText (philosophyQuote); 
       else 
        quote.setText (carpentryQuote); 
     } 
    } 
} 

上面的代碼只需創建一個面板,三個單選按鈕,每個對應的報價。它還創建一個顯示報價的標籤。每當選擇一個按鈕時,標籤中的文本將被設置爲相應的引用。我理解這個代碼就好了。試圖修改它時遇到了麻煩。假設我想創建相同的程序,但是單選按鈕垂直疊放。我們還要說,我決定通過將單選按鈕添加到具有BoxLayout的面板來解決這個問題,我在其自己的BoxPanel類中定義了BoxLayout。 (我會再加入BoxPanel我QuoteOptionsPanel,這仍然會包含我的報價JLabel的。) 所以我BoxPanel代碼可能是這個樣子:

import java.awt.*; 
import javax.swing.*; 

public class BoxPanel extends JPanel 
{ 
    private JRadioButton comedy, philosophy, carpentry; 
    public BoxPanel() 
    { 
     setLayout (new BoxLayout (this, BoxLayout.Y_AXIS)); 
     setBackground (Color.green); 

     comedy = new JRadioButton ("Comedy", true); 
    comedy.setBackground (Color.green); 
    philosophy = new JRadioButton ("Philosophy"); 
    philosophy.setBackground (Color.green); 
    carpentry = new JRadioButton ("Carpentry"); 
    carpentry.setBackground (Color.green); 

    ButtonGroup group = new ButtonGroup(); 
    group.add (comedy); 
    group.add (philosophy); 
    group.add (carpentry); 

    QuoteListener listener = new QuoteListener(); 
    comedy.addActionListener (listener); 
    philosophy.addActionListener (listener); 
    carpentry.addActionListener (listener); 

    } 
    //***************************************************************** 
    // Represents the listener for all radio buttons. 
    //***************************************************************** 
    private class QuoteListener implements ActionListener 
    { 
     //-------------------------------------------------------------- 
     // Sets the text of the label depending on which radio 
     // button was pressed. 
     //-------------------------------------------------------------- 
     public void actionPerformed (ActionEvent event) 
     { 
      Object source = event.getSource(); 

      I do not know what to do here. 
     } 
    } 
} 

因此,大家可以看到,我不知道如何定義我的QuoteListener類。我希望它能夠執行與我發佈的原始程序相同的功能,但我不確定如何使其實現。顯示報價的標籤位於QuoteOptionsPanel中,因此我無法訪問它。實質上,我要求使用屬於不同面板上組件的事件偵聽器來更改一個面板上標籤的最佳方式。我將非常感謝您提供的任何幫助。如果我沒有足夠清楚地表達我的問題,請讓我知道。

回答

4

有幾種方法可以解決這個問題,但對於大多數關鍵是獲取和使用引用。都說持有的JLabel作爲私人領域有一個公共方法的類,

public void setQuoteLabelText(String text) { 
    quoteLabel.setText(text); 
} 

然後,你必須通過引用這個類的BoxPanel類的可視化對象,無論是通過構造函數參數或setXXX(...) setter方法。然後你的ActionListener可以調用這個類的對象的方法。

1

您可以創建類其私有實例變量,你需要 訪問的實例。

2.按照許多用封裝的一個,那就是有私有實例變量 和公有的getter制定者爲該實例變量。

3.現在,您可以通過調用的 類實例的公共方法訪問私有成員。

4.一兩件事,請嘗試使用組佈局通過NetBeans團隊在2005年使用創建的窗口生成器專業版,現在免費從谷歌。