2010-08-11 107 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