gpt4 book ai didi

java - 在java上打印播放器结果

转载 作者:行者123 更新时间:2023-12-01 10:59:52 25 4
gpt4 key购买 nike

我无法在 Jtextpane 区域打印我的两个玩家的结果:

import java.awt.EventQueue;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import javax.swing.JSeparator;
import java.awt.Color;
import javax.swing.JTextPane;

public class dice {

private JFrame frame;
protected int width;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
dice window = new dice();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public dice() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 784, 945);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

JLabel lblDiceface1 = new JLabel("");
lblDiceface1.setFont(new Font("Calibri", Font.BOLD, 99));
lblDiceface1.setBounds(56, 78, 150, 150);
frame.getContentPane().add(lblDiceface1);

JLabel lblDiceface2 = new JLabel("");
lblDiceface2.setFont(new Font("Calibri", Font.BOLD, 99));
lblDiceface2.setBounds(294, 78, 150, 150);
frame.getContentPane().add(lblDiceface2);

JLabel lbloperatingLabel = new JLabel("Score joueur #1 : \r\n");
lbloperatingLabel.setBounds(56, 34, 500, 30);
frame.getContentPane().add(lbloperatingLabel);

JLabel lblnbdecoup = new JLabel("");
lblnbdecoup.setBounds(575, 183, 132, 29);
frame.getContentPane().add(lblnbdecoup);

JTextPane lblResultat = new JTextPane();
lblResultat.setText("Partie\tJoueur#1\t\tJoueur#2");
lblResultat.setBounds(56, 588, 443, 285);
frame.getContentPane().add(lblResultat);

JButton jouer1 = new JButton("Jouer");
JButton jouer2 = new JButton("Jouer");
JButton btnNouvellePartie = new JButton("Nouvelle partie");
//Set Button enable\disable..........................................................................
jouer1.setEnabled(true);

jouer1.addActionListener(new ActionListener() {
//Declare and inisialize the button clicks.......................................................
int nbDeCoup = 0;

public void actionPerformed(ActionEvent arg0){
//PLAYER1............................................
int face1, face2;
double temp1, temp2;
Icon icon, icon2;
int resultat;

//face1..............................................
temp1 = Math.random() * 6;
face1 = (int) Math.floor(temp1) + 1;

icon = new ImageIcon("dice_" + face1 + ".png");
icon = new ImageIcon(((ImageIcon) icon).getImage().getScaledInstance(150, 150, BufferedImage.SCALE_SMOOTH));
lblDiceface1.setIcon(icon);


//face2...............................................
temp2 = Math.random() * 6;
face2 = (int) Math.floor(temp2) + 1;

icon2 = new ImageIcon("dice_" + face2 + ".png");
icon2 = new ImageIcon(((ImageIcon) icon2).getImage().getScaledInstance(150, 150, BufferedImage.SCALE_SMOOTH));
lblDiceface2.setIcon(icon2);

//print score of the player1..........................
if (face1 == face2){
resultat = (face1 + face2) - (nbDeCoup + 1);
lbloperatingLabel.setText("Score joueur #1 : " + face1 + " + " + face2 + " - " + (nbDeCoup + 1) + " = " + resultat);
//Set Button enable\disable.......................
jouer1.setEnabled(false);
jouer2.setEnabled(true);
lblResultat.setText(resultat);
}


//print button "jouer" clicks of the player1..........
nbDeCoup++;
lblnbdecoup.setText("" + nbDeCoup);
}
});
jouer1.setBounds(575, 133, 132, 34);
frame.getContentPane().add(jouer1);

JSeparator separator = new JSeparator();
separator.setForeground(Color.BLACK);
separator.setBounds(56, 62, 554, 2);
frame.getContentPane().add(separator);

JLabel lbloperatingLabel2 = new JLabel("Score joueur #2 : \r\n");
lbloperatingLabel2.setBounds(56, 271, 554, 30);
frame.getContentPane().add(lbloperatingLabel2);

JSeparator separator_1 = new JSeparator();
separator_1.setForeground(Color.BLACK);
separator_1.setBounds(56, 300, 554, 2);
frame.getContentPane().add(separator_1);

JLabel lblface1 = new JLabel("");
lblface1.setFont(new Font("Calibri", Font.BOLD, 99));
lblface1.setBounds(56, 328, 150, 150);
frame.getContentPane().add(lblface1);

JLabel lblface2 = new JLabel("");
lblface2.setFont(new Font("Calibri", Font.BOLD, 99));
lblface2.setBounds(294, 328, 150, 150);
frame.getContentPane().add(lblface2);

JLabel lblnbdecoup2 = new JLabel("");
lblnbdecoup2.setBounds(575, 419, 132, 29);
frame.getContentPane().add(lblnbdecoup2);

jouer2.setEnabled(false);

jouer2.addActionListener(new ActionListener() {
//Declare and inisialize the button clicks................
int nbDeCoup = 0;

public void actionPerformed(ActionEvent arg0) {

//PLAYER2............................................
int face1, face2;
double temp1, temp2;
Icon icon, icon2;
int resultat;

//face1...............................................
temp1 = Math.random() * 6;
face1 = (int) Math.floor(temp1) + 1;

icon = new ImageIcon("dice_" + face1 + ".png");
icon = new ImageIcon(((ImageIcon) icon).getImage().getScaledInstance(150, 150, BufferedImage.SCALE_SMOOTH));
lblface1.setIcon(icon);

//face2...............................................
temp2 = Math.random() * 6;
face2 = (int) Math.floor(temp2) + 1;

icon2 = new ImageIcon("dice_" + face2 + ".png");
icon2 = new ImageIcon(((ImageIcon) icon2).getImage().getScaledInstance(150, 150, BufferedImage.SCALE_SMOOTH));
lblface2.setIcon(icon2);

//print score of the player1..........................
if (face1 == face2){
resultat = (face1 + face2) - (nbDeCoup + 1);
lbloperatingLabel2.setText("Score joueur #1 : " + face1 + " + " + face2 + " - " + (nbDeCoup + 1) + " = " + resultat);

jouer2.setEnabled(false);
btnNouvellePartie.setEnabled(true);
lblResultat.setText(resultat);
}

//print button "jouer" clicks of the player1..........
nbDeCoup++;
lblnbdecoup2.setText("" + nbDeCoup);
}
});
jouer2.setBounds(575, 363, 132, 34);
frame.getContentPane().add(jouer2);

JLabel lblPointage = new JLabel("Pointage :");
lblPointage.setBounds(56, 542, 200, 30);
frame.getContentPane().add(lblPointage);

//Set Button enable\disable...................................
btnNouvellePartie.setEnabled(false);

btnNouvellePartie.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

}
});
btnNouvellePartie.setBounds(514, 556, 233, 34);
frame.getContentPane().add(btnNouvellePartie);

JButton btnQuiter = new JButton("Quiter");
btnQuiter.setBounds(580, 717, 132, 29);
frame.getContentPane().add(btnQuiter);
}
}

问题是,当我在 Jtextpane 上设置显示玩家 1 结果的“lblResultat”结果时,当打印玩家 2 结果并删除玩家 1 结果时,我想做类似的事情

lblResultat.setText(player score 1 + player score 2);

玩家分数被他们的变量“resultat”替换

thxxxxx

最佳答案

  • 只需使用 JTextArea,而不是 JTextPane
  • 通过 append(...) 方法添加额外的文本行,而不是 setText(...) 方法。
  • 另一种选择是使用 JList,但实际上显示列式数据的最佳选择是使用 JTable
  • 切勿设置 Swing 文本组件的大小,因为这会损坏该组件。
  • 将 JTextArea 放入 JScrollPane 中。
  • 虽然 null 布局和 setBounds() 对于 Swing 新手来说似乎是创建复杂 GUI 的最简单、最好的方法,但您创建的 Swing GUI 越多,使用它们时遇到的困难就越严重。当 GUI 调整大小时,它们不会调整组件的大小,它们是增强或维护的皇家女巫,当放置在滚动 Pane 中时它们完全失败,当在与原始平台或屏幕分辨率不同的所有平台或屏幕分辨率上查看时,它们看起来非常糟糕.

关于java - 在java上打印播放器结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33427568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com