java程序代码大全(java语言代码大全)
大家好,今天来为大家解答java程序代码大全这个问题的一些问题点,包括java语言代码大全也一样很多人还不知道,因此呢,今天就来为大家分析分析,现在让我们一起来看看吧!如果解决了您的问题,还望您关注下本站哦,谢谢~
求java程序代码
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
/**
*一个简单的Swing窗口,输入内容单击“确定”按钮后,在文本域中显示输入的内容。
*单击“取消”按钮,清空页面内容。
*@author yzg
*
*/
public class Test extends JFrame{
private static final long serialVersionUID= 1L;
private JLabel nameLabel;
private JLabel pwdLabel;
private JTextField name;
private JTextField pwd;
public Test(String title){
super(title);
this.getContentPane().setLayout(null);
//下面两行是取得屏幕的高度和宽度
double lx= Toolkit.getDefaultToolkit().getScreenSize().getWidth();
double ly= Toolkit.getDefaultToolkit().getScreenSize().getHeight();
this.setLocation(new Point((int)(lx/ 2)- 150,(int)(ly/ 2)- 200));//设定窗口出现位置
this.setSize(340, 440);//设定窗口大小
}
public void showWin(){
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//姓名
nameLabel= new JLabel("姓名:");
nameLabel.setBounds(30, 10, 50, 25);
name= new JTextField();
name.setBounds(80, 10, 120, 20);
pwdLabel= new JLabel("密码:");
pwdLabel.setBounds(30, 40, 50, 55);
pwd= new JPasswordField();
pwd.setBounds(80, 40, 120, 50);
//确定按钮
JButton ok= new JButton("确定");
ok.setBounds(50, 190, 60, 25);
ok.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e){
if("admin".equals(name.getText())&&"123456".equals(pwd.getText())){
JOptionPane.showMessageDialog(getContentPane(),"登录成功");
}else{
JOptionPane.showMessageDialog(getContentPane(),"登录不成功");
}
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
});
//取消按钮
JButton cancel= new JButton("取消");
cancel.setBounds(120, 190, 60, 25);
cancel.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
});
this.getContentPane().add(nameLabel);
this.getContentPane().add(pwdLabel);
this.getContentPane().add(name);
this.getContentPane().add(pwd);
this.getContentPane().add(ok);
this.getContentPane().add(cancel);
// this.pack();
this.setVisible(true);
}
/**
*@param args
*/
public static void main(String[] args){
Test reg= new Test("test");
reg.showWin();
}
}
第二个
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
*一个简单的Swing窗口,输入内容单击“确定”按钮后,在文本域中显示输入的内容。
*单击“取消”按钮,清空页面内容。
*@author yzg
*
*/
public class Test extends JFrame{
private static int count=0;
private static final long serialVersionUID= 1L;
public Test(String title){
super(title);
this.getContentPane().setLayout(null);
//下面两行是取得屏幕的高度和宽度
double lx= Toolkit.getDefaultToolkit().getScreenSize().getWidth();
double ly= Toolkit.getDefaultToolkit().getScreenSize().getHeight();
this.setLocation(new Point((int)(lx/ 2)- 150,(int)(ly/ 2)- 200));//设定窗口出现位置
this.setSize(340, 440);//设定窗口大小
}
public void showWin(){
//确定按钮
JButton ok= new JButton("确定");
ok.setBounds(50, 190, 60, 25);
ok.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e){
count++;
JOptionPane.showMessageDialog(getContentPane(),count);
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
});
this.getContentPane().add(ok);
// this.pack();
this.setVisible(true);
}
/**
*@param args
*/
public static void main(String[] args){
Test reg= new Test("test");
reg.showWin();
}
}
求java程序代码,200行左右
/**
需求:奖客富翁游戏。
菜单为:
1.注册用户信息。填写用户名,密码,电脑随机输出一个4位
数字作为会员卡号。提示用户名,密码,及会员卡号。可选择第二步了
2.登陆。校验是否已注册(是否存在会员卡号)。如果为空,
提示用户不存在。
如果用户名通过,密码错误,提示用户名和密码不匹配。如果
验证成功结束。如果输入的用户名密码均匹配,给出登陆成功提示。
可选择第三步了。
3.先判断用户是否已登陆(是否存在会员卡)
询问是否进行抽奖,选Y,电脑随机输出5个4位
数字。如果其中有一个数字与用户卡号相同,则为中奖,奖励
人民币为卡号数字的金额。否则,谢谢参与。程序退出。
*/
importjava.util.Scanner;
importjava.util.Random;
publicclassRichPerson{
publicstaticvoidmain(String[]args){
//输出菜单。
for(inta=0;a<6;a++){
System.out.print("*");
}
System.out.print("欢迎进入奖客富翁系统");
for(intb=0;b<6;b++){
System.out.print("*");
}
System.out.print("
\t\t1.注册
"+"\t\t2.登录
"+"\t\t3.抽奖
");
for(intc=0;c<31;c++){
System.out.print("*");
}
//输出菜单结束。
intcaidan=0;//操作菜单
Stringname=null;//会员注册名字
Stringmyname=null;//会员登录名字
Stringpassword=null;//会员注册密码
Stringmypassword=null;//会员登录密码
intnumber=0;//会员卡号
//intluckynumber=0;//幸运号码
Stringstr=null;//询问是否重试
//输入数字选择菜单。
Scannerinput=newScanner(System.in);
Randomoutput=newRandom();
do{
System.out.println("
请选择菜单:");
caidan=input.nextInt();
switch(caidan){
case1:
System.out.println("欢迎注册富翁,请依次输入用户名和密码");
System.out.println("用户名:");
name=input.next();
System.out.println("密码:");
password=input.next();
//随机给出4位数的会员卡号
number=output.nextInt(8999)+1000;
System.out.println("注册成功!您的用户名:"+name+",密码:"+password
+",会员卡号:"+number+"。请牢记!");
break;
case2:
//先判断用户是否已注册。
if(name==null){
System.out.println("您好,请先注册!");
break;
}else{
System.out.println("欢迎登录富翁,请依次输入用户名和密码!");
//用户名和密码匹配校验次数设置为3次。
for(intx=0;x<3;x++){
System.out.println("用户名:");
myname=input.next();
if(!myname.equals(name)){
System.out.println("用户"+myname+"不存在!请重试,剩余次数"
+(2-x)+"次!");
}else{
System.out.println("密码:");
}
mypassword=input.next();
if(!mypassword.equals(password)){
System.out.println(myname+"密码错误!"+"请重试,剩余次数"
+(2-x)+"次!");
}else{
System.out.println("登陆成功!");
break;
}
}
}
break;
case3:
//判断用户是否已注册,是否已登录。
if(mypassword==null){
System.out.println("您好,请先登录!");
break;
}elseif(!mypassword.equals(password)){
System.out.println("您好,请先重试登录!");
break;
}elseif(mypassword.equals(password)){
System.out.println("欢迎进入富翁,请问是否进行幸运抽奖(y/n)?");
Stringchoujiang=input.next();
if(choujiang.equals("y")){
int[]luckynumber=newint[5];
for(intm=0;m<5;m++){
luckynumber[m]=output.nextInt(8999)+1000;
}
for(intm=0;m<5;m++){
System.out.print(luckynumber[m]+"\t");
}
System.out.println("
"+number);
for(intm=0;m<5;m++){
if(number==luckynumber[m]){
System.out.println("
中奖!金额为:"+luckynumber);
break;
}else{
System.out.println("
木有中奖!");
break;
}
}
利用java编写代码实现如下功能,需要全部代码
很简单的应用,为了节省字数,代码注释我就不加了
首先是显示层,LoinWindow:
importjava.awt.FlowLayout;
importjava.awt.GridBagConstraints;
importjava.awt.GridBagLayout;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.FocusEvent;
importjava.awt.event.FocusListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
importjavax.swing.border.EmptyBorder;
publicclassLoinWindowextendsJFrameimplementsActionListener,FocusListener{
privateJPanelmainPanel,namePanel,btnPanel;
privateJTextFieldtfName,tfPsd;
privateJButtonbtnLogin,btnCancel;
privatestaticfinalintWIDTH=300;
privatestaticfinalintHEIGHT=200;
privateLoginServiceservice=newLoginService();
publicLoinWindow(){
super("登录窗体");
}
publicvoidlaunch(){
setSize(WIDTH,HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
GridLayoutmainLayout=newGridLayout(2,1);
mainLayout.setVgap(10);
mainPanel=newJPanel(mainLayout);
GridBagLayoutnameLayout=newGridBagLayout();
namePanel=newJPanel(nameLayout);
namePanel.setBorder(newEmptyBorder(10,10,10,10));
JLabelnameLabel=newJLabel("姓名:");
tfName=newJTextField();
JLabelpsdLabel=newJLabel("密码:");
tfPsd=newJTextField();
JLabelblank=newJLabel("");
namePanel.add(nameLabel);
namePanel.add(tfName);
namePanel.add(blank);
namePanel.add(psdLabel);
namePanel.add(tfPsd);
GridBagConstraintss=newGridBagConstraints();
s.fill=GridBagConstraints.BOTH;
s.gridwidth=1;
s.weightx=0;
s.weighty=0;
nameLayout.setConstraints(nameLabel,s);
s.gridwidth=0;
s.weightx=1;
s.weighty=0;
nameLayout.setConstraints(tfName,s);
s.gridwidth=0;
s.weightx=4;
s.weighty=0;
nameLayout.setConstraints(blank,s);
s.gridwidth=1;
s.weightx=0;
s.weighty=0;
nameLayout.setConstraints(psdLabel,s);
s.gridwidth=3;
s.weightx=1;
s.weighty=0;
nameLayout.setConstraints(tfPsd,s);
FlowLayoutbtnLayout=newFlowLayout();
btnLayout.setAlignment(FlowLayout.CENTER);
btnPanel=newJPanel(btnLayout);
btnLogin=newJButton("确定");
btnCancel=newJButton("取消");
btnPanel.add(btnLogin);
btnPanel.add(btnCancel);
btnCancel.addActionListener(this);
btnLogin.addActionListener(this);
mainPanel.add(namePanel);
mainPanel.add(btnPanel);
setContentPane(mainPanel);
tfName.addFocusListener(this);
tfPsd.addFocusListener(this);
pack();
setSize(WIDTH,HEIGHT);
setLocationRelativeTo(null);
}
@Override
publicvoidactionPerformed(ActionEvente){
Objectsource=e.getSource();
if(source==btnCancel){
System.exit(0);
}elseif(source==btnLogin){
Stringusername=tfName.getText();
Stringpassword=tfPsd.getText();
booleansuccess=service.login(username,password);
if(success){
warn("成功","登录成功!");
}else{
warn("失败","您输入的用户名或密码错误!");
}
}
}
@Override
publicvoidfocusGained(FocusEventarg0){
}
@Override
publicvoidfocusLost(FocusEvente){
Objectsource=e.getSource();
if(source==tfName){
Stringusername=tfName.getText();
try{
service.matchUsername(username);
}catch(LoginExceptione1){
warn("验证错误",e1.getMessage());
}
}elseif(source==tfPsd){
Stringpassword=tfPsd.getText();
try{
service.matchPassword(password);
}catch(LoginExceptione1){
warn("验证错误",e1.getMessage());
}
}
}
privatevoidwarn(Stringtitle,Stringmsg){
JOptionPane.showMessageDialog(null,msg,title,JOptionPane.INFORMATION_MESSAGE);
}
publicstaticvoidmain(String[]args){
newLoinWindow().launch();
}
}然后是模型层:LoginDao
publicclassLoginDao{
publicbooleanlogin(Stringusername,Stringpassword){
if(username.equals("admin")&&password.equals("12345")){
returntrue;
}
returnfalse;
}
}LoginService
importjava.util.regex.Pattern;
publicclassLoginService{
privatestaticfinalPatternLOGIN_PATTERN=Pattern.compile("[a-zA-Z]+");
privatestaticfinalPatternPASSWORD_PATTERN=Pattern.compile("[1-9]+");
privateLoginDaodao=newLoginDao();
publicbooleanmatchUsername(Stringusername)throwsLoginException{
if(null==username||username.isEmpty()){
returnfalse;
}
if(!LOGIN_PATTERN.matcher(username).matches()){
thrownewLoginException("您输入的用户名不合法,请输入英文!");
}
returntrue;
}
publicbooleanmatchPassword(Stringpassword)throwsLoginException{
if(null==password||password.isEmpty()){
returnfalse;
}
if(!PASSWORD_PATTERN.matcher(password).matches()){
thrownewLoginException("您输入的密码不合法,请输入数字!");
}
returntrue;
}
publicbooleanlogin(Stringusername,Stringpassword){
if(null==username||username.isEmpty()){
returnfalse;
}
if(null==password||password.isEmpty()){
returnfalse;
}
if(!dao.login(username,password)){
returnfalse;
}
returntrue;
}
}LoginException
publicclassLoginExceptionextendsException{
publicLoginException(Stringarg0){
super(arg0);
}
}不知道分层设计思想是不是我想的这样
好了,本文到此结束,如果可以帮助到大家,还望关注本站哦!