单纯的记录帖,一个刚接触java的小白要20天写出一个小项目。。。
昨天做完java的lab5的时候才发现还有一个mini project要写,ddl是下个月的7号,呜呜呜
有些地方加粗,这是我后续要放在报告里的文字
2022.05.21 index0已经初步完成初始界面和设置被猜单词,选择好被猜单词后可以进入index1,index0和index1的区别在于前者点击开始会有“未选择单词”的提示,打算后续再添加一个叫“游戏说明”的按钮,玩家可以点击查看游戏的玩法说明。
设置好之后点击开始,在文本框输入五个大写字母(必须五个大写字母,不够或者不是字母的会报错,输入小写的,我在考虑是让它自动转换成大写、还是也报错);输入完了点击“确认”按钮,错了会出现一行提示并可以继续填,对了会有祝贺词且不能继续填,填空的地方下面会显示历史记录。
现在头疼的问题是,我想用List加上字母的信息,但是报错了
单独建一个名为TestArgs的java文件,也报错
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.List.*;
public class TestArgs {
public static void main(String[] args) {
// 创建并初始化 List
List list = new ArrayList() {{
add(new Person(1, 30, "北京"));
add(new Person(2, 20, "西安"));
add(new Person(3, 40, "上海"));
}};
// 打印 list 集合
list.forEach(p -> {
System.out.println(p);
});
}
}
class Person {
private int id;
private int age;
private String name;
public Person(int id, int age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
}
现在代码还有很多重复的地方,后续把程序差不多写完了再整合成函数,最好界面能再弄美观点
重要的是能先做出来。。。
下面是目前全部代码
package Mini;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.event.*;
public class Test {
static int wordnumber=0;
public static void main(String[] args) {
// index0();
index1();//测试一下假如设定好了GAMER
}
public static void index0() {
JFrame f = new JFrame("Wordle Game");
// f.setLayout(new BorderLayout());//另一种排法,还没解决界面布局
f.setLayout(new FlowLayout( FlowLayout.CENTER));
f.setSize(400,250);
f.setLocation(400,300);
JButton sb = new JButton("Start");
JButton rb = new JButton("Setting");
f.add(sb, BorderLayout.CENTER);
f.add(rb, BorderLayout.WEST);//上面那个想解决,这是尝试1
JButton button1 = new JButton("A");
f.add (button1);
ActionListener a = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == sb) {//当被猜单词还没设置好就选择开始时
System.out.println("You clicked the StartButton");
JDialog nochoose = new JDialog(f,"Hint");
nochoose.setBounds(450,350,400,100);
nochoose.setLayout(new FlowLayout( FlowLayout.CENTER));
Box vBox0 = Box.createVerticalBox();
vBox0.add(new Label("Have not chosen the word to guess yet."));
nochoose.add(vBox0);
nochoose.setVisible(true);
nochoose.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
} else {//设置好单词后直接跳转到一个和初始界面一样的界面,但这个界面可以点击开始并且有定好的被猜单词
System.out.println("Please choosing the word.");
JDialog cw = new JDialog(f,"chooseword");
cw.setLayout(new FlowLayout( FlowLayout.LEFT));
cw.setBounds(450,350,400,200);
JButton button1 = new JButton("GAMER");
button1.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
wordnumber=1;//先设一个数字,万一有用
System.out.println(wordnumber);
index1();
}
});
JButton button2 = new JButton("MUSIC");
button2.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
wordnumber=2;
System.out.println(wordnumber);
}
});
JButton button3 = new JButton("GLOBE");
button3.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
wordnumber=3;
System.out.println(wordnumber);
}
});
cw.add(button1);
cw.add(button2);
cw.add(button3);
cw.setVisible(true);
cw.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
}
};
sb.addActionListener(a);
rb.addActionListener(a);
f.getContentPane().add(sb);
f.getContentPane().add(rb);
// f.pack();//调整到只放得下按钮的大小
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void exec(String str) {//该函数用于把string转成一个字符串数组
for (char c : str.toCharArray()) {
System.out.print("'" + c + "',");
}
System.out.println();
}
public static void index1() {//选了GAMER
JFrame f = new JFrame("Wordle Game");
f.setLayout(new FlowLayout( FlowLayout.CENTER));
f.setSize(400,250);
f.setLocation(400,300);
JButton sb = new JButton("Start");
JButton rb = new JButton("Setting");
JButton button1 = new JButton("A");
f.add (button1);
ActionListener a = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == sb) {//选了GAMER,开始
JDialog play1 = new JDialog(f,"writedown");
play1.setLayout(new FlowLayout( FlowLayout.LEFT));
play1.setBounds(450,350,400,200);
Box vBox0 = Box.createVerticalBox();
vBox0.add(new Label("Write down the 5-letter English word as you guess."));
play1.add(vBox0);
JTextArea area = new JTextArea( 10,20);
area.setLineWrap(true);
play1.add(area);//添加区域
JButton button = new JButton("GAMER");
button.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
// area.append("NNN");
System.out.println(wordnumber);
String a=area.getText();
System.out.println(a);
exec(a);
}
});
play1.add(area);
play1.add(button);
play1.setVisible(true);
play1.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
} else {//重新设置的话……
System.out.println("Please choosing the word.");
JDialog cw = new JDialog(f,"chooseword");
cw.setLayout(new FlowLayout( FlowLayout.LEFT));
cw.setBounds(450,350,400,200);
JButton button1 = new JButton("GAMER");
button1.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
index1();
wordnumber=1;
System.out.println(wordnumber);
}
});
JButton button2 = new JButton("MUSIC");
button2.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
wordnumber=2;
System.out.println(wordnumber);
}
});
JButton button3 = new JButton("GLOBE");
button3.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
wordnumber=3;
System.out.println(wordnumber);
}
});
cw.add(button1);
cw.add(button2);
cw.add(button3);
cw.setVisible(true);
cw.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
}
};
sb.addActionListener(a);
rb.addActionListener(a);
f.getContentPane().add(sb);
f.getContentPane().add(rb);
// f.pack();//调整到只放得下按钮的大小
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}