JButton可以工作,但点击它时不会显示我的图标

时间:2010-09-08 02:11:26

标签: java swing applet

我有一个buttonlistener,它在单击时删除了TextFields和StartButton,但是代码的最后一部分告诉它运行一个应该显示图标的方法,但最后只是搞砸了,但是仍然删除TextFields和JButton。请帮忙。

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

     }



 }

2 个答案:

答案 0 :(得分:2)

您根本不必覆盖paintComponent()。只需使用JLabel并设置布局。

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

附录:这是收集启动信息的另一种方法。

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

答案 1 :(得分:1)

您确定要覆盖paintComponenet而不是paint吗?考虑到你正在呼叫super.paint(g),我会先看一下。