Final Project Help-- how to set a fram invisable
Author |
Message |
gg1992gg
|
Posted: Tue Dec 22, 2009 3:47 pm Post subject: Final Project Help-- how to set a fram invisable |
|
|
Hello,
I am doing my final project for our grade 12 computers class. I am having trouble setting a frame back to invisable... code is posted below... I have bolded the error
//PIZZAMAIN .... MAIN CLASS
import java.awt.*;
import javax.swing.*;
public class PizzaMain
{
public JFrame MainFrame;
public static void main(String[] args)
{
JFrame MainFrame = new JFrame("Pizza Shop Home");
MainPagePanel MainScreen = new MainPagePanel();
MainScreen.setPreferredSize(new Dimension(800, 600));
MainFrame.getContentPane().add(MainScreen);
MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MainFrame.setBounds(0,0,805,630);
MainFrame.setResizable(false);
MainFrame.setVisible(true);
}
}
//MAINPAGEPANEL CLASS
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
public class MainPagePanel extends JPanel implements ActionListener
{
private ImageIcon mainscreen = new ImageIcon("images/mainscreen.png");
private JButton btnPlay = new JButton(new ImageIcon("images/btnPlayNormal.png"));
private JButton btnHelp = new JButton(new ImageIcon("images/btnHelpNormal.png"));
private JButton btnQuit = new JButton(new ImageIcon("images/btnQuitNormal.png"));
private Image Background;
boolean playopen = false;
public MainPagePanel()
{
Background = mainscreen.getImage();
setLayout (null);
btnPlay.setBounds(230,439,170,80);
btnPlay.setBorderPainted(false);
btnPlay.setRolloverIcon(new ImageIcon("images/btnPlayHover.png"));
btnPlay.setPressedIcon(new ImageIcon("images/btnPlayPressed.png"));
btnPlay.addActionListener(this);
btnHelp.setBounds(420,439,170,80);
btnHelp.setBorderPainted(false);
btnHelp.setRolloverIcon(new ImageIcon("images/btnHelpHover.png"));
btnHelp.setPressedIcon(new ImageIcon("images/btnHelpPressed.png"));
btnHelp.addActionListener(this);
btnQuit.setBounds(610,439,170,80);
btnQuit.setBorderPainted(false);
btnQuit.setRolloverIcon(new ImageIcon("images/btnQuitHover.png"));
btnQuit.setPressedIcon(new ImageIcon("images/btnQuitPressed.png"));
btnQuit.addActionListener(this);
add(btnPlay);
add(btnHelp);
add(btnQuit);
setFocusable(true);
}
public void paintComponent(Graphics gc)
{
super.paintComponent(gc);
gc.drawImage(Background,0,0,null);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==btnPlay)
{
if (playopen==false)
{
MainFrame.setVisible(false);//error is commented below
/*
cannot find symbol
symbol : variable MainFrame
location: class MainPagePanel
MainFrame.setVisible(false);
^
1 error*/
PlayGamePanel PlayGame = new PlayGamePanel();
PlayGame.setPreferredSize(new Dimension(800,600));
PlayFrame.getContentPane().add(PlayGame);
PlayFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
PlayFrame.setBounds(0,0,805,630);
PlayFrame.setResizable(false);
PlayFrame.setVisible(true);
playopen= true;
}
else if (playopen==true)
{
PlayFrame.setVisible(true);
}
}
else if (e.getSource()==btnHelp)
{
}
else if (e.getSource()==btnQuit)
{
System.exit(0);
}
}
}
I realize I cannot do what I am trying but I would like to know how I can access the MainFrame object in the main class, I would like it to be set invisable when the other frame opens.
Thanks in advance for any help! |
|
|
|
|
|
Sponsor Sponsor
|
|
|
chrisbrown
|
Posted: Tue Dec 22, 2009 4:46 pm Post subject: Re: Final Project Help-- how to set a fram invisable |
|
|
First of all, it is very important that you follow proper naming conventions, i.e. variables should start with a lower-case letter.
It improves readability and gives you more useful information. For example, Java coders reading your program would expect to find classes called MainFrame and MainScreen, not variables as you are using them.
To the point, though:
You want to be able to call a method of one object - a JFrame - from another - MainPagePanel. All you need is for MainPagePanel to be able to access mainFrame (note the capitalization).
What that means is that in MainPagePanel, you want to make a new JFrame variable - let's call it parent - that will store the same thing as mainFrame.
Then to get mainFrame into MainPagePanel, its constructor should take one argument, a JPanel. Set parent to the argument value you will pass in the constructor.
Java: | public MainPagePanel(JFrame p) {
parent = p;
...
} |
Then in your main program you can call:
Java: | MainPagePanel mainScreen = new MainPagePanel(mainFrame); |
Now you can call methods on parent, and since it is the same object as mainFrame, you can now control the frame as you need to. |
|
|
|
|
|
gg1992gg
|
Posted: Tue Dec 22, 2009 5:20 pm Post subject: Re: Final Project Help-- how to set a fram invisable |
|
|
THANK YOU!
It worked! I will pay attention to naming conventions as well thanks for the help! |
|
|
|
|
|
|
|