java - JButton 可以工作,但在单击时不会显示我的图标

标签 java swing applet

我有一个按钮监听器,它在单击时删除 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();
             }
         }

     }



 }

最佳答案

您根本不必重写 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");
        }
    }
}

关于java - JButton 可以工作,但在单击时不会显示我的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3664178/

相关文章:

java - 更新通知时设置 ContentView 最终会卡住应用程序? (安卓)

java - Java 与 Python 中相同功能的不同之处

java - 单击 JButton 时打印特定值

java - 在不安装 Java Web 应用程序的情况下使用 xuggle

java - 在 Java Applet 中生成/下载文档

java - 操作 JSON 内的元素 : Change the position of elements inside a JSON string

java - 在一个 Java 文件中一个接一个地运行程序

java - 不知道使用 JLayeredPane 时编译器错误在哪里形成

java - 如何在 Java 中更新 Swing JProgressBar

java - 如何删除 Ubuntu 上 Firefox/Chrome 状态栏中的 'Applet Started' 消息?