㈠ 求java源代码 ~~~~!!!!!!
我给你一个计算器的源代码,这个好讲。自己前几天写的,已经被网络收录了。
import java.awt.Button; import java.awt.Color; import java.awt.Frame; import java.awt.GridLayout; import java.awt.Panel; import java.awt.TextField; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Calculator extends WindowAdapter implements MouseListener { private String first; private String second; private String operator; private Button zero = new Button("0"); private Button one = new Button("1"); private Button two = new Button("2"); private Button three = new Button("3"); private Button four = new Button("4"谨升); private Button five = new Button("5"); private Button six = new Button("6"); private Button seven = new Button("7"); private Button eight = new Button("8"); private Button nine = new Button("9"); private Button decimal = new Button("."); private Button equal = new Button("="); private Button add = new Button("+"); private Button sub = new Button("-"); private Button mul = new Button("*"); private Button div = new Button("/"); private TextField input = new TextField(); private Button CE = new Button("CE"); private Button DEL = new Button("Del"); 旦晌旦public static void main(String[] args) { new Calculator(); } public Calculator(){ Frame f = new Frame("Calculator"); f.add("North", input); Panel keys = new Panel(); f.add(keys, "Center"); keys.setLayout(new GridLayout(4, 4)); keys.add(seven); keys.add(eight); keys.add(nine); keys.add(add); keys.add(four); keys.add(five); keys.add(six); keys.add(sub); keys.add(one); keys.add(two); keys.add(three); keys.add(mul); keys.add(zero); 模扰keys.add(decimal); keys.add(equal); keys.add(div); zero.addMouseListener(this); one.addMouseListener(this); two.addMouseListener(this); three.addMouseListener(this); four.addMouseListener(this); five.addMouseListener(this); six.addMouseListener(this); seven.addMouseListener(this); eight.addMouseListener(this); nine.addMouseListener(this); add.addMouseListener(this); sub.addMouseListener(this); div.addMouseListener(this); mul.addMouseListener(this); equal.addMouseListener(this); decimal.addMouseListener(this); Panel addtionalPanel = new Panel(); addtionalPanel.setLayout(new GridLayout(1, 2)); addtionalPanel.add(CE); addtionalPanel.add(DEL); CE.setBackground(Color.green); DEL.setBackground(Color.cyan); CE.addMouseListener(this); DEL.addMouseListener(this); f.add("South", addtionalPanel); f.addWindowListener(this); f.setVisible(true); f.setLocation(200, 300); f.setSize(200, 200); } public void windowClosing(WindowEvent e) { System.exit(0); } public void mouseClicked(MouseEvent e) { Button btn = (Button) e.getSource(); String key = btn.getActionCommand().trim(); if(btn == one || btn == two || btn == three || btn == zero || btn == four || btn == five || btn == six || btn == seven || btn == eight || btn == nine){ if(isBlank(operator)){ if(isBlank(first)){ first = key; }else{ first += key; } input.setText(first); }else{ if(isBlank(second)){ second = key; }else{ second += key; } input.setText(second); } }else if(btn == decimal){ if(isBlank(operator)){ if(isBlank(first)){ first = "0."; input.setText(first); }else{ if(first.indexOf(".") == -1){ first += "."; input.setText(first); } } }else{ if(isBlank(second)){ second = "0."; input.setText(second); }else{ if(second.indexOf(".") == -1){ second += "."; input.setText(second); } } } }else if(btn == equal){ if(!isBlank(second) && !isBlank(first) && !isBlank(operator)){ double result = 0.0D; if(operator.equals("+")){ result = Double.parseDouble(first) + Double.parseDouble(second); }else if(operator.equals("-")){ result = Double.parseDouble(first) - Double.parseDouble(second); }else if(operator.equals("*")){ result = Double.parseDouble(first) * Double.parseDouble(second); }else if(operator.equals("/")){ result = Double.parseDouble(first) / Double.parseDouble(second); } int value = (int)Math.round(result); if(value == result){ input.setText(String.valueOf(value)); }else{ input.setText(String.valueOf(result)); } first = String.valueOf(result); second = null; operator = null; } }else if(btn == add || btn == sub || btn == div || btn == mul){ if(!isBlank(first)){ if(!isBlank(operator) && !isBlank(second)){ if(operator.equals("+")){ first = String.valueOf(Double.parseDouble(first) + Double.parseDouble(second)); }else if(operator.equals("-")){ first = String.valueOf(Double.parseDouble(first) - Double.parseDouble(second)); }else if(operator.equals("*")){ first = String.valueOf(Double.parseDouble(first) * Double.parseDouble(second)); }else if(operator.equals("/")){ first = String.valueOf(Double.parseDouble(first) / Double.parseDouble(second)); } second = null; } operator = key; } }else if(btn == CE){ first = null; second = null; operator = null; input.setText(null); }else if(btn == DEL){ if(isBlank(operator)){ if(!isBlank(first)){ first = first.substring(0, first.length() -1); input.setText(first); } }else{ if(!isBlank(second)){ second = second.substring(0, second.length() -1); input.setText(second); } } } } private boolean isBlank(String str){ return str == null || str.trim().equals(""); } public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} }
㈡ java课程设计源代码(急!!!!)
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
public class game21 extends JFrame {
private JLabel label_2;
private int number;
private int sum;
final JLabel label = new JLabel();
final JLabel label_1 = new JLabel();
public static void main(String[] args) {
new game21();
}
public game21() {
super("21点?!");
getContentPane().setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
onClick();
}
});
button.setText("出牌");
button.setBounds(170, 350, 106, 28);
getContentPane().add(button);
label.setBorder(new LineBorder(Color.black, 1, false));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font("", Font.BOLD, 26));
label.setText("背面");
label.setBounds(158, 81, 137, 153);
getContentPane().add(label);
label_1.setText("你已经拥有的牌:");
label_1.setBounds(109, 22, 270, 45);
getContentPane().add(label_1);
this.setBounds(200, 300, 501, 528);
this.setVisible(true);
getContentPane().add(getLabel_2());
}
public int randNumber() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
return (int) (Math.random() * 10 + 1);
}
public void onClick() {
number = this.randNumber();
this.sum += number;
label.setText("" + number);
String strTemp = this.label_1.getText();
strTemp += "" + number + " ";
label_1.setText(strTemp);
String temp = "合计:" + sum;
label_2.setText(temp);
isWin();
}
public void isWin() {
if (sum > 21) {
JOptionPane.showMessageDialog(this, "你输了");
clear();
return;
} else if (sum == 21) {
JOptionPane.showMessageDialog(this, "你赢了");
clear();
return;
} else {
int i = JOptionPane.showOptionDialog(this, "是否继续?", "提示",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE, null, null, null);
if (i == JOptionPane.OK_OPTION) {
onClick();
} else
return;
}
}
private void clear() {
label_2.setText("合计:");
sum = 0;
number = 0;
label_1.setText("你已经拥有的牌:");
}
/**
* @return
*/
protected JLabel getLabel_2() {
if (label_2 == null) {
label_2 = new JLabel();
label_2.setText("合计:");
label_2.setBounds(313, 35, 66, 18);
}
return label_2;
}
}
真好无聊中。。
㈢ 求编写一个超级简单的Java的程序源代码
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login {
public static void main(String args[]) {
LoginFrm frame = new LoginFrm();
}
}
class LoginFrm extends JFrame implements ActionListener{
JLabel nameLabel=new JLabel("用户名:");
JLabel pwdLabel=new JLabel("密码:");
JTextField name=new JTextField(10);
JPasswordField password=new JPasswordField(10);
JButton butnSure=new JButton("确定");
JButton butnCancel=new JButton("取消");
public LoginFrm() {
super("登陆");
setBounds(500, 200, 280, 220);
setVisible(true);
setLayout(null);
nameLabel.setBounds(45, 20, 100, 25);
add(nameLabel);
add(name);
name.setBounds(105, 20, 110, 25);
add(pwdLabel);
pwdLabel.setBounds(45, 60, 100, 25);
add(password);
password.setBounds(105, 60, 110, 25);
add(butnSure);
butnSure.setBounds(45, 100, 80, 25);
add(butnCancel);
butnCancel.setBounds(135, 100, 80, 25);
butnSure.addActionListener(this);
butnCancel.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();//刷新
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() ==butnSure){
System.out.println("用户名:"+name.getText());
System.out.println("密码:"+name.getText());
if("admin".equals(name.getText().trim())&&"123".equals(password.getText().trim())){
this.dispose();
new MainFrm("用户界面",name.getText().trim(),password.getText().trim());
}else {
JOptionPane.showMessageDialog(this, "用户不存在");
}
}else if(e.getSource()==butnCancel){
System.exit(1);
}
}
class MainFrm extends JFrame{
private JLabel info;
public MainFrm(String s,String name,String password) {
super(s);
setBounds(400, 200, 500, 400);
setLayout(new FlowLayout());
info=new JLabel("登陆成功,用户名:"+name+",密码:"+password);
add(info);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
}
}
㈣ Java100行以上源代码,至少五个class以及一个interface,可以简单点
下面是一个可能的Java源代码,它包含了一个接口租冲薯(Shape)和五个类(Circle, Rectangle, Triangle, Square 和 Main)。它的功能是计算不同形状的面积和周长。
//定义一个接口Shape,有两判指个抽象方法:getArea()和getPerimeter()interface Shape { double getArea(); double getPerimeter();
}//定义一个类Circle,实现Shape接口class Circle implements Shape { //定义一个私有属性radius,表示圆的半径
private double radius; //定义一个公有构造方法,用于初始化radius
public Circle(double radius) { this.radius = radius;
} //实现getArea()方法,返回圆的面积
public double getArea() { return Math.PI * radius * radius;
} //实现getPerimeter()方法,返回圆的周长
public double getPerimeter() { return Math.PI * radius * 2;
}
}//定义一个类Rectangle,实现Shape接口class Rectangle implements Shape { //定义两个私有属性width和height,表示矩形的宽度和高度
private double width; private double height; //定义一个公有构造方法,用于初始化width和height
public Rectangle(double width, double height) { this.width = width; this.height = height;
} //实现getArea()方法,返回矩形的面积
public double getArea() { return width * height;
} //实现getPerimeter()方法,返回矩形的周长
public double getPerimeter() { return (width + height) *2;
}
}//定义一个类Triangle,实现Shape接口class Triangle implements Shape { //定义三个私有属性a,b,c表示三角形的三条边长
private double a; private double b; private double c; //定义一个公有构造方法,用于初始化a,b,c,并检查是否满足三角形条件(任意两边之和大于第三边)
public Triangle(double a, double b, double c) throws Exception{ if (a + b > c && a + c > b && b + c > a) {
this.a = a; this.b = b;
this.c = c;
} else {
throw new Exception("Invalid triangle");
}
} //实现getArea()方法,返回三角形的面积(使用海伦公式)
public double getArea() { //计算半周长p
double p = (a + b + c) /2; //计算并返回面积s(使用Math.sqrt()函数求平方根)
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
} //实现getPerimeter()方法,返回三角形的周长
public double getPerimeter(){ return a + b + c;
}
}//定义一个类Square,继承Rectangle类,并重写构造方法和toString()方法class Square extends Rectangle { //重写构造方法,在调用父类构造方法时传入相弊者同的参数side作为width和height
public Square(double side){ super(side, side);
} //重写toString()方法,在原来基础上加上"Square:"前缀,并只显示side属性而不显示width和height属性(使用String.format()函数格式化字符串)
@Override
public String toString(){ return String.format("Square: side=%.2f", super.width); /* 或者直接使用super.getPerimeter()/4作为side */
/* return String.format("Square: side=%.2f", super.getPerimeter()/4); */
/* 注意:不能直接访问super.side属性,
㈤ 求java图形界面树类编程源码举例。类似windows资源管理器那样的。如附图,2层2项即可。
publicvoidinit(){
ContainercontentPane=null;
=newDefaultMutableTreeNode("我的电脑");
1=newDefaultMutableTreeNode("网络");
2=newDefaultMutableTreeNode("硬盘");
1_1=newDefaultMutableTreeNode("无限");
1_2=newDefaultMutableTreeNode("有限");
2_1=newDefaultMutableTreeNode("A");
2_2=newDefaultMutableTreeNode("B");
treeNode.add(treeNode1);
treeNode.add(treeNode2);
treeNode1.add(treeNode1_1);
treeNode1.add(treeNode1_2);
treeNode2.add(treeNode2_1);
treeNode2.add(treeNode2_2);
JTreetree=newJTree(treeNode);
contentPane=getContentPane();
JPaneljp=newJPanel();
jp.add(tree);
contentPane.add(jp);
this.setSize(300,250);
this.setLocation(400,300);
this.setVisible(true);
}