按钮图片图标未显示

时间:2017-08-22 21:05:25

标签: java

请注意我是新手。

在我的编程书" SamTeachYourself Java(24小时)第7版"中,它在第252页说(以防万一有人现在有它),你可以添加图像图标我在第7,8和9行做了一个按钮。当我运行程序时,按钮显示,没有图片。出于某种原因,在我添加图标之前,我在按钮上有文字。当我使用图标运行时,文本仍然在按钮上,即使我删除了图标的文本。我需要认真帮助!

package thing;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Thing extends JFrame implements ActionListener {
    ImageIcon a = new ImageIcon("Desktop\\oof.png");
    JButton fireTorpedos = new JButton(a);
    JButton noTorpedos = new JButton(a);
    JButton reset = new JButton("Reset");
    JLabel txt = new JLabel("Sir, should we fire the torpedos?");
    JLabel output = new JLabel("");
    public void actionPerformed(ActionEvent event) {
        String cmd = event.getActionCommand();
        switch(cmd) {
           case "1":

               noTorpedos.setEnabled(false);
               output.setText("You fired the torpedos!");
               break;
           case "2":

               fireTorpedos.setEnabled(false);
               output.setText("You refuse to fire the torpedos!");
               break;
           case "3":

               fireTorpedos.setEnabled(true);
               noTorpedos.setEnabled(true);
               output.setText("You reset the program.");
               break;

        }
  }
    public Thing() {
        super("Torpedo Launcher!!");
        setLookAndFeel();
        setSize(200, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel grid = new JPanel();
        BoxLayout box = new BoxLayout(grid, BoxLayout.Y_AXIS);
        grid.setLayout(box);
        setVisible(true);
        FlowLayout foo = new FlowLayout();
        fireTorpedos.addActionListener(this);
        noTorpedos.addActionListener(this);
        reset.addActionListener(this);
        fireTorpedos.setActionCommand("1");
        noTorpedos.setActionCommand("2");
        reset.setActionCommand("3");
        grid.add(fireTorpedos);
        grid.add(noTorpedos);
        grid.add(reset);
        grid.add(txt);
        grid.add(output);
        add(grid);





    }
    private void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel (
                    "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
            );
        } catch (Exception exc) {

    }
}
    public static void main(String[] args) {
        Thing frame = new Thing();
    }

}

0 个答案:

没有答案
相关问题