北邮国院大二java mini project记录贴(二)

2022.5.22

        在eclipse上写java代码,用ctrl+z返回上一步了就点不了重做按钮了。。。每次都不吸取教训,下意识地ctrl+z,还经常撤回过头。。。。

        感谢我的舍友帮我解决了之前的问题,虽然过程极其曲折,曲折到我们同时怀疑自己是否配得上这份文凭

        命令提示符,就是那个cmd,有的程序在里面能运行却不能在eclipse上运行,极有可能是你少了开头的package

        为啥我大二下才能用java呀,之前的作业都是用c语言,指针特别难弄,欸……

        先分享一些我觉得对写这个小项目很有用的、可以单独编译的代码,有一些是网上找的,但是一堆bug,我给修好了

        首先是昨天的问题的解答——

package Mini;
import java.awt.List.*;
import java.util.ArrayList;
public class Test2 {
	public static void main(String[] args) {
        // 创建并初始化 List
        ArrayList<Person1> list = new ArrayList<Person1>();

        list.add(new Person1(1, 30, "北京"));
        list.add(new Person1(2, 20, "西安"));
        list.add(new Person1(3, 40, "上海"));
 

        // 打印 list 集合
        for(int i=0;i<list.size();i++) {
        	System.out.println(list.get(i));
        }
    }
}

class Person1 {
private int id;
private int age;
private String name;

public Person1(int id, int age, String name) {
    this.id = id;
    this.age = age;
    this.name = name;
}
public String toString() {
	return name+age+id;
}
}

        然后是变换label中字体的方法——

package Mini;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test3  {
	public Test3() {
		//字体样式设置方法1
		JFrame f = new JFrame("Wordle Game"); 
		f.setLayout(new FlowLayout( FlowLayout.CENTER));
		f.setSize(400,250);
		f.setLocation(400,300);
		
		JLabel jl1 = new JLabel("床前明月光");
		Font font = new Font("宋体", Font.PLAIN, 25);
		jl1.setFont(font);
		jl1.setForeground(new Color(0,245,255));
		//字体样式设置方法2
		JLabel jl2 = new JLabel("疑是地上霜");	
		f.add(jl1);
		f.add(jl2);
		

		f.setTitle("字体设置");//标题
		f.setSize(500,300);//窗口大小
		f.setLocationRelativeTo(null);//窗口居中
		f.setVisible(true);        
	    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

	public static void main(String[] args) {
		new Test3();//创建窗口实例
	}
}

        最后是弹出窗体实验——

package Mini;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Test4 {
	public Test4(){
		final JFrame jf=new JFrame("弹出窗体实验");
		Dimension screensize=Toolkit.getDefaultToolkit().getScreenSize(); 
		int Swing1x=500;
		int Swing1y=300;
		jf.setBounds(screensize.width/2-Swing1x/2,screensize.height/2-Swing1y/2,Swing1x,Swing1y);
		jf.setVisible(true);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container c=jf.getContentPane();
		c.setBackground(Color.red);
		c.setLayout(null);

		Dimension Swing1size=jf.getSize();
		JButton jb=new JButton("弹出对话窗");
		int jbx=100;
		int jby=30;
		jb.setBounds(Swing1size.width/2-jbx/2,Swing1size.height/2-jby/2,jbx,jby);
		c.add(jb);

		class Dialog1 {
			JDialog jd=new JDialog(jf,"JDialog窗体",true);
			Dialog1(){

				jd.setSize(300,200);
				Container c2=jd.getContentPane();
				c2.setLayout(null);
				JLabel jl=new JLabel("只是一个对话框");
				jl.setBounds(0,20,100,100);
				JButton jbb=new JButton("确定");
				jbb.setBounds(100,100,60,30);
				c2.add(jl);
				c2.add(jbb);
				jbb.addActionListener(new ActionListener(){
					@Override
					public void actionPerformed(ActionEvent e) {
						jd.dispose();
					}

				});
				System.out.println("点击弹出");

				jd.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

			}
		}

		jb.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				new Dialog1().jd.setVisible(true);//弹出对话框
				System.out.println("弹出对话框");
			}
		});
		System.out.println("初始");
	}

	public static void main(String[] args){
		new Test4();
	}
}

        最后附上今天完成的代码,已经完成字母颜色配对这一大难关啦!(特别长的代码,还没简化,大概400行)

package Mini;
import javax.swing.*;
import java.awt.List.*;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.*;
import java.awt.Color;
import java.awt.Font;
//import java.util.stream.Collectors;

class oneword {
	private int charterr;//0是用户输入,1是设定的单词,定好了后续不会改
	private int wordnumber;//被猜单词是表中的第几个,从1开始,用户输入的单词默认为0,需要SET GET
	private int timess;//第几次输入,从1开始,是设定的单词默认为9,需要SET GET
	private int bgcolor;//背景色,0灰1黄2绿,是设定的单词默认为9,需要SET GET
	private int seatt;//位置,从1开始,需要SET GET
	private char contentt;//内容,需要GET

	public oneword(int charterr, int wordnumber,int timess,int bgcolor,int seatt, char contentt) {
		this.charterr = charterr;
		this.wordnumber = wordnumber;
		this.timess = timess;
		this.bgcolor = bgcolor;
		this.seatt = seatt;
		this.contentt = contentt;
	}
	
	public void setWordnumber(int wordnumber) {
		this.wordnumber=wordnumber;
	}
	public int getWordnumber() {
		return wordnumber;
	}
	
	public void setTimess(int timess) {
		this.timess=timess;
	}
	public int getTimess() {
		return timess;
	}

	public void setBgcolor(int bgcolor) {
		this.bgcolor=bgcolor;
	}
	public int getBgcolor() {
		return bgcolor;
	}
	
	public void setSeatt(int seatt) {
		this.seatt=seatt;
	}
	public int getSeatt() {
		return seatt;
	}
	
	public char getContentt() {
		return contentt;
	}
	
	public String toString() {
		return charterr+" "+wordnumber+" "+timess+" "+bgcolor+" "+seatt+" "+contentt;//测试返回值
	}
}
 
 
public class Test {

	static int wordnumber=0;//暂时没用
	static int tss=0;//第几次输入
	static ArrayList<oneword> list = new ArrayList<oneword>();
	static ArrayList<oneword> putin = new ArrayList<oneword>();
	
	public static void main(String[] args) {
//      index0();
		index1();//测试一下假如设定好了GAMER
		addwords();
		putin.add(new oneword(0,0,1,1,1,'A'));//由于putin最开始是空的,难以测试

		
  }
	
	public static void addwords() {
		list.add(new oneword(1,1,9,9,1,'G'));
        list.add(new oneword(1,1,9,9,2,'A'));
        list.add(new oneword(1,1,9,9,3,'M'));
        list.add(new oneword(1,1,9,9,4,'E'));
        list.add(new oneword(1,1,9,9,5,'R'));

        for(int i=0;i<list.size();i++) {
        	System.out.println(list.get(i));//测试返回值       	
        	char cc=list.get(i).getContentt();
        	 
//        	System.out.println(cc);//修改进度
        }
	}
	
	
    
    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转成一个字符串数组,并判断是否符合
    	int length=str.length();
    	int judge=1;//判定为1才能输入
    	System.out.print(length);//输入字符的长度
    	System.out.println();
    	if(length!=5) {
    		judge=0;
    	}
    	for (char c : str.toCharArray()) {
    		if(c<'A'||c>'Z') {
    			judge=0;
    		}
//    		System.out.print("'" + c + "',");
    	}
    	if(judge==0) {  		
    		JFrame ff1 = new JFrame(" "); 
      		JDialog nochoose = new JDialog(ff1,"Hint");//没能输入正确的五个大写字母,弹出提示
      		nochoose.setBounds(500,400,400,100);
      		nochoose.setLayout(new FlowLayout( FlowLayout.CENTER));
            Box vBox0 = Box.createVerticalBox();
            vBox0.add(new Label("Failure to enter the correct five capital letters."));
            nochoose.add(vBox0);   
            nochoose.setVisible(true);
            nochoose.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);   	
    	
    	}else if(judge==1) {  	
    		tss=tss+1;//输入的次数
    		matching(str,tss);//判断输入内容并给予输入的次数   		
    	}	
    	System.out.println();
    }
    
    public static void matching(String str,int tss) {//后续改成返回int的函数?
    	int ct=0;//ct是输入字母的位置
    
    	
    	for (char c : str.toCharArray()) {//每个字母都matching一遍
    		ct=ct+1;//字母位置从1开始
    		int[] hh= {0,0,0,0,0};//带着位置和内容的c分别与gamer对比,只内容一样就是黄1,内容和位置一样就是绿2,取最大值
    		int ccolor=0;
    		
    		for(int i=0;i<list.size();i++) {//输入的字母与list里的每行的content匹配,但有个问题,list里只有gamer,
  			
            	
            	char cc=list.get(i).getContentt();
            	int cs=list.get(i).getSeatt();

        		if(c==cc) {
        			hh[i]=1;
        			if(ct==cs) {
        				hh[i]=2;
        			}    			
        		}       		
        		
            }
    		Arrays.sort(hh);
    		ccolor=hh[4];//color取最大值
    		putin.add(new oneword(0,0,tss,ccolor,ct,c));
    		System.out.print(tss+","+ccolor+","+ct+","+c);//颜色是对的了
    		System.out.println();//先搁置
    		
//    		System.out.print("'" + c + "',");
    	}
    	

    }

    public static char record(int i) {//后续用来写历史记录的
//    	int a=putin.get(0).getBgcolor();
    	char cs='t';
    	for(int r=0;r<putin.size();r++) {//输入的字母与list里的每行的content匹配,但有个问题,list里只有gamer,
  			
        	
        	
        	cs=putin.get(r).getContentt();
        	System.out.print(cs);
        	System.out.print(" zzy ");
    	}
    	return cs;

//    	System.out.print("DQY ");
    }
    
    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,400);
            	  
            	  Box vBox0 = Box.createVerticalBox();
                  vBox0.add(new Label("Write down the 5-letter English word as you guess."));
                  play1.add(vBox0);
                  
            	  	JTextArea area1 = new JTextArea(5,20);   			
          			area1.setLineWrap(true);
          			play1.add(area1);//添加区域
          			JButton button = new JButton("CHECK");
          			play1.add(button);
          			button.addActionListener(new AbstractAction() {
          				public void actionPerformed(ActionEvent e) {
//          				area.append("NNN");
          				System.out.println(wordnumber);
              			String a=area1.getText();
              			System.out.println(a);
              			exec(a);
          				}
          			});
          			
          			
          			
          			
//          			JTextArea area2 = new JTextArea(5,20);  
          			area2.setLayout(new FlowLayout( FlowLayout.CENTER));//这么写没用啊,有什么办法能改变框框的位置吗
//          			area2.setLineWrap(true);
//          			play1.add(area2);//添加区域
          			
          			Box vBox1 = Box.createVerticalBox();
          			for(int i=0;i<5;i++) {
          				
          				if(i==0) {
          					char y=record(i);
          					vBox1.add(new Label("The content of your first attempt is "+y));
          					
          				}else if(i==1) {
          					vBox1.add(new Label("The content of your second attempt is "));
          				}else if(i==2) {
          					vBox1.add(new Label("The content of your third attempt is "));
          				}else if(i==3) {
          					vBox1.add(new Label("The content of your forth attempt is "));
          				}else if(i==4) {
          					vBox1.add(new Label("The content of your fifth attempt is "));
          				}else if(i==5) {
          					vBox1.add(new Label("The content of your final attempt is "));
          				}
          				

          				
          				
//          				int a=putin.get(i).getBgcolor();
//          				String ch1=String.valueOf(a);
//          				vBox1.add(new Label(ch1));//进度
//            			System.out.print(putin.get(i).getBgcolor());
          				
          				play1.add(vBox1);
            		}
                    
                    
//                    JTextField textField = new JTextField(15);
//                    play1.add(textField);
//            
          			
                    
                    
                    
          			
//          			for(int i=0;i<5;i++) {
//            			System.out.print(putin.get(i).getBgcolor());
//            		}
 
          			
 
       		
          			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);
    	
    }
}
 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Javaproject是一个开源的Java编程练手项目,提供了许多具有挑战性的编程任务,适合Java初学者和中级开发人员进行练习和提高。如果你想下载Javaproject,可以在GitHub上搜索Javaproject并找到其代码库。在这里,你可以选择从该项目的主页或Github代码库中下载项目,也可以使用命令行工具,如git clone来获取代码库。下载后,你可以通过导入项目到Eclipse或其他Java开发工具中来开始使用JavaprojectJavaproject包含多个难度级别的编程任务和测试用例,通过完成这些任务,你可以提高自己的Java编程能力和经验。如果你在使用Javaproject时遇到困难或有任何问题,请查看项目的文档和问题解答,或向Javaproject的开发者社区求助。总之,Javaproject对于提升Java编程能力和经验非常有帮助,具有非常高的学习和练习价值。 ### 回答2: 如果您想要下载Java项目,首先需要确定您需要下载的项目类型和源代码类型。在确定项目类型之后,您可以使用GitHub等开源代码托管平台或者SourceForge等资源库下载您需要的Java项目。同时,您也需要查找项目的依赖项,确保您可以正确地编译和运行项目。 如果您是一位Java应用程序员,可以从Eclipse或其他Java IDE的开发环境中下载Java项目。这样,您可以通过Eclipse的用户界面自动化完成以下工作: - 导入已有的Java项目 - 编译Java项目 - 调试Java代码 - 导出Java项目 另外,各种涉及Java的开发环境,如Eclipse、NetBeans、IntelliJ IDEA等,还提供了一些工具,如版本控制管理、代码质量评估等。这些工具有助于Java项目团队协作开发和代码管理。 如果您是Java开发新手,您可以首先寻找相关的Java教程、文档和示例。在学习的过程中逐渐了解Java项目的基本架构和原理,并适当地下载一些小型Java项目来练习实践。在编码中注意代码规范,确保您的Java项目兼容性较强。 ### 回答3: javaproject是一个在Java编程语言中使用的开源项目。如果您需要下载javaproject,您可以前往官方网站或者GitHub上进行下载。只需搜索javaproject,就能找到一些相关的链接。 这个项目的典型用例包括:Java SE和Java EE应用程序开发、Java后端服务和Web服务开发以及与不同数据库的连接等等。下载javaproject支持您在Java编程方面的许多需要。 在下载javaproject之前,请确保您已经安装了Java SDK和IDE等必要的开发环境。此外,建议您阅读一些相关的文档和教程以加深对Java编程的了解,并利用Java社区中的一些资源和帮助来支持您的项目。无论您是一位新手还是一位有经验的Java程序员,下载javaproject都将为您提供许多方便和帮助,使您能够更好地开发Java应用程序和服务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值