【问题标题】:Change Button to JButton can't fixe it(((将 Button 更改为 JButton 无法修复它(((
【发布时间】:2013-03-08 19:29:37
【问题描述】:

我被 Button 和 JButton 卡住了

预期结果..(所有信息正常) http://i.stack.imgur.com/3qX7v.png 和我所拥有的 http://i.stack.imgur.com/a4gao.png

我想替换这个:

Button butRock = new Button("Rock");
butRock.addActionListener(this);
ButtPan.add(butRock);

到这里:

BufferedImage buttonIcon = ImageIO.read(new File("rock.jpg"));
JButton butRock = new JButton(new ImageIcon(buttonIcon));
butRock.addActionListener(this);
ButtPan.add(butRock);

我被卡住了,因为当我将 Button 替换为 JButton 时,程序无法正常工作......当我使用这个 JButton 时 问题:如何用图片替换按钮,并且该程序仍然可以正常工作......

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class gui extends JFrame implements ActionListener
{
    public JLabel JWoL,JWoLPlayer,JWoLPC,JNumWin,JNumLose,JNumTie, logo;
    public JLabel JWinT,JLoseT,JTieT, rpsP, rpsC, result;
    JPanel LabelsPan;
    static final int WINS = 0, LOSSES = 1, DRAWS = 2;
    int[] counts = new int[3];
    String[] strings = {"| You Win", "| You Lose", "| Draw"};
    JLabel[] labels = {JNumWin, JNumLose, JNumTie};


    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws IOException
    {
        gui theWindow = new gui();
        theWindow.show();
    }

    private void record(int type)
    {
        JWoL.setText(strings[type]);
        counts[type]++;
        labels[type].setText(""+counts[type]);
    }

    public gui() throws IOException
    {
        JPanel logo = new JPanel();
        logo.setLayout(new GridLayout(1,1));

        BufferedImage myPicture = ImageIO.read(new File("logo.jpg"));
        JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
        add(picLabel);


        JPanel ButtPan=new JPanel();
        ButtPan.setLayout(new GridLayout(1,3));


        BufferedImage buttonIcon = ImageIO.read(new File("rock.jpg"));
        JButton butRock = new JButton(new ImageIcon(buttonIcon));
        butRock.addActionListener(this);
        ButtPan.add(butRock);

        Button butPaper = new Button("Paper");
        butPaper.addActionListener(this);
        ButtPan.add(butPaper);

        Button butScissors = new Button("Scissors");
        butScissors.addActionListener(this);
        ButtPan.add(butScissors);


        JWoLPlayer = new JLabel();
        JWoLPC = new JLabel();
        JWoL= new JLabel();


        JLabel rpsPlayer= new JLabel("| Your Choice:  ");
        JLabel rpsComputer= new JLabel("| Computers Choice:  ");
        setTitle("| RoPaS GAME |");


        LabelsPan=new JPanel();
        LabelsPan.setLayout(new GridLayout(3,2));

        rpsP = new JLabel();
        LabelsPan.add(rpsPlayer);
        LabelsPan.add(JWoLPlayer);

        rpsC = new JLabel();
        LabelsPan.add(rpsComputer);
        LabelsPan.add(JWoLPC);

        result =new JLabel();
        LabelsPan.add(JWoL,"Center");


        JPanel WLPan = new JPanel();
        WLPan.setLayout(new GridLayout(3, 2));


        JWinT = new JLabel("Wins: ");
        JLoseT = new JLabel("Losses: ");
        JTieT = new JLabel("Ties: ");

        WLPan.add(JWinT);
        JNumWin = new JLabel();
        WLPan.add(JNumWin);
        WLPan.add(JLoseT);

        JNumLose = new JLabel();
        WLPan.add(JNumLose);
        WLPan.add(JTieT);

        JNumTie = new JLabel();
        WLPan.add(JNumTie);
        JLabel[] labels1 = {JNumWin, JNumLose, JNumTie};
        labels = labels1;


        JPanel TwoPanesN1=new JPanel();

        TwoPanesN1.setLayout(new BorderLayout());
        TwoPanesN1.add(logo,"North");
        TwoPanesN1.add(LabelsPan,"West");
        TwoPanesN1.add(WLPan,"East");

        getContentPane().setLayout(new GridLayout(3,2));
        getContentPane().add(ButtPan);
        getContentPane().add(TwoPanesN1);


        Font fontDisplay = new Font("Arial", Font.BOLD, 16);
        JWoL.setFont(fontDisplay);
        LabelsPan.setFont(fontDisplay);
        setSize(400,250);
        setVisible(true);
        setResizable(false);

        addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent ev){System.exit(0);}});
    }



    public void Play(String PlayerChoice)
    {
        String PCchoice=PCansw();
        JWoLPC.setText(PCchoice);

        if(PlayerChoice.equals(PCchoice))
            record(DRAWS);


        else if(PlayerChoice.equals("Rock"))
            if(PCchoice.equals("Paper"))
                record(LOSSES);
            else
                record(WINS);


        else if(PlayerChoice.equals("Paper"))
            if(PCchoice.equals("Scissors"))
                record(LOSSES);
            else
                record(WINS);


        else if(PlayerChoice.equals("Scissors"))
            if(PCchoice.equals("Rock"))
                record(LOSSES);
            else
                record(WINS);

    }



    public String PCansw()
    {
        String rpsPC2="";
        int rpsPC=(int)(Math.random( )*3)+1;
        if(rpsPC==1)
            rpsPC2= "Rock";
        else if(rpsPC==2)
            rpsPC2= "Paper";
        else if(rpsPC==3)
            rpsPC2= "Scissors";
        return rpsPC2;
    }


    public void actionPerformed(ActionEvent e)
    {
        if(e.getActionCommand().equals("Exit"))
            System.exit(0);
        else
        {
            JWoLPlayer.setText(e.getActionCommand());
            Play(e.getActionCommand());
        }
    }




}

【问题讨论】:

  • 究竟是什么不能正常工作?应该发生什么;发生了什么?另外,请将您的代码示例减少到重现您的问题所需的最低限度。
  • 当我用 Jbutton 替换按钮时,实际代码不起作用,我的意思是必须显示的随机数不起作用并且计数不起作用
  • 能否从错误信息、异常、编译器警告、预期结果与实际结果等方面详细说明实际问题?
  • 预期结果..(所有信息正常)i.stack.imgur.com/3qX7v.png 和我所拥有的i.stack.imgur.com/a4gao.png

标签: java image swing button jbutton


【解决方案1】:

问题是您使用getActionCommand 来确定单击了哪个JButton,但在Swing 中默认情况下未设置此属性。你必须打电话

butRock.setActionCommand("Rock");

java.awt.Button 自动使用标签属性作为ActionCommand 如果它没有明确设置。来自docs

如果命令名称为空(默认),则此方法返回按钮的标签。


一些旁白:

  • 最好避免mixing heavy & lightweight components,并在整个应用程序中使用轻量级组件。
  • 由于您的 RockPaperScissors 按钮具有相同的功能,请考虑使用 Action
  • 通常使用直接实例 JFrame 而不是扩展类
  • 使用Initial Threads
  • Window#show弃用。使用Window#setVisible
  • 不要使用System.exit - 查看更多here

【讨论】:

    猜你喜欢
    • 2022-12-31
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 2017-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多