[英]Can't figure out why Mongo Database app won't work correctly after creating a jar executable
[英]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();
}
}
因此,正如我在評論中指出的,根本原因是按鈕上的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.