導航:首頁 > 源碼編譯 > java項目編程源碼

java項目編程源碼

發布時間:2023-09-02 05:15:07

㈠ 求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);
}

閱讀全文

與java項目編程源碼相關的資料

熱點內容
吃雞國際體驗服為什麼伺服器繁忙 瀏覽:92
php中sleep 瀏覽:488
vr怎麼看視頻演算法 瀏覽:84
手機app如何申報個人所得稅零申報 瀏覽:692
如何截獲手機app連接的ip 瀏覽:330
冰箱壓縮機是否需要電容 瀏覽:344
python列表每一行數據求和 瀏覽:274
自己有一台伺服器可以玩什麼 瀏覽:656
社會學波普諾pdf 瀏覽:584
解壓做食物的小視頻 瀏覽:758
pdf怎麼單獨設置文件夾 瀏覽:474
業務邏輯程序員 瀏覽:659
addto新建文件夾什麼意思 瀏覽:161
有伺服器地址怎麼安裝軟體 瀏覽:660
安卓如何完全清除數據 瀏覽:691
安卓安卓證書怎麼信任 瀏覽:54
伺服器被攻擊如何解決 瀏覽:221
學霸變成程序員 瀏覽:883
c語言編譯錯誤fatalerror 瀏覽:443
ipv4內部伺服器地址怎麼分配 瀏覽:464