开发者

Java tabs in GUI

I am hoping someone can spoon feed me this solution. It is part of my major lab for the class, and it really isn't giving me too much since I just don understand how to make a GUI with tabs. I can make a regular program with some sort of GUI, but I've been searching and reading and can't put 2 and 2 because of the whole class part and declaring the private variables. So what I am asking is if someone can make me a main GUI with 5 tabs and put my code into 1 tab so I can learn and put the rest of my codes into the other 4 tabs. So I hope you don't think I want you to give me code when I have the code, I just don't really get the tabs, and we haven't gone over it in class. Here is the code with its own gui, I hope what I type makes sense. I learn code by seeing, and this will help me a bunch.

  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 imp开发者_如何学JAVAlements 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();
    }
}


Instead of getting the content pane and adding to it, just create a new JPanel and add your stuff to that. Then, add the panel to a new JTabbedPane with whatever title you want, and set the content pane of the JFrame to be the tabbed pane.

Here is a simple example of what you would do:

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

You can leave the rest of your code how it is and it should work fine.


The tutorial and its demo are pretty straight forward examples.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜