Java 文本文件和文本文件中的数据显示在GUI文本字段中
直截了当的问题 我希望有关选择和胜负的数据将显示在文本字段中,并不断添加像集合一样的数据,可能需要使用数组列表,这就是我遇到的问题 我不知道如何连接特定的数据将显示在文本字段和添加等 意思是说,当我点击纸张图片和计算机时,例如,它是岩石,所以它会写:Java 文本文件和文本文件中的数据显示在GUI文本字段中,java,swing,user-interface,text-files,read-write,Java,Swing,User Interface,Text Files,Read Write,直截了当的问题 我希望有关选择和胜负的数据将显示在文本字段中,并不断添加像集合一样的数据,可能需要使用数组列表,这就是我遇到的问题 我不知道如何连接特定的数据将显示在文本字段和添加等 意思是说,当我点击纸张图片和计算机时,例如,它是岩石,所以它会写: | Human: Win | PAPER bit ROCK | 程序代码: import java.awt.Color; import java.awt.Container; import java.awt.event.*; import j
| Human: Win | PAPER bit ROCK |
程序代码:
import java.awt.Color;
import java.awt.Container;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class gui2 {
static int humanWon; // use for statistic
static int win=0;
static int total=0;
static int tie=0;
public static void main(String[] args){ // main
gamePanel();// launch main game
introductionPanel(); // launch instruction
}
private static void introductionPanel(){ // give the instruction to the game
String text="RoPaS Game is a strategic game played between\ntwo people. The choices are rock, paper or scissors \n gesture with the hand. Paper covers rock, rock\n crushes scissors,scissors cuts paper.";
JOptionPane.showMessageDialog(null,text, "Introduction", 0, new ImageIcon(System.getProperty("user.dir")+"/image/5.gif"));
}
private static void gamePanel(){ // the main game panel
JFrame frame = new JFrame("RoPaS Game"); //the main frame of the game
Container panel = frame.getContentPane(); // creating a container panel, so we can place buttons where we pleased
panel.setLayout(null);
String[] iconString= new String[3]; // creating icon string name so we can place the directory in with little effort
int[] boundInt= new int[3]; // same idea
for(int i=0; i<=2; i++){ // creating the condtions
iconString[i]=System.getProperty("user.dir")+"/image/"+i+".jpg";
boundInt[i]=60+110*i;
}
JButton b1 = new JButton (" ", new ImageIcon(iconString[0]));
b1.setBackground(Color.white);
b1.setBounds(10,boundInt[0],150,100);
JButton b2 = new JButton (" ", new ImageIcon(iconString[1]));
b2.setBackground(Color.white);
b2.setBounds(10,boundInt[1],150,100);
JButton b3 = new JButton (" ", new ImageIcon(iconString[2]));
b3.setBackground(Color.white);
b3.setBounds(10,boundInt[2],150,100);//creating three buttons
JLabel l1 = new JLabel(new ImageIcon(System.getProperty("user.dir")+"/image/3.jpg"));
l1.setBounds(0, 0, 400, 50);
panel.add(l1);//creating a question button
JButton b4 = new JButton("Cheat");
b4.setContentAreaFilled(false);
b4.setBounds(300, 350, 80, 30); //create a code button, this button will give you an automatic win
JButton b5 = new JButton("Quit"); //quit
b5.setContentAreaFilled(false);
b5.setBounds(210, 350, 80, 30);
JTextField b6 = new JTextField("TEXT"); //quit
b6.setBounds(210, 60, 170, 270);
panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(b4);
panel.add(b5); //place button on panel
panel.add(b6);
b1.addActionListener( //next three button will listen for which play pick and calculate the win in computeWinner
new ActionListener() {
public void actionPerformed( ActionEvent event ) {
computeWinner(1);
}
}
);
b2.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event ) {
computeWinner(2);
}
}
);
b3.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event ) {
computeWinner(3);
}
}
);
b4.addActionListener(
new ActionListener() {//cheat button, hit the guy and get a win
public void actionPerformed( ActionEvent event ) {
win=win+1;
total=total+1;
JOptionPane.showMessageDialog(null,"Rack up another win!"+"\nWin/Loss rate: " + win+"/"+total+"\nTie: "+tie,"Cheater do prosper", 0, new ImageIcon(System.getProperty("user.dir")+"/image/4.jpg"));
}
}
);
b5.addActionListener( //quit the game and show three beat up guys
new ActionListener() {
public void actionPerformed( ActionEvent event ) {
String text="Paper: Thank goodness you stop playing!\nThe rock keep trying to break free\n and the scissors keep cutting me!\nRock: Let me out!\nScissors: Damn rock! Snip snip.\n\nAuthor: Thank you for playing and I have\ntake these guys to the hospital now.";
JOptionPane.showMessageDialog(null,text, "Thank you for playing!", 0, new ImageIcon(System.getProperty("user.dir")+"/image/6.gif"));
System.exit(0);
}
}
);
frame.setSize(400, 420);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set frame size and the game begins!
}
public static void computeWinner(int x){ // computing the winner
int computerChoice=computerRandomChoice();
int humanChoice=x;
String text,text1="";
String winningCombination= ""+Math.min(computerChoice, humanChoice)+Math.max(computerChoice, humanChoice);
switch(Integer.parseInt(winningCombination)){
case 12:
text = "| Paper wins |";
if(humanChoice==2) humanWon=1;
break;
case 13:
text = "| Rock wins |";
if(humanChoice==1) humanWon=1;
break;
case 23:
text = "| Scissors wins |";
if(humanChoice==3) humanWon=1;
break;
default: text="| DRAW |";
humanWon=2;
tie=tie+1;
}
if(humanWon==1){
text1="| Human wins | ";
humanWon=0;
win=win+1;
total=total+1;
}else if(humanWon==2){
text1="";
humanWon=0;
}else{
text1="| Computer wins |";
total=total+1;
}
JFrame frame = new JFrame("RoPaS Game");
Container panel = frame.getContentPane();
panel.setLayout(null);
JLabel l0 = new JLabel(text1+text);
l0.setBounds(10, 10, 300, 15);
panel.add(l0);
JLabel l01 = new JLabel("________________________________________________");
l01.setBounds(0, 10, 350, 25);
panel.add(l01);
//show the result in a new splash screen
JLabel l1 = new JLabel("| Human |");
l1.setBounds(60, 35, 150, 35);
panel.add(l1);
JLabel l2 = new JLabel("| Computer |");
l2.setBounds(165, 35, 150, 35);
panel.add(l2);
JLabel l3 = new JLabel(new ImageIcon(System.getProperty("user.dir")+"/image/"+(humanChoice-1)+".jpg"));
l3.setBounds(0, 70, 170, 80);
panel.add(l3);
JLabel l4 = new JLabel(new ImageIcon(System.getProperty("user.dir")+"/image/"+(computerChoice-1)+".jpg"));
l4.setBounds(115, 70,170, 80);
panel.add(l4);
JLabel l015 = new JLabel("________________________________________________");
l015.setBounds(0, 10, 350, 280);
panel.add(l015);
JLabel l5 = new JLabel("Win/Loss rate: " + win+"/"+total);
l5.setBounds(5, 25, 150, 290);
panel.add(l5);
JLabel l6 = new JLabel("Tie: "+tie);
l6.setBounds(5, 30, 125, 310);
panel.add(l6);
frame.setSize(300, 240);
frame.setVisible(true);
frame.setResizable(false);
}
public static int computerRandomChoice(){// creating a random choice of rock paper or scissors by the computer
int result=(int)(Math.random()*3)+1;
return result;
}
}
导入java.awt.Color;
导入java.awt.Container;
导入java.awt.event.*;
导入java.util.Random;
导入javax.swing.*;
公共类GUI 2{
static int humanWon;//用于统计
静态int-win=0;
静态整数总计=0;
静态int-tie=0;
公共静态void main(字符串[]args){//main
gamePanel();//启动主游戏
简介面板();//启动指令
}
private static void introductionPanel(){//为游戏提供说明
String text=“RoPaS游戏是两人之间进行的一种战略游戏。可以选择石头、布或剪刀\n手势。布盖住石头,石头压碎剪刀,剪刀剪纸。”;
showMessageDialog(null,文本,“简介”,0,新图像图标(System.getProperty(“user.dir”)+“/image/5.gif”);
}
私有静态void gamePanel(){//主游戏面板
JFrame=newJFrame(“RoPaS游戏”);//游戏的主框架
Container panel=frame.getContentPane();//创建一个容器面板,这样我们就可以将按钮放在我们喜欢的地方
panel.setLayout(空);
String[]iconString=newstring[3];//创建图标字符串名称,这样我们就可以轻松地将目录放入
int[]boundInt=newint[3];//同样的想法
对于(int i=0;i将结果放入数组中,并通过迭代显示每一轮的数组。或者通过获取字段的文本内容并使用“+”添加新结果来连接结果
应该没什么大不了的我理解它,我知道如何使用数组等等,我不擅长GUI(因为刚开始它),这就是为什么我不确定如何将这个数组放到GUI中使它工作(1)panel.setLayout(null);
不要这样做,它会导致各种问题。2)你的问题是什么?问题:例如,当用户单击ROCK时,计算机生成的剪刀意味着用户获胜,因此我希望将此信息(该用户获胜)放入文本字段,当用户反复单击不同或相同的图片(选择)…信息将自动添加到表格中。。。。