CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: dispose method

Hybrid View

  1. #1
    Join Date
    Feb 2013

    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.
    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");
    		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));
    		//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());
    		//Build button2
    		button2 = new JButton("Click To Enter");
    		button2.setBounds(350, 300, 220, 75);
    		button2.addActionListener(new button2Listener());
    		//Build label1
    		label1 = new JLabel();
    		label1.setIcon(new ImageIcon ("bible3.gif"));
    		label1.setBounds(0, 15, 600, 600);
    		//Build label2
    		label2 = new JLabel();
    		label2.setIcon(new ImageIcon ("seed.jpg"));
    		label2.setBounds(707, 100, 400, 400);
    		//Build ComboBox
    		box1 = new JComboBox();
    		box1.setModel(new DefaultComboBoxModel(new String[]
    			"Gospel", "Raeggae", "Hip-Hop"
    		box1.setBounds(700, 0, 89, 50);
    		setSize(1100, 770);
    		setTitle("Main Screen");
    		setBackground(new Color(204, 102, 0));
    		getContentPane().setBackground(new Color(204, 102, 0));
    	private class button1Listener implements ActionListener
    					public void actionPerformed(ActionEvent e)
    	private class button2Listener implements ActionListener
    					public void actionPerformed(ActionEvent e)
    						MiddleScreen midScr = new MiddleScreen();
    	public static void main(String[] args)
    		new MainScreen();
    The error I'm getting is "non-static method dispose() can't be referenced from a static context MainScreen.dispose();"

  2. #2
    Join Date
    May 2009

    Re: dispose method

    Quote Originally Posted by jumpman8947 View Post
    The error I'm getting is "non-static method dispose() can't be referenced from a static context MainScreen.dispose();"
    Well, you're calling dispose as if it were a static method of MainScreen but there is no such method, is it? At least I cannot see you put one there.

    There is a dispose method inherited over Jframe from Window though (I think I can recall). If so you could call it just by skipping the MainScreen prefix. But I don't know if calling dispose will actually solve your problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.