Click to See Complete Forum and Search --> : JFrame Help Please


brw2008
July 20th, 2008, 01:50 PM
Hey all,

Basically i havn't programmed for quite a while now and am trying to get back into it, i am trying to make a little application with a JFrame, and here is the code I have produced...


public class titleScreen extends JFrame {

ImageIcon title = new ImageIcon("pics/title.JPEG");
JLabel label1 = new JLabel("The Software For Your Garden", title, JLabel.CENTER);
JLabel label3 = new JLabel(""); JLabel label4 = new JLabel(""); JLabel label5 = new JLabel("");
JLabel label6 = new JLabel(""); JLabel label7 = new JLabel(""); JLabel label8 = new JLabel("");
JLabel label9 = new JLabel(""); JLabel label10 = new JLabel(""); JLabel label11 = new JLabel("");
JLabel label12 = new JLabel(""); JLabel label13 = new JLabel(""); JLabel label14 = new JLabel("");
JLabel label15 = new JLabel(""); JLabel label16 = new JLabel(""); JLabel label17 = new JLabel("");
JLabel label18 = new JLabel(""); JLabel label19 = new JLabel(""); JLabel label120 = new JLabel("");

JButton butNew = new JButton("New");
JButton butExit = new JButton("Exit");

JTextArea area1 = new JTextArea();

JPanel mainPane = new JPanel();
JPanel pane1 = new JPanel();
JPanel pane2 = new JPanel();
JPanel pane3 = new JPanel();
JPanel pane4 = new JPanel();
JPanel pane5 = new JPanel();


public titleScreen() {

super("Garden");
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

area1 = new JTextArea (
"Welcome to Garden Design!"
);
area1.setEditable(false);
area1.setFont(new Font("Serif", Font.BOLD, 20));
//area1.setLineWrap(true);
area1.setWrapStyleWord(true);

mainPane.setLayout(new BorderLayout());

//TITLE PANEL
pane1.setBackground(Color.WHITE);
pane1.add(label1);
label1.setVerticalTextPosition(JLabel.BOTTOM);
label1.setHorizontalTextPosition(JLabel.CENTER);

//BOTTOM PANEL
pane2.add(label2);
pane2.setBackground(Color.WHITE);

//MAIN PANEL
pane3.setBackground(Color.WHITE);
pane3.add(area1);

//THE BUTTON PANEL WITH A GRID LAYOUT
pane4.setLayout(new GridLayout(3,6,2,2));
pane4.setBackground(Color.WHITE);
pane4.add(label3); pane4.add(label4); pane4.add(label5); pane4.add(label6); pane4.add(label7); pane4.add(label8);
pane4.add(label9); pane4.add(butNew); pane4.add(label10); pane4.add(label11); pane4.add(butExit); pane4.add(label13);
pane4.add(label14); pane4.add(label15); pane4.add(label16); pane4.add(label17); pane4.add(label18); pane4.add(label19);
butNew.setHorizontalTextPosition(JButton.CENTER);
butNew.addActionListener(new ButtonHandler(this, 'A'));
butExit.setHorizontalTextPosition(JButton.CENTER);
butExit.addActionListener(new ButtonHandler(this, 'B'));

pane5.setLayout(new BorderLayout());
pane5.add(pane3, BorderLayout.CENTER);
pane5.add(pane4, BorderLayout.NORTH);

//Setting up the Main Pane
mainPane.add(pane1, BorderLayout.NORTH);
mainPane.add(pane2, BorderLayout.SOUTH);
mainPane.add(pane5, BorderLayout.CENTER);
setContentPane(mainPane);

}

public static void main (String args[]) {

titleScreen titleScreen = new titleScreen();
titleScreen.setBounds(300,100,700,550);

}

class panel extends JPanel {

public void paintComponent(Graphics g) {}

}

class ButtonHandler implements ActionListener {

titleScreen theFrame;
char theOp;

ButtonHandler(titleScreen frame, char op) {

theFrame = frame;
theOp = op;

}

public void actionPerformed(ActionEvent e) {

if (theOp == 'A') {

System.out.print("New has been pushed! \n");

HERE

}

else if (theOp == 'B') {

System.out.print("Exit has been pushed! \n");

HERE

}

}

}

}




Basically, I have created a little JFrame with some layout and 2 main buttons...and this is in a file called titleFrame.java....what I want to do though, is when the 'New' Button is pressed, i would like it to call another seperate class i have created, main.java....whereby in that java file it is like this one, but with different buttons and all....

I think this is really simple to achieve, i just simply cannot remmeber what to do, if anyone can help it would be superb! Also, when the 'Exit' button is pushed, what is the correct syntax to close the JFrame, I need to catch back up on this but im starting to get back into it.

Mank Thanks,

Ben

Norm
July 20th, 2008, 02:09 PM
call another seperate class
Does this mean create a new class or call a method in the class?

To call a method in another class, you need a reference to that class.
You would need to pass the reference to the class to be called to the ButtonHandler class if that is where you want to call it.

To find what method to use, read the API doc for JFrame and see which ones do what you want. Sometimes you'll have to look at the classes that JFrame extends to find the method you want. For JFrame, that's Frame and Window(hint: This one).

brw2008
July 20th, 2008, 02:34 PM
thank you, i solved this by using the following:



System.out.print("New has been pushed! \n");
main mainFrame = new main();
mainFrame.setBounds(325,50,700,700);



but, that does what i want and creates the new window, but how on earth do i close the original one, i can't do this at all??

Many thanks

Norm
July 20th, 2008, 03:19 PM
Please post links to other sites you've posted this on to save us from answering an answered question.