首页编程java编程java简单爱心源码(java最简单的代码)

java简单爱心源码(java最简单的代码)

编程之家2026-05-241085次浏览

大家好,java简单爱心源码相信很多的网友都不是很明白,包括java最简单的代码也是一样,不过没有关系,接下来就来为大家分享关于java简单爱心源码和java最简单的代码的一些知识点,大家可以关注收藏,免得下次来找不到哦,下面我们开始吧!

java简单爱心源码(java最简单的代码)

急求一段简单的java源代码(用户名、密码操作界面)

下面的程序可以直接通过编译运行,自己寻找要用到的代码段。

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.FocusEvent;

import java.awt.event.FocusListener;

java简单爱心源码(java最简单的代码)

import java.io.File;

import java.io.IOException;

import java.io.RandomAccessFile;

import javax.swing.BoxLayout;

import javax.swing.JFrame;

import javax.swing.JLabel;

java简单爱心源码(java最简单的代码)

import javax.swing.JPanel;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

public class UserLogin extends JPanel implements ActionListener{

JTextField userjt=null;//用户输入框

JPasswordField pwdjt=null;

JTextField sysUserjt=null;//系统显示用户名输入框

JTextField sysPwdjt=null;

public UserLogin(){

super(new GridLayout(1,2));

JPanel userPanel=new JPanel();//用户界面,左边

userPanel.setLayout(new BoxLayout(userPanel,BoxLayout.Y_AXIS));

this.add(userPanel);

JPanel userUpPanel=new JPanel();//用户界面上半部分

userPanel.add(userUpPanel);

JPanel userDownPanel=new JPanel();//用户界面下半部分

userPanel.add(userDownPanel);

JPanel sysPanel=new JPanel();//系统界面,右边

sysPanel.setLayout(new BoxLayout(sysPanel,BoxLayout.Y_AXIS));

this.add(sysPanel);

JPanel sysUserPanel=new JPanel();//系统界面上半部分

sysPanel.add(sysUserPanel);

JPanel sysPwdPanel=new JPanel();//系统界面下半部分

sysPanel.add(sysPwdPanel);

userjt=new JTextField(5);

userjt.setText("用户名");

userUpPanel.add(userjt);

pwdjt=new JPasswordField(5);

pwdjt.setText("密码");

pwdjt.setEchoChar('\0');

userDownPanel.add(pwdjt);

JLabel sysUserjl=new JLabel("用户名为:");

sysUserPanel.add(sysUserjl);

sysUserjt=new JTextField(5);

sysUserPanel.add(sysUserjt);

JLabel sysPwdjl=new JLabel("密码为:");

sysPwdPanel.add(sysPwdjl);

sysPwdjt=new JTextField(5);

sysPwdPanel.add(sysPwdjt);

userjt.addActionListener(this);

pwdjt.addActionListener(this);

userjt.addFocusListener(new FocusListener(){

public void focusGained(FocusEvent e){

if(userjt.getText().equals("用户名"))

userjt.setText("");

}

public void focusLost(FocusEvent e){

if(userjt.getText().equals(""))

userjt.setText("用户名");

}});

pwdjt.addFocusListener(new FocusListener(){

public void focusGained(FocusEvent e){

if(new String(pwdjt.getPassword()).equals("密码")){

pwdjt.setText("");

pwdjt.setEchoChar('*');

}

}

public void focusLost(FocusEvent e){

if(new String(pwdjt.getPassword()).equals("")){

pwdjt.setText("密码");

pwdjt.setEchoChar('\0');

}

}});

}

public void actionPerformed(ActionEvent e){

if(e.getSource().equals(userjt)){

pwdjt.requestFocus();

}else{

if(new String(pwdjt.getPassword()).equals("")||userjt.getText().equals("")||userjt.getText().equals("用户名")) return;

sysUserjt.setText(userjt.getText());

sysPwdjt.setText(new String(pwdjt.getPassword()));

try{

writetoFile();

} catch(IOException e1){

System.out.println("写入文件发生异常!");

e1.printStackTrace();

}

}

}

private void writetoFile() throws IOException{

File f=new File("User_Psd.txt");

// if(!f.exists()) f.createNewFile();

RandomAccessFile accessFile=new RandomAccessFile(f,"rw");

accessFile.seek(accessFile.length());

accessFile.write(("user:"+userjt.getText()+"\r\npassword:"+new String(pwdjt.getPassword())+"\r\n\r\n").getBytes());

}

public static void main(String args[]){

JFrame jf=new JFrame("用户登陆模块测试");

jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);

jf.add(new UserLogin());

jf.setBounds(400,300,280,150);

jf.setVisible(true);

}

}

高分求两个简单的JAVA设计源代码

上面 wuzhikun12同学写的不错,但我想还不能运行,并且还不太完善。我给个能运行的:(注意:文件名为:Test.java)

//要实现对象间的比较,就必须实现Comparable接口,它里面有个compareTo方法

//Comparable最好使用泛型,这样,无论是速度还是代码量都会减少

@SuppressWarnings("unchecked")

class Student implements Comparable<Student>{

private String studentNo;//学号

private String studentName;//姓名

private double englishScore;//英语成绩

private double computerScore;//计算机成绩

private double mathScore;//数学成绩

private double totalScore;//总成绩

//空构造函数

public Student(){}

//构造函数

public Student(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore){

this.studentNo= studentNo;

this.studentName= studentName;

this.englishScore= englishSocre;

this.computerScore= computerScore;

this.mathScore= mathScore;

}

//计算总成绩

public double sum(){

this.totalScore= englishScore+computerScore+mathScore;

return totalScore;

}

//计算评测成绩

public double testScore(){

return sum()/3;

}

//实现compareTO方法

@Override

public int compareTo(Student student){

double studentTotal= student.getTotalScore();

return totalScore==studentTotal?0:(totalScore>studentTotal?1:-1);

}

//重写toString方法

public String toString(){

return"学号:"+this.getStudentNo()+"姓名:"+this.getStudentName()+"英语成绩:"+this.getEnglishScore()+"数学成绩:"+this.getMathScore()+"计算机成绩:"+this.getComputerScore()+"总成绩:"+this.getTotalScore();

}

//重写equals方法

public boolean equals(Object obj){

if(obj== null){

return false;

}

if(!(obj instanceof Student)){

return false;

}

Student student=(Student)obj;

if(this.studentNo.equals(student.getStudentName())){//照现实来说,比较是不是同一个学生,应该只是看他的学号是不是相同

return true;

} else{

return false;

}

}

/*以下为get和set方法,我个人认为,totalScore的set的方法没必要要,因为它是由其它成绩计算出来的

在set方法中,没设置一次值,调用一次sum方法,即重新计算总成绩

*/

public String getStudentNo(){

return studentNo;

}

public void setStudentNo(String studentNo){

this.studentNo= studentNo;

sum();

}

public String getStudentName(){

return studentName;

}

public void setStudentName(String studentName){

this.studentName= studentName;

sum();

}

public double getEnglishScore(){

return englishScore;

}

public void setEnglishScore(double englishScore){

this.englishScore= englishScore;

sum();

}

public double getComputerScore(){

return computerScore;

}

public void setComputerScore(double computerScore){

this.computerScore= computerScore;

sum();

}

public double getMathScore(){

return mathScore;

}

public void setMathScore(double mathScore){

this.mathScore= mathScore;

sum();

}

public double getTotalScore(){

return totalScore;

}

}

//Student子类学习委员类的实现

class StudentXW extends Student{

//重写父类Student的testScore()方法

@Override

public double testScore(){

return sum()/3+3;

}

public StudentXW(){}

//StudentXW的构造函数

public StudentXW(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore){

super(studentNo,studentName,englishSocre,computerScore,mathScore);

}

}

//Student子类班长类的实现

class StudentBZ extends Student{

//重写父类Student的testScore()方法

@Override

public double testScore(){

return sum()/3+5;

}

public StudentBZ(){}

//StudentXW的构造函数

public StudentBZ(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore){

super(studentNo,studentName,englishSocre,computerScore,mathScore);

}

}

//测试类

public class Test{

public static void main(String[] args){

//生成若干个student类、StudentXW类、StudentBZ类

Student student1= new Student("s001","张三",70.5,50,88.5);

Student student2= new Student("s002","李四",88,65,88.5);

Student student3= new Student("s003","王五",67,77,90);

StudentXW student4= new StudentXW("s004","李六",99,88,99.5);

StudentBZ student5= new StudentBZ("s005","朱漆",56,65.6,43.5);

Student[] students={student1,student2,student3,student4,student5};

for(int i= 0; i<students.length; i++){

double avgScore= students[i].testScore();

System.out.println(students[i].getStudentName()+"学生的评测成绩为:"+ avgScore+"分");

}

}

}

运行结果为:

张三学生的评测成绩为:69.66666666666667分

李四学生的评测成绩为:80.5分

王五学生的评测成绩为:78.0分

李六学生的评测成绩为:98.5分

朱漆学生的评测成绩为:60.03333333333333分

java小游戏源代码

介绍这个给你把...我空间还有很多..

importjava.applet.Applet;

importjava.applet.AudioClip;

importjava.awt.Dimension;

importjava.awt.Font;

importjava.awt.Toolkit;

importjavax.sound.sampled.AudioFileFormat;

importjavax.sound.sampled.AudioSystem;

importjavax.swing.JFrame;

importjavax.swing.JPanel;

importjava.awt.Rectangle;

importjavax.swing.BorderFactory;

importjavax.swing.JButton;

importjavax.swing.JOptionPane;

importjavax.swing.JSlider;

importjavax.swing.JLabel;

importjavax.swing.SwingUtilities;

importjavax.swing.UIManager;

importjavax.swing.event.ChangeEvent;

importjavax.swing.event.ChangeListener;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.awt.event.KeyAdapter;

importjava.awt.event.KeyEvent;

importjava.io.File;

importjava.util.Vector;

publicclassFrameextendsJFrameimplementsRunnable{

JPanelcontentPane;

JPaneljPanel1=newJPanel();

JButtonjButton1=newJButton();

JSliderjSlider1=newJSlider();

JLabeljLabel1=newJLabel();

JButtonjButton2=newJButton();

JLabeljLabel2=newJLabel();

intcount=1,rapidity=80;//count当前进行的个数,rapidity游标的位置

intzhengque=0,cuowu=0;

intrush[]={10,20,30};//游戏每关的个数可以自由添加.列{10,20,30,40,50}

intrush_count=0;//记录关数

charlist[]={'A','B','C','D','E','F','G','H','I','J','K','L',

'M','N','O','P','Q','R','S','T','U','V','W','X','Y',

'Z','1','2','3','4','5','6','7','8','9'};//随机出现的数字可以自由添加

Vectornumber=newVector();

Stringpaiduan="true";

AudioClipMusci_anjian,Music_shibai,Music_chenggong;

publicFrame(){

try{

setDefaultCloseOperation(EXIT_ON_CLOSE);

//-----------------声音文件---------------------

Musci_anjian=Applet.newAudioClip(newFile("sounds//anjian.wav")

.toURL());

Music_shibai=Applet.newAudioClip(newFile("sounds//shibai.wav")

.toURL());

Music_chenggong=Applet.newAudioClip(newFile(

"sounds//chenggong.wav").toURL());

//---------------------------------------

jbInit();

}catch(Exceptionexception){

exception.printStackTrace();

}

}

/**

*Componentinitialization.

*

*@throwsjava.lang.Exception

*/

privatevoidjbInit()throwsException{

contentPane=(JPanel)getContentPane();

contentPane.setLayout(null);

setSize(newDimension(588,530));

setTitle("FrameTitle");

jPanel1.setBorder(BorderFactory.createEtchedBorder());

jPanel1.setBounds(newRectangle(4,4,573,419));

jPanel1.setLayout(null);

jButton1.setBounds(newRectangle(277,442,89,31));

jButton1.setText("开始");

jButton1.addActionListener(newFrame1_jButton1_actionAdapter(this));

jSlider1.setBounds(newRectangle(83,448,164,21));

jSlider1.setMaximum(100);

jSlider1.setMinimum(1);

jSlider1.setValue(50);

jLabel1.setText("速度");

jLabel1.setBounds(newRectangle(35,451,39,18));

jButton2.setBounds(newRectangle(408,442,89,31));

jButton2.setText("结束");

jButton2.addActionListener(newFrame1_jButton2_actionAdapter(this));

jLabel2.setText("第一关:100个");

jLabel2.setBounds(newRectangle(414,473,171,21));

contentPane.add(jPanel1);

contentPane.add(jButton2);

contentPane.add(jButton1);

contentPane.add(jSlider1);

contentPane.add(jLabel1);

contentPane.add(jLabel2);

this.addKeyListener(newMyListener());

jButton1.addKeyListener(newMyListener());

jSlider1.addKeyListener(newMyListener());

jSlider1.addChangeListener(newChangeListener(){

publicvoidstateChanged(ChangeEvente){

rapidity=jSlider1.getValue();

}

});

}

publicvoidrun(){

number.clear();

zhengque=0;

cuowu=0;

paiduan="true";

while(count<=rush[rush_count]){

try{

Threadt=newThread(newTthread());

t.start();

count+=1;

Thread.sleep(1000+(int)(Math.random()*2000));//生产下组停顿时间

//最快1快.最慢2秒

}catch(InterruptedExceptione){

e.printStackTrace();

}

}

while(true){//等待最后一个字符消失

if(number.size()==0){

break;

}

}

if(zhengque==0){//为了以后相除..如果全部正确或者错误就会出现错误.所以..

zhengque=1;

}

if(cuowu==0){

cuowu=1;

}

if(paiduan.equals("true")){//判断是否是自然结束

if(zhengque/cuowu>=2){

JOptionPane.showMessageDialog(null,"恭喜你过关了");

rush_count+=1;//自动加1关

if(rush_count<rush.length){

if(rapidity>10){//当速度大于10的时候在-5提加速度.怕速度太快

rapidity-=5;//速度自动减10毫秒

jSlider1.setValue(rapidity);//选择位置

}

Threadt=newThread(this);

t.start();

}else{

JOptionPane.showMessageDialog(null,"牛B...你通关了..");

rush_count=0;

count=0;

}

}else{

JOptionPane.showMessageDialog(null,"请再接再励");

rush_count=0;

count=0;

}

}else{

rush_count=0;

count=0;

}

}

publicvoidjButton1_actionPerformed(ActionEvente){

Threadt=newThread(this);

t.start();

}

publicvoidjButton2_actionPerformed(ActionEvente){

count=rush[rush_count]+1;

paiduan="flase";

}

classTthreadimplementsRunnable{

publicvoidrun(){

booleanfo=true;

intY=0,X=0;

JLabelshow=newJLabel();

show.setFont(newjava.awt.Font("宋体",Font.PLAIN,33));

jPanel1.add(show);

X=10+(int)(Math.random()*400);

Stringparameter=list[(int)(Math.random()*list.length)]+"";

Beanbean=newBean();

bean.setParameter(parameter);

bean.setShow(show);

number.add(bean);

show.setText(parameter);

while(fo){

//---------------------数字下移--------------------

show.setBounds(newRectangle(X,Y+=2,33,33));

try{

Thread.sleep(rapidity);

}catch(InterruptedExceptione){

e.printStackTrace();

}

if(Y>=419){

fo=false;

for(inti=number.size()-1;i>=0;i--){

Beanbn=((Bean)number.get(i));

if(parameter.equalsIgnoreCase(bn.getParameter())){

cuowu+=1;

jLabel2.setText("正确:"+zhengque+"个,错误:"+cuowu

+"个");

number.removeElementAt(i);

Music_shibai.play();

break;

}

}

}

}

}

}

classMyListenerextendsKeyAdapter{

publicvoidkeyPressed(KeyEvente){

Stringuu=e.getKeyChar()+"";

for(inti=0;i<number.size();i++){

Beanbean=((Bean)number.get(i));

if(uu.equalsIgnoreCase(bean.getParameter())){

zhengque+=1;

number.removeElementAt(i);

bean.getShow().setVisible(false);

jLabel2.setText("正确:"+zhengque+"个,错误:"+cuowu+"个");

Music_chenggong.play();

break;

}

}

Musci_anjian.play();

}

}

publicstaticvoidmain(String[]args){

try{

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}catch(Exceptionexception){

exception.printStackTrace();

}

Frameframe=newFrame();

DimensionscreenSize=Toolkit.getDefaultToolkit().getScreenSize();

DimensionframeSize=frame.getSize();

if(frameSize.height>screenSize.height){

frameSize.height=screenSize.height;

}

if(frameSize.width>screenSize.width){

frameSize.width=screenSize.width;

}

frame.setLocation((screenSize.width-frameSize.width)/2,

(screenSize.height-frameSize.height)/2);

frame.setVisible(true);

}

}

classFrame1_jButton2_actionAdapterimplementsActionListener{

privateFrameadaptee;

Frame1_jButton2_actionAdapter(Frameadaptee){

this.adaptee=adaptee;

}

publicvoidactionPerformed(ActionEvente){

adaptee.jButton2_actionPerformed(e);

}

}

classFrame1_jButton1_actionAdapterimplementsActionListener{

privateFrameadaptee;

Frame1_jButton1_actionAdapter(Frameadaptee){

this.adaptee=adaptee;

}

publicvoidactionPerformed(ActionEvente){

adaptee.jButton1_actionPerformed(e);

}

}

classBean{

Stringparameter=null;

JLabelshow=null;

publicJLabelgetShow(){

returnshow;

}

publicvoidsetShow(JLabelshow){

this.show=show;

}

publicStringgetParameter(){

returnparameter;

}

publicvoidsetParameter(Stringparameter){

this.parameter=parameter;

}

}

好了,文章到此结束,希望可以帮助到大家。

怎么免费给自己建网站(怎么免费创建个人网站)前端开发是青春饭吗?软件工程师是青春饭吗