導航:首頁 > 源碼編譯 > 45個java源碼

45個java源碼

發布時間:2023-05-18 19:36:32

Ⅰ 求編寫一個超級簡單的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();
}
}
}

Ⅱ 求java版畫圖程序的源代碼

找到了,很久以前寫的一個簡單畫圖,呵呵~當時要求用AWT寫,很難受。

package net.miqiang.gui;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;

/**
* 簡單畫圖板程序
* 好久沒用 AWT 了,寫起來真別扭,如果用 swing 會很舒服,有空再改寫吧。
*
* @author 米強
*
*/
public class TestMain extends Frame {
// 畫板
private Palette palette = null;

// 顯示當前顏色的面板
private Panel nonceColor = null;

// 畫筆粗細
private Label drawWidth = null;

// 畫筆端點的裝飾
private Label drawCap = null;

// 選取顏色按鈕的監聽事件類
private ButtonColorAction buttonColorAction = null;

// 滑鼠進入按鈕後游標樣式的監聽事件類
private ButtonCursor buttonCursor = null;

// 畫筆樣式的監聽事件
private ButtonStrokeAction buttonStrokeAction = null;

/**
* 構造方法
*
*/
public TestMain() {
// 設置標題欄文字
super("簡易畫圖板");

// 構造一個畫圖板
palette = new Palette();

Panel pane = new Panel(new GridLayout(2, 1));

// 畫筆顏色選擇器
Panel paneColor = new Panel(new GridLayout(1, 13));

// 12 個顏色選擇按鈕
Button [] buttonColor = new Button[12];
Color [] color = {Color.black, Color.blue, Color.cyan, Color.darkGray, Color.gray, Color.green, Color.magenta, Color.orange, Color.pink, Color.red, Color.white, Color.yellow};
// 顯示當前顏色的面板
nonceColor = new Panel();
nonceColor.setBackground(Color.black);
paneColor.add(nonceColor);
buttonColorAction = new ButtonColorAction();
buttonCursor = new ButtonCursor();
for(int i = 0; i < buttonColor.length; i++){
buttonColor[i] = new Button();
buttonColor[i].setBackground(color[i]);
buttonColor[i].addActionListener(buttonColorAction);
buttonColor[i].addMouseListener(buttonCursor);
paneColor.add(buttonColor[i]);
}
pane.add(paneColor);

// 畫筆顏色選擇器
Panel paneStroke = new Panel(new GridLayout(1, 13));

// 控制畫筆樣式
buttonStrokeAction = new ButtonStrokeAction();
Button [] buttonStroke = new Button[11];
buttonStroke[0] = new Button("1");
buttonStroke[1] = new Button("3");
buttonStroke[2] = new Button("5");
buttonStroke[3] = new Button("7");
buttonStroke[4] = new Button("9");
buttonStroke[5] = new Button("11");
buttonStroke[6] = new Button("13");
buttonStroke[7] = new Button("15");
buttonStroke[8] = new Button("17");
buttonStroke[9] = new Button("■");
buttonStroke[10] = new Button("●");
drawWidth = new Label("3", Label.CENTER);
drawCap = new Label("●", Label.CENTER);
drawWidth.setBackground(Color.lightGray);
drawCap.setBackground(Color.lightGray);
paneStroke.add(drawWidth);
for(int i = 0; i < buttonStroke.length; i++){
paneStroke.add(buttonStroke[i]);
buttonStroke[i].addMouseListener(buttonCursor);
buttonStroke[i].addActionListener(buttonStrokeAction);
if(i <= 8){
buttonStroke[i].setName("width");
}else{
buttonStroke[i].setName("cap");
}
if(i == 8){
paneStroke.add(drawCap);
}
}
pane.add(paneStroke);

// 將畫筆顏色選擇器添加到窗體中
this.add(pane, BorderLayout.NORTH);

// 將畫圖板添加到窗體中
this.add(palette);

// 添加窗口監聽,點擊關閉按鈕時退出程序
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

// 設置窗體 ICON 圖標
this.setIconImage(Toolkit.getDefaultToolkit().createImage("images/palette.png"));

// 設置窗口的大小
this.setSize(new Dimension(400, 430));
// 設置窗口位置,處於屏幕正中央
this.setLocationRelativeTo(null);
// 顯示窗口
this.setVisible(true);
}

/**
* 程序入口
*
* @param args
* 字元串數組參數
*/
public static void main(String[] args) {
new TestMain();
}

/**
* 選取顏色按鈕的監聽事件類
* @author 米強
*
*/
class ButtonColorAction implements ActionListener {

public void actionPerformed(ActionEvent e) {
Color color_temp = ((Button)e.getSource()).getBackground();
nonceColor.setBackground(color_temp);
palette.setColor(color_temp);
}

}

/**
* 滑鼠進入按鈕變換游標樣式監聽事件類
* @author 米強
*
*/
class ButtonCursor extends MouseAdapter {

public void mouseEntered(MouseEvent e) {
((Button)e.getSource()).setCursor(new Cursor(Cursor.HAND_CURSOR));
}

public void mouseExited(MouseEvent e) {
((Button)e.getSource()).setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}

}

/**
* 設置畫筆的監聽事件類
* @author 米強
*
*/
class ButtonStrokeAction implements ActionListener {

public void actionPerformed(ActionEvent e) {
Button button_temp = (Button) e.getSource();
String name = button_temp.getName();
if(name.equalsIgnoreCase("width")){
drawWidth.setText(button_temp.getLabel());
palette.setStroke(Float.parseFloat(button_temp.getLabel()));
}else if(name.equalsIgnoreCase("cap")){
drawCap.setText(button_temp.getLabel());
if(button_temp.getLabel().equals("■")){
palette.setStroke(BasicStroke.CAP_SQUARE);
}else if(button_temp.getLabel().equals("●")){
palette.setStroke(BasicStroke.CAP_ROUND);
}
}
}

}

}

/**
* 畫板類
*
* @author 米強
*
*/
class Palette extends Panel implements MouseListener, MouseMotionListener {
// 滑鼠 X 坐標的位置
private int mouseX = 0;

// 上一次 X 坐標位置
private int oldMouseX = 0;

// 滑鼠 Y 坐標的位置
private int mouseY = 0;

// 上一次 Y 坐標位置
private int oldMouseY = 0;

// 畫圖顏色
private Color color = null;

// 畫筆樣式
private BasicStroke stroke = null;

// 緩存圖形
private BufferedImage image = null;

/**
* 構造一個畫板類
*
*/
public Palette() {
this.addMouseListener(this);
this.addMouseMotionListener(this);
// 默認黑色畫筆
color = new Color(0, 0, 0);
// 設置默認畫筆樣式
stroke = new BasicStroke(3.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
// 建立 1280 * 1024 的 RGB 緩存圖象
image = new BufferedImage(1280, 1024, BufferedImage.TYPE_INT_RGB);
// 設置顏色
image.getGraphics().setColor(Color.white);
// 畫背景
image.getGraphics().fillRect(0, 0, 1280, 1024);
}

/**
* 重寫 paint 繪圖方法
*/
public void paint(Graphics g) {
super.paint(g);

// 轉換為 Graphics2D
Graphics2D g2d = (Graphics2D) g;

// 獲取緩存圖形 Graphics2D
Graphics2D bg = image.createGraphics();

// 圖形抗鋸齒
bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

// 設置繪圖顏色
bg.setColor(color);

// 設置畫筆樣式
bg.setStroke(stroke);

// 畫線,從上一個點到新的點
bg.drawLine(oldMouseX, oldMouseY, mouseX, mouseY);

// 將緩存中的圖形畫到畫板上
g2d.drawImage(image, 0, 0, this);
}

/**
* 重寫 update 方法
*/
public void update(Graphics g) {
this.paint(g);
}

/**
* @return stroke
*/
public BasicStroke getStroke() {
return stroke;
}

/**
* @param stroke 要設置的 stroke
*/
public void setStroke(BasicStroke stroke) {
this.stroke = stroke;
}

/**
* 設置畫筆粗細
* @param width
*/
public void setStroke(float width) {
this.stroke = new BasicStroke(width, stroke.getEndCap(), stroke.getLineJoin());
}

/**
* 設置畫筆端點的裝飾
* @param cap 參考 java.awt.BasicStroke 類靜態欄位
*/
public void setStroke(int cap) {
this.stroke = new BasicStroke(stroke.getLineWidth(), cap, stroke.getLineJoin());
}

/**
* @return color
*/
public Color getColor() {
return color;
}

/**
* @param color 要設置的 color
*/
public void setColor(Color color) {
this.color = color;
}

public void mouseClicked(MouseEvent mouseEvent) {
}

/**
* 滑鼠按下
*/
public void mousePressed(MouseEvent mouseEvent) {
this.oldMouseX = this.mouseX = mouseEvent.getX();
this.oldMouseY = this.mouseY = mouseEvent.getY();
repaint();
}

public void mouseReleased(MouseEvent mouseEvent) {
}

/**
* 滑鼠進入棋盤
*/
public void mouseEntered(MouseEvent mouseEvent) {
this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
}

/**
* 滑鼠退出棋盤
*/
public void mouseExited(MouseEvent mouseEvent) {
this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}

/**
* 滑鼠拖動
*/
public void mouseDragged(MouseEvent mouseEvent) {
this.oldMouseX = this.mouseX;
this.oldMouseY = this.mouseY;
this.mouseX = mouseEvent.getX();
this.mouseY = mouseEvent.getY();
repaint();
}

public void mouseMoved(MouseEvent mouseEvent) {
}

}

Ⅲ 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小項目源代碼,供自己學習參考

每個公司用到的技術都不一樣,不過主流的一般是SSH2+jquery。或者spring mvc。等。框架都是大同小異的東西。

Ⅳ 求java小游戲源代碼

[最佳答案] 連連看java源代碼 import javax.swing.*; import java.awt.*; import java.awt.event.*; pu... int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戲按鈕的位...

Ⅵ 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;
}

}
真好無聊中。。

閱讀全文

與45個java源碼相關的資料

熱點內容
穿越之命令與征服將軍 瀏覽:351
android廣播重復 瀏覽:832
像阿里雲一樣的伺服器 瀏覽:318
水冷空調有壓縮機嗎 瀏覽:478
訪問日本伺服器可以做什麼 瀏覽:432
bytejava詳解 瀏覽:448
androidjava7 瀏覽:384
伺服器在山洞裡為什麼還有油 瀏覽:885
天天基金app在哪裡下載 瀏覽:974
伺服器軟路由怎麼做 瀏覽:289
冰箱壓縮機出口 瀏覽:227
OPT最佳頁面置換演算法 瀏覽:644
網盤忘記解壓碼怎麼辦 瀏覽:852
文件加密看不到裡面的內容 瀏覽:653
程序員腦子里都想什麼 瀏覽:431
oppp手機信任app在哪裡設置 瀏覽:187
java地址重定向 瀏覽:272
一年級下冊摘蘋果的演算法是怎樣的 瀏覽:448
程序員出軌電視劇 瀏覽:90
伺服器系統地址怎麼查 瀏覽:56