helloWorldIsAlliKnow
helloWorldIsAlliKnow

Reputation: 77

Don't know how to use an external pane for GUI

I have the following 2 java classes. I want to make a third class. The third class is supposed to be a tabbed pane which has two tabs. one tab should have the first pane that i put up and the 2nd tab should have the 2nd one I put up. I can't figure it out. Please help me before I break my computer. I've searched everywhere. I've tried to read the oracle documents and it just doesn't click for me I guess. I have read my text over and over and over and........

package Week4;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DecimalFormat;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class OfficeAreaCalculator extends JFrame{

private JFrame mainFrame;
private JButton calculateButton;
private JButton exitButton;
private JTextField lengthField;
private JTextField widthField;
private JTextField areaField;
private JLabel lengthLabel;
private JLabel widthLabel;
private JLabel areaLabel;

public OfficeAreaCalculator()
{
    mainFrame = new JFrame("Office Area Calculator");

    exitButton = new JButton("Exit");
    lengthLabel = new JLabel("Enter the length of the office:");
    widthLabel = new JLabel("Enter the width of the office:");
    areaLabel = new JLabel("Office area:");
    lengthField = new JTextField(5);
    widthField = new JTextField(5);
    areaField = new JTextField(5);
    areaField.setEditable(false);
    calculateButton = new JButton("Calculate");

    Container c = mainFrame.getContentPane();
    c.setBackground(Color.white);
    c.setLayout(new FlowLayout());

    c.add(lengthLabel);
    c.add(lengthField);
    c.add(widthLabel);
    c.add(widthField);
    c.add(areaLabel);
    c.add(areaField);
    c.add(calculateButton);
    c.add(exitButton);

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

    mainFrame.setSize(260, 150);

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

    CalculateButtonHandler chandler = new CalculateButtonHandler();
    calculateButton.addActionListener(chandler);

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

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

    mainFrame.setVisible(true);
}




class CalculateButtonHandler implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        DecimalFormat num = new DecimalFormat(",###.##");
        double width, length, area;
        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);

        area = length * width;
        areaField.setText(num.format(area));

    }

}

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)
        {
            areaField.setText("");
        }
        else if (e.getSource() == areaField)
        {
            calculateButton.requestFocus();
        }
    }

    public void focusLost(FocusEvent e)
    {
        if (e.getSource() == widthField)
        {
            calculateButton.requestFocus();
        }
    }
}

public static void main(String arg[])
{
    new OfficeAreaCalculator();
}
}

and

package Week4;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.*;


public class DayGUI extends JFrame
{
private JFrame mainFrame;
private JButton cmdGood;
private JButton cmdBad;

public DayGUI(){
    mainFrame = new JFrame("Messages");


    cmdGood = new JButton("Good");
    cmdBad = new JButton("Bad");

    Container c = mainFrame.getContentPane();
    c.setBackground(Color.white);
    c.setLayout(new FlowLayout());

    c.add(cmdGood);
    cmdGood.setBackground(Color.green);
    c.add(cmdBad);
    cmdBad.setBackground(Color.red);

    cmdGood.setMnemonic('G');
    cmdBad.setMnemonic('B');

    mainFrame.setSize(300, 100);

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

    ButtonsHandler bhandler = new ButtonsHandler();
    cmdGood.addActionListener(bhandler);
    cmdBad.addActionListener(bhandler);

    mainFrame.setVisible(true);
}

class ButtonsHandler implements ActionListener
{
    public void actionPerformed(ActionEvent e){

        if (e.getSource() == cmdGood)               
            JOptionPane.showMessageDialog(null, "Today is a good day!",
                    "Event Handler Message",
                    JOptionPane.INFORMATION_MESSAGE);

        if (e.getSource() == cmdBad)
            JOptionPane.showMessageDialog(null, "Today is a bad day!",
                    "Event Handler Message",
                    JOptionPane.INFORMATION_MESSAGE);

    }
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    DayGUI app;
    app = new DayGUI();
}

}

Upvotes: 0

Views: 62

Answers (1)

inor
inor

Reputation: 2846

You can't add a Jframe to a tabbed pane. if each of your application was a JPanel you could just add them to your JTabbedPane.

each one of your JFrame classes uses a JFrame, and in the frame, each class creates its content. You can re-arrange your code so that each of your JFrame creates a panel which creates its content just like the Jframe. then the panel is added to the JFrame. your first two applications should still work.

for your third class, use a Jframe, add the JTabbedPane to the Jframe, and then add the two panels created above (one from each of your first two) to your tabbed pane.

the easiest way is probably to accomplish the 3rd class is to just change the first two classes to extend JPanel instead of Jframe. Then create a class which extends from JFrame, and adds a JTabbedPane which adds the two panels.

Upvotes: 1

Related Questions