我无法在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上设置结果时,“lblResultat”显示了玩家1的结果,并且当打印出玩家2结果时擦除了玩家的一个结果时,我想做类似的事情
lblResultat.setText(player score 1 + player score 2);
玩家得分由其变量“resultat”替换
thxxxxx
答案 0 :(得分:1)
append(...)
方法添加其他文字行,而不是setText(...)
方法。setBounds()
可能看起来像创建复杂GUI的最简单和最好的方式,但是你创建的Swing GUI越多,使用它们时会遇到更严重的困难。当GUI调整大小时,它们不会调整组件的大小,它们是增强或维护的皇室女巫,当它们被放置在滚动窗格中时它们完全失败,当它们在所有平台上观看时或者与原始平台不同的屏幕分辨率时它们看起来很糟糕