Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1056431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:42:52+00:00 2026-05-16T17:42:52+00:00

I have a buttonlistener that removes the TextFields and the StartButton when it is

  • 0

I have a buttonlistener that removes the TextFields and the StartButton when it is clicked, but the last part of the code that tells it to run a method that is supposed to display the icon is screwing up only at the end, but is still deleting the TextFields and the JButton. Please help.

public class TypeInNames extends JApplet{

  JButton StartButton;
  JTextField name1, name2;
  String player1, player2;
  String reply;
  boolean test = false;
  ImageIcon myIcon;

  Container cp = getContentPane();

     public void init() 
     {
         setSize(350, 400);
         setLayout(null);

         cp.setBackground(Color.black);

         StartButton = new JButton("Start Game!");
         name1 = new JTextField("Player 1",35);
         name2 = new JTextField("Player 2",35);
         //(x, y, width, height);
         StartButton.setBounds(115,200,120,30);
         name1.setBounds(115,140,120,20);
         name2.setBounds(115,170,120,20);

         startGame();
     }

     public void startGame()
     {
         add(StartButton);
         add(name1);
         add(name2);

         StartButton.addActionListener(new ButtonListener());
     }

     public void game()
     {

     }

     public void endGame()
     {
         myIcon = new ImageIcon("portal-cake.jpg");
         test = true;
         repaint();
     }

     public void paintComponent(Graphics g) {
         super.paint(g);
         if(test)
             myIcon.paintIcon(this, g, 0, 0);
     }

     private class ButtonListener implements ActionListener{

         public void actionPerformed(ActionEvent event)
         {
             if (event.getSource() == StartButton)
             {
                 player1 = name1.getText();
                 player2 = name2.getText();
                 remove(StartButton);
                 remove(name1);
                 remove(name2);

                 endGame();
             }
         }

     }



 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:42:52+00:00Added an answer on May 16, 2026 at 5:42 pm

    You don’t have to override paintComponent() at all. Just use a JLabel and set the layout.

    public void endGame() {
        myIcon = new ImageIcon("portal-cake.jpg");
        JLabel label = new JLabel(myIcon);
        this.setLayout(new GridLayout());
        this.add(label);
        this.validate();
    }
    

    Addendum: Here’s an alternate approach to collecting startup information.

    import java.awt.*;
    import javax.swing.*;
    
    public class TypeInNames extends JApplet {
    
        JTextField name1 = new JTextField("Player 1", 35);
        JTextField name2 = new JTextField("Player 2", 35);
    
        @Override
        public void init() {
            this.getContentPane().setBackground(Color.black);
            Icon myIcon = new ImageIcon("portal-cake.jpg");
            JLabel label = new JLabel(myIcon);
            this.add(label);
            startGame();
        }
    
        private void startGame() {
            JPanel panel = new JPanel(new GridLayout(0, 1));
            panel.add(new JLabel("Player 1:"));
            panel.add(name1);
            panel.add(new JLabel("Player 2:"));
            panel.add(name2);
            int result = JOptionPane.showConfirmDialog(
                this, panel, "Click OK to Start",
                JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
            if (result == JOptionPane.OK_OPTION) {
                System.out.println("Selected:"
                    + " " + name1.getText()
                    + " " + name2.getText());
            } else {
                System.out.println("Cancelled");
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys, I have a problem with a code that I've been writing. I
I have a JButton that's attached to an ActionListener, but I also wanted to
Have following listener for keyboard ArrowDown event(it's key code is 40 ): window.onload =
Have converted devise new session from erb to Haml but doens't work, this is
Have searched the database but need to specifically sum(of hours flown or days off)in
Have a bunch of WCF REST services hosted on Azure that access a SQL
Have added the following code to my SQL query: (Note cut down version) DECLARE
I may seem ignorant but I have not found much information about how to
Have some code: using (var ctx = new testDataContext()) { var options = new
Here is what I'm working with: I have an ExpandableListView that uses a SimpleCursorTreeAdapter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.