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编程题,自己觉得又点难(求高手写代码)
//Color类
public class Color {
private String colorName;
final public void setColor(String color){
this.colorName = color;
}
public String getColor(){
return this.colorName;
}
}
//White类
public class White extends Color{
public White(){
this.setColor("white");
}
public String getColor() {
return super.getColor();
}
}
//Red类
public class Red extends White{
public Red(){
this.setColor("Red");
}
}
//Prism类
public class Prism {
static public void activePrism(Color c){
if(c.getColor().equals("white")){
Red r = new Red();
Blue b = new Blue();
System.out.println(r.getColor());
System.out.println(b.getColor());
}
else{
return;
}
}
}
//测试类
public class ColorTest {
public static void main(String[] args) {
White w = new White();
Prism.activePrism(w);
}
}
其他颜色自己写吧。
求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();
}
}
求一个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多线程例子,最好有代码,谢谢啦!
package a.b.test;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Calculate1000 implements CallableInteger{
public Calculate1000(){}
public Calculate1000(int a, int b){
this.a = a;
this.b = b;
}
int a;
int b;
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//同步
Calculate1000 ca1 = new Calculate1000();
Date ds1 = new Date();
int result = 0;
for(int i = 1 ; i = 1000 ; i++){
result = ca1.add(i, result);
}
System.out.println(result);
System.out.println("同步用时" + (new Date().getTime() - ds1.getTime()) + "MS");
//异步
Date ds2 = new Date();
result = 0;
ExecutorService es = Executors.newFixedThreadPool(2);
FutureInteger future1 = es.submit(new Calculate1000(1,500));
FutureInteger future2 = es.submit(new Calculate1000(501,1000));
result = future1.get() + future2.get();
System.out.println(result);
System.out.println("异步用时" + (new Date().getTime() - ds2.getTime()) + "MS");
es.shutdown();
}
private int add(int a, int b) throws Exception{
Thread.sleep(10);
return a + b;
}
@Override
public Integer call() throws Exception {
int res = 0;
for(int i = a ; i = b ; i++){
res = this.add(res, i);
}
return res;
}
}
楼主你试一下这段代码行不行,行的话请采纳!
网站标题:java代码例子复杂 java太复杂了
本文来源:http://lswzjz.com/article/hgodje.html