Java 程序不';请不要在关闭应用程序后停止
我试着重新创建连接四,我成功了。但我想通过频繁切换颜色,让播放机知道获奖的四张光盘在哪里。我不熟悉线程和编程中的时间概念 我也成功地给了用户这个指示,但是在我关闭应用程序之后,控制台仍然会给出输出,也是在我使用setOnCloseRequest时 其他几个问题:Java 程序不';请不要在关闭应用程序后停止,java,multithreading,events,javafx,Java,Multithreading,Events,Javafx,我试着重新创建连接四,我成功了。但我想通过频繁切换颜色,让播放机知道获奖的四张光盘在哪里。我不熟悉线程和编程中的时间概念 我也成功地给了用户这个指示,但是在我关闭应用程序之后,控制台仍然会给出输出,也是在我使用setOnCloseRequest时 其他几个问题: 1:对于我使用html名称的颜色,最好使用十六进制三元组还是不使用首选项。 2:为了防止网格和其他元素占据屏幕左侧,我添加了一个与背景颜色相同的边框,有更好的方法吗? 3:我没有创建将keycode转换为整数的方法,而是在init函数中
1:对于我使用html名称的颜色,最好使用十六进制三元组还是不使用首选项。
2:为了防止网格和其他元素占据屏幕左侧,我添加了一个与背景颜色相同的边框,有更好的方法吗?
3:我没有创建将keycode转换为整数的方法,而是在init函数中创建的。我这样做是因为我不知道如何传递keyevent。如何做到这一点 代码如下:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class FourInARow extends Application {
GridPane boardGrid = new GridPane();
Label[][] labels = new Label[7][7];
Label statusLabel = new Label();
int[][] cell = new int[7][6];
int player = 0;
int won = 0;
String baseStyle = "-fx-background-radius: 40; -fx-min-width: 80; -fx-min-height: 80; -fx-alignment: center; -fx-border-width: 2; -fx-border-color: #000000;-fx-background-color: ";
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
public static void main(String[] args){launch(args);}
private void init(Stage window){
createLabels();
startGame();
Label above = new Label("Try to connect four discs in a row!");
above.setStyle("-fx-font-size: 30; -fx-alignment: center; -fx-min-width: 600");
boardGrid.setStyle("-fx-background-color: silver;-fx-border-color: #F4F4F4;-fx-border-width: 0 20 0 20");
Button newGame = new Button("New Game");
newGame.setStyle("-fx-min-width: 100;-fx-font-size:20");
newGame.setOnAction(e -> startGame());
statusLabel.setStyle("-fx-font-size: 30;-fx-alignment: center; -fx-min-width: 300;");
HBox below = new HBox();
below.setStyle("-fx-border-width: 0 0 0 20;-fx-border-color: #F4F4F4");
below.getChildren().addAll(newGame, statusLabel);
VBox layout = new VBox();
layout.getChildren().addAll(above, boardGrid, below);
Scene scene = new Scene(layout, 600, 620);
scene.setOnKeyPressed(e -> {
if (won == 0) {
try {
String k = e.getCode().toString();
int l = k.length();
int col = Integer.parseInt(k.substring(l - 1, l)) - 1;
placeDisc(col, player);
switchPlayer();
updateScreen();
} catch (NumberFormatException | ArrayIndexOutOfBoundsException error) {
System.out.println("error: " + error);
}
}
});
window.setScene(scene);
window.setTitle("Connect Four");
threadThing();
}
private void threadThing() {
service.scheduleAtFixedRate(() -> {
try {
wonStyle();
} catch (InterruptedException e) {
e.printStackTrace();
}
}, 0, 1, TimeUnit.SECONDS);
}
private void startGame() {
cell = new int[7][6];
won = player = 0;
statusLabel.setText("");
updateScreen();
}
private void updateScreen() {
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 6; j++) {
labels[i][j].setStyle(baseStyle + addStyle(cell[i][j]));
}
labels[i][6].setText(Integer.toString(i+1));
labels[i][6].setStyle("-fx-alignment: center;-fx-min-width: 80;-fx-background-color: #F4F4F4;-fx-font-size: 30;");
}
switch(won) {
case 1: statusLabel.setText("Blue has won!");break;
case 2: statusLabel.setText("Yellow has won!");break;
}
}
private String addStyle(int cell) {
String style = "silver";
switch(cell){
case 1: style = "blue"; break;
case 2: style = "yellow"; break;
case 3: style = "darkblue"; break;
case 4: style = "gold;"; break;
}
return style;
}
private void placeDisc(int col, int player) {
for (int i = 5; i >= 0 ; i--) {
if(cell[col][i] == 0){
cell[col][i] = 1;
if(player == 1) cell[col][i] = 2;
break;
}else{
if(i==0) switchPlayer();
}
}
checkWon();
}
private void checkWon() {
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 6; j++) {
if (cell[i][j] != 0) {
try {
if (cell[i][j] == cell[i][j + 1] && cell[i][j] == cell[i][j + 2] && cell[i][j] == cell[i][j + 3]) {
won = cell[i][j];
cell[i][j] = cell[i][j + 1] = cell[i][j + 2] = cell[i][j + 3] = cell[i][j] + 2;
}
}catch(ArrayIndexOutOfBoundsException error) {}
try {
if (cell[i][j] == cell[i + 1][j] && cell[i][j] == cell[i + 2][j] && cell[i][j] == cell[i + 3][j]) {
System.out.println("Horizontal win");
won = cell[i][j];
cell[i][j] = cell[i + 1][j] = cell[i + 2][j] = cell[i + 3][j] = cell[i][j] + 2;
}
}catch(ArrayIndexOutOfBoundsException error) {}
try {
if (cell[i][j] == cell[i + 1][j + 1] && cell[i][j] == cell[i + 2][j + 2] && cell[i][j] == cell[i + 3][j + 3]) {
won = cell[i][j];
cell[i][j] = cell[i + 1][j + 1] = cell[i + 2][j + 2] = cell[i + 3][j + 3] = cell[i][j] + 2;
}
}catch(ArrayIndexOutOfBoundsException error) {}
try {
if (cell[i][j] == cell [i + 1][j - 1] && cell[i][j] == cell[i + 2][j - 2] && cell[i][j] == cell[i + 3][j - 3]) {
won = cell[i][j];
cell[i][j] = cell[i + 1][j - 1] = cell[i + 2][j - 2] = cell[i + 3][j - 3] = cell[i][j] + 2;
}
}catch(ArrayIndexOutOfBoundsException error) {}
}
}
}
}
private void switchPlayer() {
if(player == 0) player = 2;
player--;
}
private void createLabels() {
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
labels[i][j] = new Label();
boardGrid.add(labels[i][j], i, j);
}
}
}
private void wonStyle() throws InterruptedException {
System.out.println("Test");
boolean run = false;
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 6; j++) {
if(cell[i][j] > 2 && !run){
Thread.sleep(500);
addStyleFlicker();
run = true;
}
}
}
}
private void addStyleFlicker() throws InterruptedException {
String[] styleOne = {"-fx-background-radius: 40; -fx-min-width: 80; -fx-min-height: 80; -fx-alignment: center; -fx-border-width: 2; -fx-border-color: #000000;-fx-background-color: blue;",
"-fx-background-radius: 40; -fx-min-width: 80; -fx-min-height: 80; -fx-alignment: center; -fx-border-width: 2; -fx-border-color: #000000;-fx-background-color: darkblue;"};
String[] styleTwo = {"-fx-background-radius: 40; -fx-min-width: 80; -fx-min-height: 80; -fx-alignment: center; -fx-border-width: 2; -fx-border-color: #000000;-fx-background-color: yellow;",
"-fx-background-radius: 40; -fx-min-width: 80; -fx-min-height: 80; -fx-alignment: center; -fx-border-width: 2; -fx-border-color: #000000;-fx-background-color: gold;"};
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 6; j++) {
if(cell[i][j] == 3){
labels[i][j].setStyle(styleOne[0]);
}else if(cell[i][j] == 4){
labels[i][j].setStyle(styleTwo[0]);
}
}
}
Thread.sleep(500);
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 6; j++) {
if(cell[i][j] == 3){
labels[i][j].setStyle(styleOne[1]);
}else if(cell[i][j] == 4) {
labels[i][j].setStyle(styleTwo[1]);
}
}
}
}
@Override
public void start(Stage window) throws Exception {
init(window);
window.show();
}
}
导入javafx.application.application;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.Label;
导入javafx.scene.layout.GridPane;
导入javafx.scene.layout.HBox;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
导入java.util.concurrent.Executors;
导入java.util.concurrent.ScheduledExecutorService;
导入java.util.concurrent.TimeUnit;
公共类FourInARow扩展了应用程序{
GridPane boardGrid=新建GridPane();
标签[][]标签=新标签[7][7];
标签状态标签=新标签();
int[][]单元格=新int[7][6];
int-player=0;
整数=0;
String baseStyle=“-fx背景半径:40;-fx最小宽度:80;-fx最小高度:80;-fx对齐:中心;-fx边框宽度:2;-fx边框颜色:#000000;-fx背景颜色:”;
ScheduledExecutorService=Executors.newSingleThreadScheduledExecutor();
公共静态void main(字符串[]args){launch(args);}
私有void init(后台窗口){
createLabels();
startGame();
标签上方=新标签(“尝试将四张光盘连成一行!”);
setStyle(“-fx字体大小:30;-fx对齐:中心;-fx最小宽度:600”);
boardGrid.setStyle(“-fx背景颜色:银色;-fx边框颜色:#F4;-fx边框宽度:0 20”);
按钮新游戏=新按钮(“新游戏”);
newGame.setStyle(“-fx最小宽度:100;-fx字体大小:20”);
setOnAction(e->startGame());
statusLabel.setStyle(“-fx字体大小:30;-fx对齐:中心;-fx最小宽度:300;”);
下面的HBox=新的HBox();
下面的.setStyle(“-fx边框宽度:020;-fx边框颜色:#F4”);
下面是.getChildren().addAll(新游戏,状态标签);
VBox布局=新的VBox();
layout.getChildren().addAll(上图、boardGrid、下图);
场景=新场景(布局,600620);
场景。按下设置键(e->{
如果(韩元=0){
试一试{
字符串k=e.getCode().toString();
int l=k.长度();
int col=Integer.parseInt(k.substring(l-1,l))-1;
placeDisc(列,播放器);
switchPlayer();
updateScreen();
}捕获(NumberFormatException | ArrayIndexOutOfBoundsException错误){
System.out.println(“错误:+错误”);
}
}
});
window.setScene(场景);
window.setTitle(“连接四”);
threadThing();
}
私人物品{
service.scheduleAtFixedRate(()->{
试一试{
wonStyle();
}捕捉(中断异常e){
e、 printStackTrace();
}
},0,1,时间单位为秒);
}
私有void startGame(){
单元格=新整数[7][6];
赢=玩家=0;
statusLabel.setText(“”);
updateScreen();
}
私有void updateScreen(){
对于(int i=0;i<7;i++){
对于(int j=0;j<6;j++){
标签[i][j].setStyle(baseStyle+addStyle(cell[i][j]);
}
标签[i][6].setText(Integer.toString(i+1));
标签[i][6]。设置样式(“-fx对齐:中心;-fx最小宽度:80;-fx背景色:#F4;-fx字体大小:30;”);
}
开关(韩元){
案例1:statusLabel.setText(“蓝色赢了!”);中断;
案例2:statusLabel.setText(“黄色赢了!”);中断;
}
}
私有字符串addStyle(int单元格){
字符串样式=“银色”;
开关(单元){
案例1:style=“blue”中断;
案例2:style=“yellow”中断;
案例3:style=“darkblue”中断;
案例4:style=“gold;”“break;
}
回归风格;
}
专用void placeDisc(整数列,整数播放器){
对于(int i=5;i>=0;i--){
if(单元格[col][i]==0){
单元格[col][i]=1;
如果(player==1)单元格[col][i]=2;
打破
}否则{
如果(i==0)switchPlayer();
}
}
checkWon();
}
私有void checkWon(){
对于(int i=0;i<7;i++){
对于(int j=0;j<6;j++){
如果(单元[i][j]!=0){
试一试{
如果(单元[i][j]==单元[i][j+1]&单元[i][j]==单元[i][j+2]&单元[i][j]==单元[i][j+3]){
元=单元[i][j];
单元[i][j]=单元[i][j+1]=单元[i][j+2]=单元[i][j+3]=单元[i][j]+2;
}
}catch(ArrayIndexOutOfBoundsException错误){}
试一试{
如果(单元[i][j]==单元[i+1][j]&单元[i][j]==单元[i+2][j]&单元[i][j]==单元[i+3][j]){
System.out.println(“横向赢”);
元=单元[i][j];
单元[i][j]=单元[i+1][j]=单元[i+2][j]=单元[i+3][j]=单元[i][j]+2;
}
}catch(ArrayIndexOutOfBoundsException错误){}
试一试{
如果(单元[i][j]==单元[i+1][j+1]&单元[i][j]==单元[i+2][j+2]&单元[i][j]==单元[i+3][j+3]){
元=单元[i][j];
@Override
public void stop() {
System.exit(0);
}
Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setDaemon(true);
return t;
}
});