2010-08-11 57 views
2

我希望有人能給我提供這個解決方案。它是我班的主要實驗室的一部分,它真的不給我太多,因爲我只是不明白如何製作帶有選項卡的GUI。我可以使用某種圖形用戶界面來創建一個常規程序,但是我一直在搜索和閱讀,因爲整個類部件和聲明瞭私有變量,所以不能放置2和2。所以我問的是,如果有人能讓我成爲一個帶有5個選項卡的主GUI,並將我的代碼放到1個選項卡中,那麼我可以學習並將其餘的代碼放入其他4個選項卡中。所以我希望你不要以爲我希望你在我有代碼時給我代碼,我只是沒有真正得到這些標籤,而且我們在課堂上還沒有完成。這裏有自己的gui的代碼,我希望我輸入的內容很有意義。我通過觀察來學習代碼,這將幫助我一堆。GUI中的Java選項卡

package landscape; 
import javax.swing.*; 
import java.awt.Component; 
import java.awt.Container; 
import java.awt.FlowLayout; 
import java.awt.event.*; 
import java.text.DecimalFormat; 

public class Landscape extends JFrame { 

    private JFrame mainFrame; 
    private JButton calculateButton; 
    private JButton exitButton; 
    private JTextField lengthField; 
    private JLabel lengthLabel; 
    private JTextField widthField; 
    private JLabel widthLabel; 
    private JTextField depthField; 
    private JLabel depthLabel; 
    private JTextField volumeField; 
    private JLabel volumeLabel; 
    private JRadioButton builtIn; 
    private JRadioButton above; 
    private JTabbedPane panel; 

    public Landscape() 
    { 

     JTabbedPane tabs=new JTabbedPane(); 
     tabs.addTab("Pool", panel);//add a tab for the panel with the title "title" 
     //you can add more tabs in the same fashion - obviously you can change the title 
     //tabs.addTab("another tab", comp);//where comp is a Component that will occupy the tab 
     mainFrame.setContentPane(tabs);//the JFrame will now display the tabbed pane 
     mainFrame.setSize(265,200); 
     mainFrame.setLocationRelativeTo(null); 
     mainFrame.setVisible(true); 
     mainFrame.setResizable(false); 
     JPanel panel = new JPanel();//FlowLayout is default 

     //Pool 
      panel.setOpaque(false);//this tells the panel not to draw its background; looks nicer under LAFs where the background inside a tab is different from that of JPanel 
     panel.add(builtIn); 
     panel.add(above); 
     panel.add(lengthLabel); 
     panel.add(lengthField); 
     panel.add(widthLabel); 
     panel.add(widthField); 
     panel.add(depthLabel); 
     panel.add(depthField); 
     panel.add(volumeLabel); 
     panel.add(volumeField); 
     panel.add(calculateButton); 
     panel.add(exitButton);  
     //creating components 
     calculateButton = new JButton ("Calculate"); 
     exitButton = new JButton ("Exit"); 
     lengthField = new JTextField (5); 
     lengthLabel = new JLabel ("Enter the length of your pool: "); 
     widthField = new JTextField (5); 
     widthLabel = new JLabel ("Enter the width of your pool: "); 
     depthField = new JTextField (5); 
     depthLabel = new JLabel ("Enter the depth of your pool: "); 
     volumeField = new JTextField (5); 
     volumeLabel = new JLabel ("Volume of the pool: "); 
     //radio button 
     ButtonGroup buttonGroup = new ButtonGroup(); 
     builtIn = new JRadioButton ("Built in"); 
     buttonGroup.add(builtIn); 
     above = new JRadioButton ("Above"); 
     buttonGroup.add(above); 

     exitButton.setMnemonic('x'); 
     calculateButton.setMnemonic('C'); 


     mainFrame.addWindowListener(new WindowAdapter() { 
      public void windowClosing(WindowEvent e) 
      { System.exit(0); } 
     }); 

     // create the handlers 
     calculateButtonHandler chandler = new calculateButtonHandler(); //instantiate new object 
     calculateButton.addActionListener(chandler); // add event listener 

     ExitButtonHandler ehandler = new ExitButtonHandler(); 
     exitButton.addActionListener(ehandler); 

     FocusHandler fhandler = new FocusHandler(); 
     lengthField.addFocusListener(fhandler); 
     widthField.addFocusListener(fhandler); 
     depthField.addFocusListener(fhandler); 

    } 

    class calculateButtonHandler implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      DecimalFormat num = new DecimalFormat(", ###.##"); 
      double width; 
      double length; 
      double depth; 
      double volume; 
      double volume2; 
      double height; 
      String instring; 

      instring = lengthField.getText(); 
      if (instring.equals("")) 
      { 
       instring = "0"; 
       lengthField.setText("0"); 
      } 
      length = Double.parseDouble(instring); 

      instring = widthField.getText(); 
      if (instring.equals("")) 
      { 
       instring = "0"; 
       widthField.setText("0"); 
      } 
      width = Double.parseDouble(instring); 

      instring = depthField.getText(); 
      if (instring.equals("")) 
      { 
       instring = "0"; 
       depthField.setText("0"); 
      } 
      depth = Double.parseDouble(instring); 

      volume = width * length * depth; 
      volumeField.setText(num.format(volume)); 

      volume2 = width * length * depth; 
      } 

     } 

    class ExitButtonHandler implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      System.exit(0); 
     } 
    } 

    class FocusHandler implements FocusListener 
    { 
     public void focusGained(FocusEvent e) 
     { 
      if(e.getSource() == lengthField || e.getSource() == widthField || e.getSource() == depthField) 
      { 
       volumeField.setText(""); 
      } 
      else if (e.getSource() == volumeField) 
      { 
       volumeField.setNextFocusableComponent(calculateButton); 
       calculateButton.grabFocus(); 
      } 
     } 

     public void focusLost1(FocusEvent e) 
     { 
      if(e.getSource() == widthField) 
      { 
       widthField.setNextFocusableComponent(calculateButton); 
      } 
     } 

     public void focusLost(FocusEvent e) 
     { 
      if(e.getSource() == depthField) 
      { 
       depthField.setNextFocusableComponent(calculateButton); 
      } 
     } 
    } 

    public static void main(String args[]) 
    { 
     Landscape app = new Landscape(); 
    } 
} 

回答

1

除了獲取內容窗格並添加它,只需創建一個新的JPanel並添加您的東西。然後,將面板添加到新的JTabbedPane中,並使用所需的任何標題,並將JFrame的內容窗格設置爲選項卡式窗格。

這裏,你會怎麼做一個簡單的例子:

JPanel panel=new JPanel();//FlowLayout is default 
panel.setOpaque(false);//this tells the panel not to draw its background; looks nicer under LAFs where the background inside a tab is different from that of JPanel 
panel.add(builtIn); 
panel.add(above); 
//...you get the picture; add all the stuff you already do, just use panel instead of c 
JTabbedPane tabs=new JTabbedPane(); 
tabs.addTab("title", panel);//add a tab for the panel with the title "title" 
//you can add more tabs in the same fashion - obviously you can change the title 
tabs.addTab("another tab", comp);//where comp is a Component that will occupy the tab 
mainFrame.setContentPane(tabs);//the JFrame will now display the tabbed pane 

您可以留下您的代碼的其餘部分是怎麼回事,它應該工作的罰款。

+0

這有助於很多,謝謝,但與panel.add將這只是在選項卡?因爲我需要一個像4個不同的標籤,其中有不同的操作。很顯然,我需要第一個選項卡包含用於計算池的體積的代碼,而我的下一個選項卡將是熱水浴缸的代碼。 此外,我瞭解如何添加選項卡,但是如何讓我的代碼池顯示在名爲「title」的選項卡中? – David 2010-08-11 06:18:37

+0

面板中的任何內容都將顯示在名爲「title」的選項卡中,並且僅在該選項卡中顯示。要將東西放在其他標籤中,只需創建另一個JPanel,將東西放入新面板中,然後在其中添加另一個標籤。希望有所幫助。 – qmega 2010-08-11 06:29:09

+0

嗨,我編輯了我的代碼,以瞭解*,我相信*,你在暗示,但是當我嘗試編譯它時,我收到了這個錯誤消息。 線程「main」中的異常java.lang.NullPointerException \t at landscape.Landscape。 (Landscape.java:33) \t在landscape.Landscape.main(Landscape.java:189) 我修改了原來的代碼是什麼,我現在有工作。出於某種原因,我覺得我屠殺了這個。 – David 2010-08-11 06:57:48

1

tutorial及其demo是非常直接的例子。

+0

我一直在使用它,並切割我的代碼,並把它放在那裏,但它無法正常工作。我的問題是公共類()和所有變量,他們去哪裏。我不相信我可以舉辦更多的公開課,所以它讓我感到厭煩。 – David 2010-08-11 05:18:54

+0

每個班級文件不能有多個公共班級。你可以擁有任意數量的類文件,只要你喜歡*。在一個類文件中,您可以擁有任意數量的非公開課程*。 (*除非你的老師另有說明)。 – emory 2010-08-11 05:24:03