i need help, for some reason i cannot remove items that i have added to my JPanels
the welcome.remove will is causing a problem

// The "RunescapeCalc" class.
import java.awt.*;
import hsa.Console;
import javax.swing.*;
import java.awt.event.*;
import sun.audio.*;
import java.io.FileInputStream;
import java.math.*;

public class Game1 extends JFrame implements ActionListener
{
    JPanel welcome;
    Container contentPane;
    JLabel startingText;
    JTextField startingField;
    JButton startGame;
    JMenu menu;
    JMenu file;
    JMenuBar menuBar;
    JMenuItem Quit;
    Color menuColor = new Color (200, 255, 100);

    public Game1 ()
    {
        welcome = new JPanel (); // Initializing JPanel as RsPane
        welcome.setLayout (null); // the setup is null meaning I chose what goes where
        contentPane = getContentPane ();   // getting the content pane
        contentPane.add (welcome);  // adding the JPanel to the content pane
        SwingUtilities.updateComponentTreeUI (contentPane); //UPDATES!!!!
        validate ();  // this method sets everything up in the out put screen
        start ();
    }


    public void start ()
    {
        JLabel startingText = new JLabel ("What is your name?");
        startingText.setBounds (50, 50, 200, 50);
        JTextField startingField = new JTextField (8);
        startingField.setBounds (200, 65, 200, 25);
        JButton startGame = new JButton ("Enter");
        startGame.setActionCommand ("startgame");
        startGame.addActionListener (this);
        startGame.setBounds (125, 150, 150, 50);
        SwingUtilities.updateComponentTreeUI (contentPane); //UPDATES!!!!
        menuBar = new JMenuBar (); //creates menuBar
        menuBar.setBackground (menuColor); //colors the menubar
        setJMenuBar (menuBar);
        file = new JMenu ("File"); //creates the menu file
        file.setBackground (menuColor); //colors the menu
        Quit = new JMenuItem ("Quit"); //creates the quit
        Quit.setBackground (menuColor); //colors the quit
        Quit.setActionCommand ("exit"); //sets the action name as exit
        Quit.addActionListener (this); // tells the computer that this is the action name
        file.add (Quit); //adds the quit
        menuBar.add (file); //adds the menu to the menu bar
        welcome.add (startingText);
        welcome.add (startingField);
        welcome.add (startGame);
        contentPane.add (welcome);
    }


    public void stageOne ()
    {

        //  contentPane.add (welcome);
    }


    public void actionPerformed (ActionEvent e)
    {
        String event = e.getActionCommand ();
        if (event.equals ("exit"))
        {
            hide ();
            System.exit (0);
        }
        if (event.equals ("startgame"))
        {
            String name;
            name = startingField.getText ();
             welcome.remove (startingText);
             welcome.remove (startingField);
             welcome.remove (startGame);
            stageOne ();
        }
    }


    public static void main (String[] args)
    {
        Game1 window = new Game1 ();  // creating a windown called GasStation
        window.setTitle ("Game 1");  // Giving the window a title
        window.setSize (900, 700);  // giving the size of the window
        window.setVisible (true);  // asking if this true
    }
}

Recommended Answers

All 7 Replies

L15
L39 double declared startingField, the second declaration covers previous
L82 looking for L15
you have more than one such case

.

Hi.
It is a common good practice; when working with swing; to keep in mind that the swing component exceutes in there own thread. and it is a good practice to conserve these structure:
First step is to write a class as you did. if you choose to put the main methode inside the classe and that calss extends the ActionListener as you did then fist structure to build first is as follow.

// The "RunescapeCalc" class.
import java.awt.*;
//import hsa.Console;
import javax.swing.*;
import java.awt.event.*;
import sun.audio.*;
import java.io.FileInputStream;
import java.math.*;

public class Game1 extends JFrame implements ActionListener {


    public Game1() {
        initComponents();
    }

    private void initComponents() {
        //begin with this will exit the programm when tyou close the JFrame.
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        //Add components here and add the listener to components here.

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //put here your code
    }

    public static void main(String args[]) {
        //This will be excecutes in the EDT.
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Game1().setVisible(true);
            }
        });
    }
}

And then add the components as you wish.

Hope it helps.

i still do not know what is wrong with line 83 - 85
please help, i dont understand AHHH!!! lol

The observations posted by guuba are correct but this will not resolve the problem.
As I said you should first remodel your class as shown above; please.

i dont understand that format, is it possible if you do a line or two to better explain please

actually getting rid of the double declare did fix my problem, thanks guuba

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.