「java代码收集」java代码库

博主:adminadmin 2023-01-15 00:09:09 455

今天给各位分享java代码收集的知识,其中也会对java代码库进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

求一个50行左右的JAVA代码,最好每行带注释,谢谢啦

/*这个相当详细了.

程序也不算太难.而且给老师看的时候效果比较好.因为有图形化界面,又实现一个比较实用的功能.老师会比较高兴的.

建立一个文件名为Change.java就可以编译了*/

/*

* 这个程序实现输入身高算出标准体重,输入体重,算出身高的功能

*/

import java.awt.*; //导入相关类包,这才样使用相应awt图形界面的类

import java.awt.event.*;//同上

public class Change extends Frame { //定义一个类Change, 父类是Frame(图形界面的)

Button b = new Button("互查"); //创建一个按钮的对象b,显示为"互查"

Label l1 = new Label("身高(cm)");//创建一个lable.显示身高

Label l2 = new Label("体重(kg)");//创建一个lable 显示体重

double heigth, weigth; //定义变量

double x, y; //定义变量

TextField tf1 = new TextField(null, 10);//添加Text框

TextField tf2 = new TextField(null, 10);//添加Text框

public Change() {//类的构造函数,完成初始化

super("互查表");//创建窗口,标题为互查表

setLayout(new FlowLayout(FlowLayout.LEFT));//设置布局

add(l1);//把lable 身高放到window里

add(tf1);//把Text 框 放到窗口上

add(l2); //把lable 体重放到window里

add(tf2);//Test放到窗口里

add(b);//把button放到窗口上

pack();//自动放到窗口里排列上边的组件

setVisible(true);//可以让用户看到窗口

addWindowListener(new WindowAdapter() {//如果按 X, 关闭窗口

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

b.addActionListener(new ButtonListener());//添加button监听函数

}

class ButtonListener implements ActionListener {//实现click button时功能操作

public void actionPerformed(ActionEvent e) {//当click调用

if (tf1.getText()!=null) {//检查tf1 test 是否为空

try {//取异常

x = Double.parseDouble(tf1.getText());//字符转为double型

weigth = (x - 100) * 0.9;//算重量

tf2.setText("" + weigth);//显示重量

} catch (NumberFormatException ex) {

tf1.setText("");//如果输入不是数字,设为空

}

}

if (tf1.getText().equals("")==true){//tf1是否为空

y = Double.parseDouble(tf2.getText());//把tf2里的文本转为double 型 的

heigth = y / 0.9 + 100; //算身高根据重量

tf1.setText("" + heigth);}//显示身高

}

}

public static void main(String[] args) {//主函数,程序入口

new Change(); //建立类Change的对象,并调用他的构造函数Change().显示窗口

}

}

java代码调Oracle的存储过程收集表中的数据后将数据录入新建的文件这个过程中文件的字符集编码以哪个为准

一般数据库的编码集与java工作空间字符集是一致的,不一致的话,可以以数据库为基准,也可以取出来后再转码呀

求java小程序代码,500行左右。。大作业用。追加50

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class mypanel extends Panel implements MouseListener

{

int chess[][] = new int[11][11];

boolean Is_Black_True;

mypanel()

{

Is_Black_True = true;

for(int i = 0;i 11;i++)

{

for(int j = 0;j 11;j++)

{

chess[i][j] = 0;

}

}

addMouseListener(this);

setBackground(Color.BLUE);

setBounds(0, 0, 360, 360);

setVisible(true);

}

public void mousePressed(MouseEvent e)

{

int x = e.getX();

int y = e.getY();

if(x 25 || x 330 + 25 ||y 25 || y 330+25)

{

return;

}

if(chess[x/30-1][y/30-1] != 0)

{

return;

}

if(Is_Black_True == true)

{

chess[x/30-1][y/30-1] = 1;

Is_Black_True = false;

repaint();

Justisewiner();

return;

}

if(Is_Black_True == false)

{

chess[x/30-1][y/30-1] = 2;

Is_Black_True = true;

repaint();

Justisewiner();

return;

}

}

void Drawline(Graphics g)

{

for(int i = 30;i = 330;i += 30)

{

for(int j = 30;j = 330; j+= 30)

{

g.setColor(Color.WHITE);

g.drawLine(i, j, i, 330);

}

}

for(int j = 30;j = 330;j += 30)

{

g.setColor(Color.WHITE);

g.drawLine(30, j, 330, j);

}

}

void Drawchess(Graphics g)

{

for(int i = 0;i 11;i++)

{

for(int j = 0;j 11;j++)

{

if(chess[i][j] == 1)

{

g.setColor(Color.BLACK);

g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16);

}

if(chess[i][j] == 2)

{

g.setColor(Color.WHITE);

g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16);

}

}

}

}

void Justisewiner()

{

int black_count = 0;

int white_count = 0;

int i = 0;

for(i = 0;i 11;i++)//横向判断

{

for(int j = 0;j 11;j++)

{

if(chess[i][j] == 1)

{

black_count++;

if(black_count == 5)

{

JOptionPane.showMessageDialog(this, "黑棋胜利");

Clear_Chess();

return;

}

}

else

{

black_count = 0;

}

if(chess[i][j] == 2)

{

white_count++;

if(white_count == 5)

{

JOptionPane.showMessageDialog(this, "白棋胜利");

Clear_Chess();

return;

}

}

else

{

white_count = 0;

}

}

}

for(i = 0;i 11;i++)//竖向判断

{

for(int j = 0;j 11;j++)

{

if(chess[j][i] == 1)

{

black_count++;

if(black_count == 5)

{

JOptionPane.showMessageDialog(this, "黑棋胜利");

Clear_Chess();

return;

}

}

else

{

black_count = 0;

}

if(chess[j][i] == 2)

{

white_count++;

if(white_count == 5)

{

JOptionPane.showMessageDialog(this, "白棋胜利");

Clear_Chess();

return;

}

}

else

{

white_count = 0;

}

}

}

for(i = 0;i 7;i++)//左向右斜判断

{

for(int j = 0;j 7;j++)

{

for(int k = 0;k 5;k++)

{

if(chess[i + k][j + k] == 1)

{

black_count++;

if(black_count == 5)

{

JOptionPane.showMessageDialog(this, "黑棋胜利");

Clear_Chess();

return;

}

}

else

{

black_count = 0;

}

if(chess[i + k][j + k] == 2)

{

white_count++;

if(white_count == 5)

{

JOptionPane.showMessageDialog(this, "白棋胜利");

Clear_Chess();

return;

}

}

else

{

white_count = 0;

}

}

}

}

for(i = 4;i 11;i++)//右向左斜判断

{

for(int j = 6;j = 0;j--)

{

for(int k = 0;k 5;k++)

{

if(chess[i - k][j + k] == 1)

{

black_count++;

if(black_count == 5)

{

JOptionPane.showMessageDialog(this, "黑棋胜利");

Clear_Chess();

return;

}

}

else

{

black_count = 0;

}

if(chess[i - k][j + k] == 2)

{

white_count++;

if(white_count == 5)

{

JOptionPane.showMessageDialog(this, "白棋胜利");

Clear_Chess();

return;

}

}

else

{

white_count = 0;

}

}

}

}

}

void Clear_Chess()

{

for(int i=0;i11;i++)

{

for(int j=0;j11;j++)

{

chess[i][j]=0;

}

}

repaint();

}

public void paint(Graphics g)

{

Drawline(g);

Drawchess(g);

}

public void mouseExited(MouseEvent e){}

public void mouseEntered(MouseEvent e){}

public void mouseReleased(MouseEvent e){}

public void mouseClicked(MouseEvent e){}

}

class myframe extends Frame implements WindowListener

{

mypanel panel;

myframe()

{

setLayout(null);

panel = new mypanel();

add(panel);

panel.setBounds(0,23, 360, 360);

setTitle("单人版五子棋");

setBounds(200, 200, 360, 383);

setVisible(true);

addWindowListener(this);

}

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

public void windowDeactivated(WindowEvent e){}

public void windowActivated(WindowEvent e){}

public void windowOpened(WindowEvent e){}

public void windowClosed(WindowEvent e){}

public void windowIconified(WindowEvent e){}

public void windowDeiconified(WindowEvent e){}

}

public class mywindow

{

public static void main(String argc [])

{

myframe f = new myframe();

}

}

Java程序代码

import java.awt.*;//计算器实例

import java.awt.event.*;

public class calculator

{

public static void main(String args[])

{

MyWindow my=new MyWindow("计算器");

}

}

class MyWindow extends Frame implements ActionListener

{ StringBuffer m=new StringBuffer();

int p;

TextField tex;

Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,jia,jian,cheng,chu,deng,dian,qingling,kaifang;

MyWindow(String s)

{

super(s);

//StringBuffer s2=new StringBuffer();

//String s;

tex=new TextField(18);

b0=new Button(" 0 ");

b1=new Button(" 1 ");

b2=new Button(" 2 ");

b3=new Button(" 3 ");

b4=new Button(" 4 ");

b5=new Button(" 5 ");

b6=new Button(" 6 ");

b7=new Button(" 7 ");

b8=new Button(" 8 ");

b9=new Button(" 9 ");

dian=new Button(" . ");

jia=new Button(" + ");

jian=new Button(" - ");

cheng=new Button(" × ");

chu=new Button(" / ");

deng=new Button(" = ");

qingling=new Button(" 清零 ");

kaifang=new Button(" √ ");

setLayout(new FlowLayout());

add(tex);

add(b0);

add(b1);

add(b2);

add(b3);

add(b4);

add(b5);

add(b6);

add(b7);

add(b8);

add(b9);

add(dian);

add(jia);

add(jian);

add(cheng);

add(chu);

add(kaifang);

add(qingling);

add(deng);

b0.addActionListener(this);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

b5.addActionListener(this);

b6.addActionListener(this);

b7.addActionListener(this);

b8.addActionListener(this);

b9.addActionListener(this);

jia.addActionListener(this);

jian.addActionListener(this);

cheng.addActionListener(this);

chu.addActionListener(this);

dian.addActionListener(this);

deng.addActionListener(this);

qingling.addActionListener(this);

kaifang.addActionListener(this);

setBounds(200,200,160,280);

setResizable(false);//不可改变大小

setVisible(true);

validate();

addWindowListener(new WindowAdapter()

{ public void windowClosing(WindowEvent ee)

{ System.exit(0);

}

});

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==b0)

{

m=m.append("0");

tex.setText(String.valueOf(m));

}

if(e.getSource()==b1)

{

m=m.append("1"); tex.setText(String.valueOf(m));

}

if(e.getSource()==b2)

{

m=m.append("2"); tex.setText(String.valueOf(m));

}

if(e.getSource()==b3)

{

m=m.append("3"); tex.setText(String.valueOf(m));

}

if(e.getSource()==b4)

{

m=m.append("4"); tex.setText(String.valueOf(m));

}

if(e.getSource()==b5)

{

m=m.append("5"); tex.setText(String.valueOf(m));

}

if(e.getSource()==b6)

{

m=m.append("6"); tex.setText(String.valueOf(m));

}

if(e.getSource()==b7)

{

m=m.append("7"); tex.setText(String.valueOf(m));

}

if(e.getSource()==b8)

{

m=m.append("8"); tex.setText(String.valueOf(m));

}

if(e.getSource()==b9)

{

m=m.append("9"); tex.setText(String.valueOf(m));

}

if(e.getSource()==jia)

{

m=m.append("+"); tex.setText(String.valueOf(m));

}

if(e.getSource()==jian)

{

m=m.append("-"); tex.setText(String.valueOf(m));

}

if(e.getSource()==cheng)

{

m=m.append("*"); tex.setText(String.valueOf(m));

}

if(e.getSource()==chu)

{

m=m.append("/"); tex.setText(String.valueOf(m));

}

if(e.getSource()==dian)

{

m=m.append("."); tex.setText(String.valueOf(m));

}

String mm=String.valueOf(m);

int p1=mm.indexOf("+");

int p2=mm.indexOf("-");

int p3=mm.indexOf("*");

int p4=mm.indexOf("/");

if(p1!=-1)

{

p=p1;

}

else if(p3!=-1)

{

p=p3;

}

else if(p2!=-1)

{

p=p2;

}

else if(p4!=-1)

{

p=p4;

}

if(e.getSource()==deng)

{

String m1=mm.substring(0,p);

String m2=mm.substring(p+1);

String ch=mm.substring(p,p+1);

//System.out.println(m1);

//System.out.println(m2);

//System.out.println(ch);

if(ch.equals("+"))

{

float n1=Float.parseFloat(m1);

float n2=Float.parseFloat(m2);

float sum=n1+n2;

String su=String.valueOf(sum);

tex.setText(su);

}

if(ch.equals("-"))

{

float n1=Float.parseFloat(m1);

float n2=Float.parseFloat(m2);

float sum=n1-n2;

String su=String.valueOf(sum);

tex.setText(su);

}

if(ch.equals("*"))

{

float n1=Float.parseFloat(m1);

float n2=Float.parseFloat(m2);

float sum=n1*n2;

String su=String.valueOf(sum);

tex.setText(su);

}

if(ch.equals("/"))

{

float n1=Float.parseFloat(m1);

float n2=Float.parseFloat(m2);

float sum=n1/n2;

String su=String.valueOf(sum);

tex.setText(su);

}

}

if(e.getSource()==qingling)

{StringBuffer kk=new StringBuffer();

m=kk;

tex.setText("0");

// System.out.println(mm);

}

if(e.getSource()==kaifang)

{

String t=tex.getText();

float num=Float.parseFloat(t);

double nub=Math.sqrt(num);

tex.setText(String.valueOf(nub));

}

}

}

北大青鸟设计培训:Java代码的优化方法有哪些?

说到代码优化,每个人或多或少都掌握一到两种方法,但是这样的方法对提升代码运行效率效果不大,最重要是对代码的重视和了解,这样才能提升代码的运行效率。

在进行代码优化的过程中,方法是非常重要的,多掌握几种方法,根据代码的不同情况选择适合的方法进行优化。

下面电脑培训为大家介绍Java代码优化的几种方法。

1、使用指定类、方法的final修饰符具有final修饰符的类不可派生。

在Java核心API中,有许多最终应用程序的例子,例如java.lang.String,整个类都是final。

为类指定final修饰符允许继承类,并且为方法指定final修饰符允许覆盖该方法。

如果将类指定为final,IT培训认为该类的所有方法都是final。

Java编译器将寻找内联所有最终方法的机会。

内联对于提高Java操作的效率非常重要。

这可以将性能平均提高50%。

2、重用对象String对象的使用是非常重要的,StringBuilder/StringBuffer并不是字符串连接。

由于Java虚拟机需要时间来生成对象,所以将来垃圾收集和处理这些对象可能需要一些时间。

因此,生成太多对象将对程序的性能产生很大影响。

3、使用局部变量调用方法时传递的参数以及在调用中创建的临时变量都保存在堆栈中,速度更快。

其他变量(如静态变量和实例变量)在堆中创建并且速度较慢。

此外,岳阳北大青鸟发现在堆栈中创建的变量,当方法完成运行时,内容消失,不需要进行额外的垃圾收集。

4、及时关闭流在Java编程过程中,在执行数据库连接和I/O流操作时要小心。

使用后,北大青鸟岳阳嘉荟校区官网建议应及时关闭以释放资源。

因为这些大型物体的操作会导致系统的大量开销,稍微粗心会导致严重的后果。

急求JAVA源代码,小游戏或者别的

//这是个聊天程序, 在ECLIPSE 运行 Client.java 就可以了。 连接是:localhost

//Server 代码,

package message;

import java.io.*;

import java.net.*;

import java.util.*;

public class Server {

public static void main(String[] args) throws Exception{

System.out.print("Server");

ServerSocket socket=new ServerSocket(8888);

Vector v=new Vector();

while(true){

Socket sk=socket.accept();

DataInputStream in=new DataInputStream(sk.getInputStream());

DataOutputStream out=new DataOutputStream(sk.getOutputStream());

v.add(sk);

new ServerThread(in,v).start();

}

}

}

//ServerThread.java 代码

package message;

import java.net.*;

import java.io.*;

import java.util.*;

public class ServerThread extends Thread{

DataInputStream in;

Vector all;

public ServerThread(DataInputStream in,Vector v){

this.in=in;

this.all=v;

}

public void run()

{

while(true)

{

try{

String s1=in.readUTF();

for(int i=0;iall.size();i++)

{

Object obj=all.get(i);

Socket socket=(Socket)obj;

DataOutputStream out=new DataOutputStream(socket.getOutputStream());

out.writeUTF(s1);

System.out.print(i);

out.flush();

}

System.out.print("Message send over!");

}catch(Exception e){e.printStackTrace();};

}

}

}

//ClientFrame.java 代码

package message;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.net.*;

import java.io.*;

public class ClientFrame extends JFrame implements ActionListener{

JButton b1=new JButton ("SendMessage");

JButton b2=new JButton("Link Server");

JTextField t1=new JTextField(20);

JTextField t2=new JTextField(20);

JLabel l=new JLabel("输入服务器名字:");

JTextArea area=new JTextArea(10,20);

JPanel p1=new JPanel();

JPanel p2=new JPanel();

JPanel p3=new JPanel();

JPanel p4=new JPanel();

Socket socket;

public ClientFrame()

{

this.getContentPane().add(p1);

p2.add(new JScrollPane(area));

p3.add(t1);

p3.add(b1);

p4.add(l);

p4.add(t2);

p4.add(b2);

p2.setLayout(new FlowLayout());

p3.setLayout(new FlowLayout());

p4.setLayout(new FlowLayout());

p1.setLayout(new BorderLayout());

p1.add("North",p2);

p1.add("Center",p3);

p1.add("South",p4);

b1.addActionListener(this);

b2.addActionListener(this);

this.pack();

show();

}

public void actionPerformed(ActionEvent e)

{

if(e.getActionCommand().equals("Link Server"))

{

try{

socket=new Socket(t2.getText(),8888);

b2.setEnabled(false);

JOptionPane.showMessageDialog(this, "Connection Success");

DataInputStream in=new DataInputStream(socket.getInputStream());

new ClientThread(in,area).start();

}

catch(Exception e1){

JOptionPane.showMessageDialog(this, "Connection Error");

e1.printStackTrace();};

}

else if(e.getActionCommand().equals("SendMessage"))

{

try{

DataOutputStream out=new DataOutputStream(socket.getOutputStream());

out.writeUTF(t1.getText());

t1.setText("");

}catch(Exception e1){e1.printStackTrace();};

}

}

}

//ClientThread.java 代码

package message;

import java.net.*;

import java.io.*;

import javax.swing.*;

public class ClientThread extends Thread {

DataInputStream in;

JTextArea area;

public ClientThread(DataInputStream in,JTextArea area){

this.in=in;

this.area=area;

}

public void run()

{

while(true){

try{

String s=in.readUTF();

area.append(s);

}

catch(Exception e){e.printStackTrace();};

}

}

}

//Client.java代码

package message;

public class Client {

/**

* @param args

*/

public static void main(String[] args) {

new ClientFrame();

}

}

// 每段代码都是个类,不要弄在一个文件里。 运行 Client.java

good luck to you!

java代码收集的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java代码库、java代码收集的信息别忘了在本站进行查找喔。