Results 1 to 6 of 6
- 04-24-2011, 07:55 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
closing JFrame without terminating program
There are two frames and when you click on a button on the first frame it opens the second frame. On the second frame I'm trying to create a button which when pressed closes the JFrame without terminating the program but I'm having no luck. This is the code that I'm trying to use for the second frame, which without the button compiles fine:
Java Code:class Time_First_Depot extends JFrame { Time_First_Depot() { Container c = getContentPane(); \\ creates content pane c.setLayout ( null ); Color b = new Color(100,200,255); \\ set colour of JFrame c.setBackground( b ); JButton exit = new JButton("EXIT"); \\creats button exit.addActionListener(new ExitButtonListener()); \\ adds listener to button exit.setForeground(Color.BLUE); \\edits buton exit.setFont(new Font("Time", Font.BOLD, 12)); c.add(exit);\\adds button exit.setBounds(250, 375, 90, 30);\\ sets location of button Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); this.setBounds((int) screenSize.getWidth()/2 - 370, (int) screenSize.getHeight()/2 - 300, 600, 450); // set position and size this.setResizable(false); this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); this.setTitle("Time"); this.setVisible(true); this.setResizable(false); } } class ExitButtonListener implements ActionListener { ExitButtonListener(){} public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("EXIT")) { this.dispose(); } } }
however, when i try to compile it the following message comes up:
cannot find symbol
symbol : method dispose()
location: class ExitButtonListener
this.dispose();Last edited by chirag123; 04-24-2011 at 08:23 PM.
-
Your solution: Don't use a second JFrame but rather have the second window be a JDialog since it is truly acting as a dialog of the first window. And to answer your next question, yes, a JDialog can hold as complex a GUI as any JFrame.
Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?
To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
- 04-24-2011, 08:27 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
Thanks for that Fubarable.
I know this may sound silly but I'm new to java. I'm not sure what you mean by dialog (excuse my inexperience). My teachers brief is to create a program for a bus company. The first window is basically the main menu which has links to the other frames. Not sure how that impacts on dialog but thought perhaps some more information may help clarify things.
-
- 04-24-2011, 09:02 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
Thanks for that after reading that I understand what a dialog box is but that's not quite what I'm trying to achieve. The code that I pasted is supposed to give the time from one bus depot to another bus depot, when the user selects using buttons which depots they start at and finish at and that part of the code is functioning fine so I didn't bother copying needless coding which isn't relevant to the question. Looking back perhaps I should have and it would have been clearer
- 04-25-2011, 01:10 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Closing a JFrame. setVisible etc ain't workin'.
By AndroidAppNewbie in forum New To JavaReplies: 9Last Post: 03-03-2011, 05:54 PM -
Closing a JFrame
By SBL in forum AWT / SwingReplies: 5Last Post: 11-23-2009, 02:54 PM -
Closing Popup JFrame in Applet
By Arsenic in forum Java AppletsReplies: 3Last Post: 04-10-2009, 08:37 PM -
JFrame (closing under a condition)
By Java Tip in forum Java TipReplies: 0Last Post: 03-12-2008, 11:17 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks