Results 1 to 4 of 4
Thread: dispose method
- 03-18-2013, 12:39 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 10
- Rep Power
- 0
dispose method
I created a program where when I click a button another screen shows up but the only problem I'm having is the first screen isn't going away. I tried to use the dispose method but I come up with an error don't know why though.
The error I'm getting is "non-static method dispose() can't be referenced from a static context MainScreen.dispose();"Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MainScreen extends JFrame { private JTextField field1; private JLabel label1; private JLabel label2; private JButton button1; private JButton button2; private JComboBox box1; private JLabel imageIcon; public MainScreen() { field1 = new JTextField("Welcome"); setLayout(null); Dimension size1 = field1.getPreferredSize(); //Dimension size2 = label1.getPreferredSize(); //Build field1 field1.setBounds(400, 15, 220, 75); field1.setBackground(new Color(0, 102, 102)); field1.setFont(new Font("Times New Roman", 1, 24)); field1.setEditable(false); add(field1); //Build button1 button1 = new JButton("EXIT"); button1.setBounds(400, 620, 220, 75); button1.setBackground(new Color(0, 0, 0)); button1.setForeground(new Color(255, 0, 0)); button1.addActionListener(new button1Listener()); add(button1); //Build button2 button2 = new JButton("Click To Enter"); button2.setBounds(350, 300, 220, 75); button2.addActionListener(new button2Listener()); add(button2); //Build label1 label1 = new JLabel(); label1.setIcon(new ImageIcon ("bible3.gif")); label1.setBounds(0, 15, 600, 600); add(label1); //Build label2 label2 = new JLabel(); label2.setIcon(new ImageIcon ("seed.jpg")); label2.setBounds(707, 100, 400, 400); add(label2); //Build ComboBox box1 = new JComboBox(); box1.setModel(new DefaultComboBoxModel(new String[] { "Gospel", "Raeggae", "Hip-Hop" })); box1.setBounds(700, 0, 89, 50); add(box1); setSize(1100, 770); setTitle("Main Screen"); setBackground(new Color(204, 102, 0)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().setBackground(new Color(204, 102, 0)); setVisible(true); } private class button1Listener implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } private class button2Listener implements ActionListener { public void actionPerformed(ActionEvent e) { MiddleScreen midScr = new MiddleScreen(); midScr.setVisible(true); MainScreen.dispose(); } } public static void main(String[] args) { new MainScreen(); } }
- 03-18-2013, 12:48 AM #2
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 703
- Rep Power
- 1
Re: dispose method
That's because the dispose method is an instance method and not a class method. So you need to use a reference of MainScreen to call it.
Regards,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
-
Re: dispose method
As per what jim829 states, you need an instance of MainScreen, and in fact you need the current instance which can be obtained by changing MainScreen.dispose() to MainScreen.this.dispose().
Having said that I must also be blunt and tell you that your current GUI design is not good. Most applications will have only one main window, and that will be the JFrame. Applications will then either change views held by that main window or else show child windows which should be dialogs such as JDialog or JOptionPanes as they are dependent on the main window.
- 03-18-2013, 10:56 AM #4
Member
- Join Date
- Nov 2012
- Location
- India
- Posts
- 70
- Rep Power
- 0
Re: dispose method
Hi jumpman8947,
can you post the code of MiddleScreen(); and MainScreen.dispose(); here?Regards
Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475
Similar Threads
-
dispose() gives an "invalid method declaration"
By DennisM in forum New To JavaReplies: 4Last Post: 10-28-2011, 08:25 PM -
Help to call a method on dispose()
By Muskar in forum New To JavaReplies: 16Last Post: 11-21-2010, 11:24 AM -
dispose() method not working
By R&R in forum New To JavaReplies: 19Last Post: 11-15-2010, 01:46 AM -
how to dispose?
By justlynn in forum NetBeansReplies: 15Last Post: 07-01-2010, 02:17 AM -
dispose
By simontkk2005 in forum AWT / SwingReplies: 3Last Post: 11-18-2009, 11:42 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks