繁体   English   中英

我不知道为什么我的代码可以在终端和Eclipse中正确运行,但不能单独作为jar文件运行

[英]I can't figure out why my code runs correctly in terminal and eclipse, but not as a jar file by itself

我使用html更改Buttons的颜色,因为那是我可以更改Buttons的颜色并在禁用Button后保持颜色的唯一方法。 如果有人可以告诉我如何解决此问题,将不胜感激。

import java.awt.*;  
import java.awt.event.*;
import javax.swing.*;

public class TicTacToe implements ActionListener {
/*Instance Variables*/
private JFrame window = new JFrame("Tic-Tac-Toe");
private JButton button1 = new JButton("");
private JButton button2 = new JButton("");
private JButton button3 = new JButton("");
private JButton button4 = new JButton("");
private JButton button5 = new JButton("");
private JButton button6 = new JButton("");
private JButton button7 = new JButton("");
private JButton button8 = new JButton("");
private JButton button9 = new JButton("");


private String letter = "";
public static int click = 0;
public TicTacToe()
{           
    /*Create Window*/
    window.setSize(300,300);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setLayout(new GridLayout(3,3));

    /*Add Buttons To The Window*/
    window.add(button1);
    window.add(button2);
    window.add(button3);
    window.add(button4);
    window.add(button5);
    window.add(button6);
    window.add(button7);
    window.add(button8);
    window.add(button9);

    /*Add The Action Listener To The Buttons*/
    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    button4.addActionListener(this);
    button5.addActionListener(this);
    button6.addActionListener(this);
    button7.addActionListener(this);
    button8.addActionListener(this);
    button9.addActionListener(this);

    /*Make The Window Visible*/
    window.setVisible(true);

    String input = JOptionPane.showInputDialog("Choose Your Letter:   \n1) X\n2) O");
    int let = Integer.parseInt(input);
    if ( input.equals("2")){
          setClick(1);
    }
}

public static void setClick (int cNum){
    click = cNum;            
}

public void actionPerformed(ActionEvent a) {    
    click++;

    /*Calculate Who's Turn It Is*/
    if(click == 1 || click == 3 || click == 5 || click == 7 || click == 9|| click == 11){
        letter = "<html><font color = blue>"+ "X"+"</font></html>";


    } else if(click == 2 || click == 4 || click == 6 || click == 8 || click == 10){

        letter = "<html><font color = red>"+ "O"+"</font></html>";

    }

    /*Display X's or O's on the buttons*/
    if(a.getSource() == button1){

       button1.setText(letter);


        button1.setEnabled(false);
    } else if(a.getSource() == button2){

        button2.setText(letter);

        button2.setEnabled(false);
    } else if(a.getSource() == button3){
        button3.setText(letter);

        button3.setEnabled(false);
    } else if(a.getSource() == button4){

        button4.setText(letter);


         button4.setEnabled(false);
    } else if(a.getSource() == button5){

        button5.setText(letter);


         button5.setEnabled(false);
    } else if(a.getSource() == button6){

        button6.setText(letter);


         button6.setEnabled(false);
    } else if(a.getSource() == button7){

        button7.setText(letter);


         button7.setEnabled(false);
    } else if(a.getSource() == button8){

        button8.setText(letter);


         button8.setEnabled(false);
    } else if(a.getSource() == button9){


        button9.setText(letter);


         button9.setEnabled(false);
    }     

    Boolean win=null;
  //horizontal wins
    if( button1.getText() == button2.getText() && button2.getText() == button3.getText() && button1.getText() != "")
    {
    win = true;
    }
    else if(button4.getText() == button5.getText() && button5.getText() == button6.getText() && button4.getText() != "")
    {
    win = true;
    }
    else if(button7.getText() == button8.getText() && button8.getText() == button9.getText() && button7.getText() != "")
    {
    win = true;
    }

    //vertical wins
    else if(button1.getText() == button4.getText() && button4.getText() == button7.getText() && button1.getText() != "")
    {
    win = true;
    }
    else if(button2.getText() == button5.getText() && button5.getText() == button8.getText() && button2.getText() != "")
    {
    win = true;
    }
    else if(button3.getText() == button6.getText() && button6.getText() == button9.getText() && button3.getText() != "")
    {
    win = true;
    }

    //diagonal wins
    else if(button1.getText() == button5.getText() && button5.getText() == button9.getText() && button1.getText() != "")
    {
    win = true;
    }
    else if(button3.getText() == button5.getText() && button5.getText() == button7.getText() && button3.getText() != "")
    {
    win = true;
    }
    else 
    {
    win = false;
    }

    /*Show a dialog if someone wins or the game is tie*/
    if(win == true)
    {
        JOptionPane.showMessageDialog(null, letter, "Winner!", JOptionPane.INFORMATION_MESSAGE);
        window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
    }  
    else if(click == 9 && win == false&&!(button1.isEnabled()||button2.isEnabled()||button3.isEnabled()||button4.isEnabled()||button5.isEnabled()||button6.isEnabled()||button7.isEnabled()||button8.isEnabled()||button9.isEnabled()))
    {
        JOptionPane.showMessageDialog(null, "Tie Game!");
        window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
    }
    else if(click==10&&win==false)
    {
        JOptionPane.showMessageDialog(null, "Tie Game!");
        window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
    }

}

public static void main(String[] args){           
        new TicTacToe();
   }
 }

在终端和日食中运行时进行项目

在此处输入图片说明

仅作为.jar文件运行时的项目

在此处输入图片说明

因此,正如我在评论中指出的,根本原因是按钮上的setEnabled(false) ,使按钮不可单击的一种方法是将setEnabled(false)替换为removeActionListener(this) 这将删除操作侦听器绑定,该绑定实际上使按钮在单击时不采取任何操作。

首先,老实说,我不知道为什么您的HTML颜色呈现功能取决于您启动应用程序的方式,但是无论如何,我建议您考虑使用setForeground(...)而不是HTML颜色呈现,因为您知道这可以使用,而不是在按钮或其Action上调用setEnabled(false) ,而是使用Model-View-Control或MVC程序结构,而不是停用按钮,而是停用模型 如果您以后想要调试和增强,那么这也将使调试和增强变得更加容易。 例如,在下面的代码中,模型类具有press(int row, int col)方法,该方法首先检查网格单元格是否包含X或O值或是否为空白。 如果不为空,则该方法返回/结束。 否则,它将继续设置模型网格单元的值并更改转弯值(即转弯的值)。

public void press(int row, int col) {
   // if value already selected, ignore 
   if (valueGrid[row][col] != TttValue.BLANK) {
      return;
   }

这是我的整个代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
import javax.swing.event.SwingPropertyChangeSupport;

public class TttMvc {
   private static void createAndShowGui() {
      TttModel model = new TttModel();
      TttView view = new TttView();
      new TttControl(model, view);

      JFrame frame = new JFrame("TttMvc");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(view);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

enum TttValue {
   X("X", Color.BLUE), O("O", Color.RED), BLANK("   ", new Color(0, 0, 0, 0));
   private String text;
   private Color color;

   private TttValue(String text, Color color) {
      this.text = text;
      this.color = color;
   }

   public String getText() {
      return text;
   }

   public Color getColor() {
      return color;
   }

}
class TttModel {
   public static final int SIDE = 3;
   public static final String GRID_STATE = "grid state";
   public static final String WIN = "win";
   private SwingPropertyChangeSupport pcSupport = new SwingPropertyChangeSupport(
         this);
   private TttValue[][] valueGrid = new TttValue[SIDE][SIDE];
   private TttValue turn = TttValue.X;
   private boolean win = false;

   public TttModel() {
      reset();
   }

   public TttValue getValue(int row, int col) {
      return valueGrid[row][col];
   }

   public void press(int row, int col) {
      // if value already selected, ignore 
      if (valueGrid[row][col] != TttValue.BLANK) {
         return;
      }

      // otherwise set value of cell
      setValueGrid(turn, row, col);

      // TODO: check for win here

      // if not win:
      turn = (turn == TttValue.X) ? TttValue.O : TttValue.X;
   }

   private void setValueGrid(TttValue tttValue, int row, int col) {
      TttValue oldValue = valueGrid[row][col];
      valueGrid[row][col] = tttValue;
      pcSupport.firePropertyChange(GRID_STATE, oldValue, tttValue);
   }

   public void reset() {
      turn = TttValue.X;
      for (int i = 0; i < valueGrid.length; i++) {
         for (int j = 0; j < valueGrid[i].length; j++) {
            setValueGrid(TttValue.BLANK, i, j);
         }
      }      
   }

   public void addPropertyChangeListener(PropertyChangeListener listener) {
      pcSupport.addPropertyChangeListener(listener);
   }

   public void removePropertyChangeListener(PropertyChangeListener listener) {
      pcSupport.removePropertyChangeListener(listener);
   }

   public void addPropertyChangeListener(String name,
         PropertyChangeListener listener) {
      pcSupport.addPropertyChangeListener(name, listener);
   }

   public void removePropertyChangeListener(String name,
         PropertyChangeListener listener) {
      pcSupport.removePropertyChangeListener(name, listener);
   }
}

class TttView extends JPanel {
   private static final int GAP = 2;
   private static final Font BTN_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 60);
   private JButton[][] buttonGrid = new JButton[TttModel.SIDE][TttModel.SIDE];
   private TttControl control;

   public TttView() {
      JPanel gridPanel = new JPanel();

      gridPanel.setLayout(new GridLayout(TttModel.SIDE, TttModel.SIDE, GAP, GAP));
      for (int i = 0; i < buttonGrid.length; i++) {
         for (int j = 0; j < buttonGrid[i].length; j++) {
            buttonGrid[i][j] = new JButton(TttValue.BLANK.getText());
            buttonGrid[i][j].setFont(BTN_FONT);
            buttonGrid[i][j].addActionListener(new ButtonListener(i, j));
            gridPanel.add(buttonGrid[i][j]);
         }         
      }

      JButton resetButton = new JButton("Reset");
      resetButton.addActionListener(new ResetListener());

      JPanel buttonPanel = new JPanel();
      buttonPanel.add(resetButton);

      setLayout(new BorderLayout());
      add(gridPanel, BorderLayout.CENTER);
      add(buttonPanel, BorderLayout.PAGE_END);
   }

   public void setControl(TttControl control) {
      this.control = control;
   }

   private class ResetListener implements ActionListener {
      @Override
      public void actionPerformed(ActionEvent arg0) {
         if (control != null) {
            control.reset();
         }
      }
   }

   private class ButtonListener implements ActionListener {
      private int row;
      private int col;

      public ButtonListener(int row, int col) {
         this.row = row;
         this.col = col;
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         if (control != null) {
            control.press(row, col);
         }
      }
   }

   public void setValue(TttValue tttValue, int row, int col) {
      JButton button = buttonGrid[row][col];
      button.setText(tttValue.getText());
      button.setForeground(tttValue.getColor());
   }

}


class TttControl {
   private TttModel model;
   private TttView view;

   public TttControl(TttModel model, TttView view) {
      this.model = model;
      this.view = view;

      view.setControl(this);
      model.addPropertyChangeListener(new ModelListener());
   }

   public void reset() {
      model.reset();
   }

   public void press(int row, int col) {
      model.press(row, col);
   }

   private class ModelListener implements PropertyChangeListener {
      @Override
      public void propertyChange(PropertyChangeEvent evt) {
         if (TttModel.GRID_STATE.equals(evt.getPropertyName())) {
            for (int row = 0; row < TttModel.SIDE; row++) {
               for (int col = 0; col < TttModel.SIDE; col++) {
                  TttValue tttValue = model.getValue(row, col);
                  view.setValue(tttValue, row, col);
               }
            }
         }
      }
   }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM