开发者

JButton will work but won't display my icon when it is clicked on

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.set开发者_StackOverflow社区Background(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();
             }
         }

     }



 }


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");
        }
    }
}


Are you sure you meant to override paintComponenet and not paint? Considering you're calling super.paint(g), I'd look there first.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜