㈠ 急求java學生信息管理系統源代碼,帶有連接資料庫的,萬分感謝
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
public class MainFrame extends JFrame implements ActionListener{
InsertPanel ip = null;
SelectPanel sp = null;
JPanel pframe;
JButton jb1,jb2,jb3;
JMenuItem jm11,jm21,jm22,jm23,jm31,jm32,jm41,jm42;
CardLayout clayout;
public MainFrame(String s){
super(s);
JMenuBar mb = new JMenuBar();
this.setJMenuBar(mb);
JMenu m1 = new JMenu("系統");
JMenu m2 = new JMenu("基本信息");
JMenu m3 = new JMenu("成績");
JMenu m4 = new JMenu("獎懲");
mb.add(m1);
mb.add(m2);
mb.add(m3);
mb.add(m4);
jm11 = new JMenuItem("退出系統");
jm21 = new JMenuItem("輸入");
jm22 = new JMenuItem("查詢");
jm23 = new JMenuItem("更改");
jm31 = new JMenuItem("輸入成績");
jm32 = new JMenuItem("查詢成績");
jm41 = new JMenuItem("獎勵");
jm42 = new JMenuItem("處分");
m1.add(jm11);
m2.add(jm21);
m2.add(jm22);
m2.add(jm23);
m3.add(jm31);
m3.add(jm32);
m4.add(jm41);
m4.add(jm42);
Icon i1 = new ImageIcon();
Icon i2 = new ImageIcon();
Icon i3 = new ImageIcon();
jb1 = new JButton(i1);
jb1.setToolTipText("輸入");
jb2 = new JButton(i2);
jb2.setToolTipText("查詢");
jb3 = new JButton(i3);
jb3.setToolTipText("退出");
JToolBar tb = new JToolBar("系統工具");
tb.add(jb1);
tb.add(jb2);
tb.add(jb3);
add(tb,BorderLayout.NORTH);
jm11.addActionListener(this);
jm21.addActionListener(this);
jm22.addActionListener(this);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
clayout = new CardLayout();
pframe = new JPanel(clayout);
add(pframe);
JPanel mainp = new JPanel(new BorderLayout());
JLabel mainl = new JLabel("學生信息管理平台",SwingConstants.CENTER);
mainl.setFont(new Font("serif",Font.BOLD,30));
mainp.add(mainl);
pframe.add(mainp,"main");
clayout.show(pframe, "main");
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == jm21 || e.getSource() == jb1){
if(ip == null){
ip= new InsertPanel();
pframe.add(ip,"insert");
}
clayout.show(pframe, "insert");
this.setTitle("輸入學生信息");
}
else if(e.getSource() == jm22 || e.getSource() == jb2){
if(sp == null){
sp= new SelectPanel();
pframe.add(sp,"select");
}
clayout.show(pframe, "select");
this.setTitle("查詢學生信息");
}
else if(e.getSource() == jm11 || e.getSource() == jb3){
System.exit(0);
}
}
}
第二個:
import javax.swing.JFrame;
public class MainTest {
public static void main(String [] args){
MainFrame f = new MainFrame("學生信息管理平台");
f.setSize(400,300);
f.setLocation(350,250);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
第二個:
import java.sql.Connection;
import java.sql.DriverManager;
public class MySQLConnection {
static Connection getCon(){
Connection con = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/test","root","123");
}
catch(Exception e){
System.out.println("建立資料庫連接遇到異常!");
}
return con;
}
}
第四個:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class SelectPanel extends JPanel implements ActionListener{
JButton jb;
JTextField jt;
JTextField jt1,jt2,jt3,jt4;
public SelectPanel(){
JLabel jl = new JLabel("請輸入學號:",SwingConstants.CENTER);
jt = new JTextField();
jb = new JButton("確定");
JPanel jp1 = new JPanel(new GridLayout(1,3));
jp1.add(jl);
jp1.add(jt);
jp1.add(jb);
JLabel j1,j2,j3,j4;
j1 = new JLabel("學號:",SwingConstants.CENTER);
j2 = new JLabel("姓名:",SwingConstants.CENTER);
j3 = new JLabel("性別:",SwingConstants.CENTER);
j4 = new JLabel("年齡:",SwingConstants.CENTER);
jt1 = new JTextField(6);
jt1.setEditable(false);
jt2 = new JTextField(6);
jt2.setEditable(false);
jt3 = new JTextField(6);
jt3.setEditable(false);
jt4 = new JTextField(6);
jt4.setEditable(false);
JPanel jp2 = new JPanel(new BorderLayout());
JPanel jp3 = new JPanel(new GridLayout(4,2));
jp2.add(new JLabel(""),BorderLayout.NORTH);
jp3.add(j1);
jp3.add(jt1);
jp3.add(j2);
jp3.add(jt2);
jp3.add(j3);
jp3.add(jt3);
jp3.add(j4);
jp3.add(jt4);
jp2.add(jp3);
this.setLayout(new BorderLayout());
add(jp1,BorderLayout.NORTH);
add(jp2);
jb.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == jb){
String stuNo = jt.getText().trim();
Student s = new Student();
boolean b = true;
try{
b = s.selectByStuNo(stuNo);
}
catch(Exception ex){
System.out.println("查詢學生信息遇到異常!");
}
if(b){
jt1.setText(s.getStuNo());
jt2.setText(s.getName());
jt3.setText(s.getGender());
int a = s.getAge();
Integer i = new Integer(a);
jt4.setText(i.toString());
}
else{
JOptionPane.showMessageDialog(null, "無此學生!");
}
}
}
}
第五個:
import javax.swing.JFrame;
public class SelectTest {
public static void main(String [] args){
JFrame f = new JFrame("查詢學生信息");
SelectPanel p = new SelectPanel();
f.add(p);
f.setSize(400,300);
f.setLocation(300,250);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
第六個:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class Student {
String stuNo;
String name;
String gender;
int age;
public Student(){}
public Student(String stuNo,String name,String gender, int age){
this.stuNo = stuNo;
this.name = name;
this.gender = gender;
this.age = age;
}
public String getStuNo(){
return stuNo;
}
public void setStuNo(String stuNo){
this.stuNo = stuNo;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getGender(){
return gender;
}
public void setGender(String gender){
this.gender = gender;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
public boolean insertStudent(){
boolean b = true;
try{
Connection con = MySQLConnection.getCon();
Statement statement = con.createStatement();
String sql = "insert into student values('" + stuNo + "','" + name +"','" + gender + "'," + age + ")";
sql = new String(sql.getBytes("gb2312"),"ISO8859_1");
statement.executeUpdate(sql);
con.close();
}
catch(Exception e){
b = false;
System.out.println("插入資料庫遇到異常!");
}
return b;
}
public boolean selectByStuNo(String stuNo)throws Exception{
boolean b = true;
Connection con = MySQLConnection.getCon();
Statement statement = con.createStatement();
String sql = "select * from student where stuNo =" + stuNo;
ResultSet rs = statement.executeQuery(sql);
if(rs != null && rs.next()){
String no = rs.getString(1);
this.setStuNo(no);
String n = rs.getString(2);
n = new String(n.getBytes("ISO8859_1"),"gb2312");
this.setName(n);
String g = rs.getString(3);
g = new String (g.getBytes("ISO8859_1"),"gb2312");
this.setGender(g);
this.setAge(rs.getInt(4));
b = true;
}
rs.close();
statement.close();
con.close();
return b;
}
}
資料庫你自己弄吧,我沒時間弄了!初學得多動手哦
㈡ java小程序源代碼,簡單點的,100多行,誰有啊
// My car shop.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class carshop extends JFrame
{
// JPanel to hold all pictures
private JPanel windowJPanel;
private String[] cars = { "","阿斯頓馬丁", "美洲虎", "凱迪拉克",
"羅孚", "勞斯萊斯","別克"};
private int[] jiage = { 0,150000, 260000, 230000,
140000, 290000, 150000};
// JLabels for first snack shown
private JLabel oneJLabel;
private JLabel oneIconJLabel;
// JLabels for second snack shown
private JLabel twoJLabel;
private JLabel twoIconJLabel;
// JLabels for third snack shown
private JLabel threeJLabel;
private JLabel threeIconJLabel;
// JLabels for fourth snack shown
private JLabel fourJLabel;
private JLabel fourIconJLabel;
// JLabels for fifth snack shown
private JLabel fiveJLabel;
private JLabel fiveIconJLabel;
// JLabels for sixth snack shown
private JLabel sixJLabel;
private JLabel sixIconJLabel;
// JTextField for displaying snack price
private JTextArea displayJTextArea;
// JLabel and JTextField for user input
private JLabel inputJLabel;
private JComboBox selectCountryJComboBox;
private JLabel inputJLabel2;
private JTextField inputJTextField2;
// JButton to enter user input
private JButton enterJButton;
//JButton to clear the components
private JButton clearJButton;
// no-argument constructor
public carshop()
{
createUserInterface();
}
// create and position GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
// enable explicit positioning of GUI components
contentPane.setLayout( null );
// set up windowJPanel
windowJPanel = new JPanel();
windowJPanel.setBounds( 10, 20, 340, 200 );
windowJPanel.setBorder( new LineBorder( Color.BLACK ) );
windowJPanel.setLayout( null );
contentPane.add( windowJPanel );
// set up oneIconJLabel
oneIconJLabel = new JLabel();
oneIconJLabel.setBounds( 10, 20, 100, 65 );
oneIconJLabel.setIcon( new ImageIcon( "images/阿斯頓馬丁.jpg" ) );
windowJPanel.add( oneIconJLabel );
// set up oneJLabel
oneJLabel = new JLabel();
oneJLabel.setBounds( 10, 60, 100, 70 );
oneJLabel.setText( "阿斯頓馬丁" );
oneJLabel.setHorizontalAlignment( JLabel.CENTER );
windowJPanel.add( oneJLabel );
// set up twoIconJLabel
twoIconJLabel = new JLabel();
twoIconJLabel.setBounds( 120, 20, 100, 65 );
twoIconJLabel.setIcon( new ImageIcon( "images/美洲虎.jpg" ) );
windowJPanel.add( twoIconJLabel );
// set up twoJLabel
twoJLabel = new JLabel();
twoJLabel.setBounds( 110, 60, 100, 70 );
twoJLabel.setText( "美洲虎" );
twoJLabel.setHorizontalAlignment( JLabel.CENTER );
windowJPanel.add( twoJLabel );
// set up threeIconJLabel
threeIconJLabel = new JLabel();
threeIconJLabel.setBounds( 230, 20, 100, 65 );
threeIconJLabel.setIcon( new ImageIcon(
"images/凱迪拉克.jpg" ) );
windowJPanel.add( threeIconJLabel );
// set up threeJLabel
threeJLabel = new JLabel();
threeJLabel.setBounds( 230, 60, 100, 70);
threeJLabel.setText( "凱迪拉克" );
threeJLabel.setHorizontalAlignment( JLabel.CENTER );
windowJPanel.add( threeJLabel );
// set up fourIconJLabel
fourIconJLabel = new JLabel();
fourIconJLabel.setBounds( 10, 100, 100, 65 );
fourIconJLabel.setIcon( new ImageIcon( "images/羅孚.jpg" ) );
windowJPanel.add( fourIconJLabel );
// set up fourJLabel
fourJLabel = new JLabel();
fourJLabel.setBounds( 10, 150, 50, 70 );
fourJLabel.setText( "羅孚" );
fourJLabel.setHorizontalAlignment( JLabel.CENTER );
windowJPanel.add( fourJLabel );
// set up fiveIconJLabel
fiveIconJLabel = new JLabel();
fiveIconJLabel.setBounds( 120, 100, 100, 65 );
fiveIconJLabel.setIcon( new ImageIcon(
"images/勞斯萊斯.jpg" ) );
windowJPanel.add( fiveIconJLabel );
// set up fiveJLabel
fiveJLabel = new JLabel();
fiveJLabel.setBounds( 110, 150, 100, 70 );
fiveJLabel.setText( "勞斯萊斯" );
fiveJLabel.setHorizontalAlignment( JLabel.CENTER );
windowJPanel.add( fiveJLabel );
// set up sixIconJLabel
sixIconJLabel = new JLabel();
sixIconJLabel.setBounds( 230, 100, 100, 65 );
sixIconJLabel.setIcon( new ImageIcon( "images/別克.jpg" ) );
windowJPanel.add( sixIconJLabel );
// set up sixJLabel
sixJLabel = new JLabel();
sixJLabel.setBounds( 230, 150, 100, 70 );
sixJLabel.setText( "別克" );
sixJLabel.setHorizontalAlignment( JLabel.CENTER );
windowJPanel.add( sixJLabel );
// set up enterJButton
enterJButton = new JButton();
enterJButton.setBounds( 390, 160, 135, 30 );
enterJButton.setText( "Enter" );
contentPane.add( enterJButton );
enterJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when enterJButton is clicked
public void actionPerformed( ActionEvent event )
{
enterJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set up clearJButton
clearJButton = new JButton();
clearJButton.setBounds( 390, 200, 135, 30 );
clearJButton.setText( "Clear" );
contentPane.add( clearJButton );
// set up inputJLabel
inputJLabel = new JLabel();
inputJLabel.setBounds( 390, 25, 135, 25 );
inputJLabel.setText( "Please make selection:" );
contentPane.add( inputJLabel );
selectCountryJComboBox = new JComboBox( cars );
selectCountryJComboBox.setBounds( 390, 50, 135, 21 );
selectCountryJComboBox.setMaximumRowCount( 3 );
contentPane.add( selectCountryJComboBox );
// set up inputJTextField
inputJLabel2 = new JLabel();
inputJLabel2.setBounds( 390, 80, 150, 20 );
inputJLabel2.setText( "Input the Numble:" );
contentPane.add( inputJLabel2 );
// set up inputJTextField
inputJTextField2 = new JTextField();
inputJTextField2.setBounds( 390, 100, 135, 25 );
inputJTextField2.setHorizontalAlignment( JTextField.RIGHT );
contentPane.add( inputJTextField2 );
clearJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when clearJButton is clicked
public void actionPerformed( ActionEvent event )
{
clearJButtonActionPerformed( event );
}
} // end anonymous inner class
);
// set up displayJTextField
displayJTextArea = new JTextArea();
displayJTextArea.setBounds( 10, 237,515, 70 );
displayJTextArea.setEditable( false );
contentPane.add( displayJTextArea );
// set properties of application's window
setTitle( "My car Shop" ); // set title bar string
setSize( 550, 360 ); // set window size
setVisible( true ); // display window
} // end method createUserInterface
private void clearJButtonActionPerformed( ActionEvent event )
{
// clear the JTextFields
inputJTextField2.setText( "" );
displayJTextArea.setText("");
} // end method clearJButtonActionPerformed
private void enterJButtonActionPerformed( ActionEvent event )
{
double z;
double c;
int x;
int y;
x=selectCountryJComboBox.getSelectedIndex();
y=Integer.parseInt(inputJTextField2.getText());
double discountRate;
int amount = Integer.parseInt( inputJTextField2.getText());
switch (amount/5)
{
case 0:
discountRate = 0;
break;
case 1:
discountRate = 1;
break;
case 2:
discountRate = 2;
break;
case 3:
discountRate = 3;
break;
default:
discountRate = 4;
} // end switch statement
c=1-discountRate/100;
z=jiage[x]*y*c;
displayJTextArea.append("你選擇的是:"+cars[x]+";"+
"它的單價是:"+jiage[x]+";" +"你購買該產品的數量是:"+y+"," +"\n"+"該數量的折扣是:"
+discountRate + " %"+";"+"本次消費的總價格是:"+z+"元"+"!"+"\n");
}
public static void main( String args[] )
{
carshop application = new carshop();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
} // end method main
} // end class carshop
㈢ 鐢╦ava緙栧啓:杈撳叆涓涓16浣嶇殑闀挎暣鍨嬫暟,鍒╃敤swith璇鍙ョ粺璁″叾涓涓鍒頒節姣忎釜鏁板瓧鍑虹幇鐨勬℃暟
import java.util.Scanner;
public class Test12 {
public static void main(String[] args) {
int i;
long num, temp;
int[] count = new int[]{0,0,0,0,0,0,0,0,0};
Scanner scan = new Scanner(System.in);
//杈撳叆16浣嶇殑闀挎暣鍨嬫暟
while(true){
System.out.print("input a long number : ");
num = scan.nextLong();
if(num >= 1e15 && num <= 1e16-1){
break;
}
}
//緇熻1~9鐨勬暟瀛楀嚭鐜扮殑棰戠巼
temp = num;
while(temp > 0){
switch((int)(temp % 10)){
case 1:
count[0]++;
break;
case 2:
count[1]++;
break;
case 3:
count[2]++;
break;
case 4:
count[3]++;
break;
case 5:
count[4]++;
break;
case 6:
count[5]++;
break;
case 7:
count[6]++;
break;
case 8:
count[7]++;
break;
case 9:
count[8]++;
break;
default:
break;
}
temp /= 10;
}
//杈撳嚭緇熻$粨鏋
System.out.println(num + " 涓 1 ~ 9 鍑虹幇鐨勯戠巼鍒嗗埆涓猴細");
for(i=0; i<9; i++){
System.out.println((i + 1) + " : " + count[i]);
}
}
}
㈣ 速求java編程:計算器能在科學型與標准型之間切換的源代碼
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**********************Java計算器 主類*********************/
public class Calculator implements ActionListener {
JFrame f;
JMenu mEdit;
JMenu mView;
JMenu mHelp;
JMenuItem mCopy;
JMenuItem mPaste;
JTextField tResult;
JButton bNumber;
JButton bOperator;
JButton bOther;
JButton bM;
boolean isDouble=false;//是否為實數
int opFlag=-1;
static double t1=0,t2=0,t3=0,result=0;
static int opflag1=-1,opflag2=-1,flag=0,resflag=1;
int preOp,currentOp=0;//標准位
double op1=0,op2=0;//操作數
double n3;
StringBuffer buf=new StringBuffer(20);
StringBuffer Board=new StringBuffer(20);//剪貼板
StringBuffer memory=new StringBuffer(20);//M系列
StringBuffer str=new StringBuffer();
//Java計算器 構造器
public Calculator()
{
f = new JFrame("計算器_劉亞會");
Container contentPane = f.getContentPane();
/**************************Java計算器 菜單的創建*****************************/
JMenuBar mBar = new JMenuBar();
mBar.setOpaque(true);
mEdit = new JMenu("編輯(E)");
mEdit.setMnemonic(KeyEvent.VK_E);
mCopy = new JMenuItem("復制(C)");
mEdit.add(mCopy);
mPaste = new JMenuItem("粘貼(P)");
mEdit.add(mPaste);
mView = new JMenu("查看(V)");
mView.setMnemonic(KeyEvent.VK_V);
mView.add(new JMenuItem("標准型"));
mView.add(new JMenuItem("科學型"));
mView.addSeparator();
mView.add(new JMenuItem("查看分組"));
mHelp = new JMenu("幫助(H)");
mHelp.setMnemonic(KeyEvent.VK_H);
mHelp.add(new JMenuItem("幫助主題"));
mHelp.addSeparator();
mHelp.add(new JMenuItem("關於計算器"));
mBar.add(mEdit);
mBar.add(mView);
mBar.add(mHelp);
f.setJMenuBar(mBar);
contentPane.setLayout(new BorderLayout());
JPanel pTop = new JPanel();
tResult = new JTextField("0.",26);
tResult.setHorizontalAlignment(JTextField.RIGHT);
tResult.setEditable(false);
pTop.add(tResult);
contentPane.add(pTop,BorderLayout.NORTH);
JPanel pBottom = new JPanel();
pBottom.setLayout(new BorderLayout());
JPanel pLeft = new JPanel();
pLeft.setLayout(new GridLayout(5,1,3,3));
bM = new JButton(" ");
bM.setEnabled(false);
pLeft.add(bM);
/*************************Java計算器 功能鍵定義***************************/
bOther = new JButton("MC");
bOther.addActionListener(this);
bOther.setForeground(Color.red);
bOther.setMargin(new Insets(3,2,3,2));
pLeft.add(bOther);
bOther = new JButton("MR");
bOther.addActionListener(this);
bOther.setForeground(Color.red);
bOther.setMargin(new Insets(3,2,3,2));
pLeft.add(bOther);
bOther = new JButton("MS");
bOther.addActionListener(this);
bOther.setForeground(Color.red);
bOther.setMargin(new Insets(3,2,3,2));
pLeft.add(bOther);
bOther = new JButton("M+");
bOther.addActionListener(this);
bOther.setForeground(Color.red);
bOther.setMargin(new Insets(3,2,3,2));
pLeft.add(bOther);
pBottom.add(pLeft,BorderLayout.WEST);
JPanel pRight = new JPanel();
pRight.setLayout(new BorderLayout());
JPanel pUp = new JPanel();
pUp.setLayout(new GridLayout(1,3,3,0));
bOther = new JButton("BackSpace");
bOther.addActionListener(this);
bOther.setForeground(Color.red);
bOther.setMargin(new Insets(3,0,3,5));
pUp.add(bOther);
bOther = new JButton("CE");
bOther.addActionListener(this);
bOther.setForeground(Color.red);
pUp.add(bOther);
bOther = new JButton("C");
bOther.addActionListener(this);
bOther.setForeground(Color.red);
pUp.add(bOther);
/***************************Java計算器 數字鍵盤區定義**************************/
JPanel pDown = new JPanel();
pDown.setLayout(new GridLayout(4,5,3,2));
bNumber = new JButton("7");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bNumber = new JButton("8");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bNumber = new JButton("9");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bOperator = new JButton("/");
bOperator.setForeground(Color.red);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,0,3,0));
pDown.add(bOperator);
bOperator = new JButton("sqrt");
bOperator.addActionListener(this);
bOperator.setForeground(Color.red);
bOperator.setMargin(new Insets(3,0,3,0));
pDown.add(bOperator);
bNumber = new JButton("4");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
bNumber.setHorizontalTextPosition(JButton.LEFT);
pDown.add(bNumber);
bNumber = new JButton("5");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bNumber = new JButton("6");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bOperator = new JButton("*");
bOperator.setForeground(Color.red);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
bOperator = new JButton("%");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
bNumber = new JButton("1");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bNumber = new JButton("2");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bNumber = new JButton("3");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bOperator = new JButton("-");
bOperator.setForeground(Color.red);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
bOperator = new JButton("1/x");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
pDown.add(bOperator);
bNumber = new JButton("0");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bOperator = new JButton("+/-");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
bOperator = new JButton(".");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
bOperator = new JButton("+");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
bOperator = new JButton("=");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
pRight.add(pUp,BorderLayout.NORTH);
pRight.add(pDown,BorderLayout.SOUTH);
pBottom.add(pRight,BorderLayout.EAST);
contentPane.add(pBottom,BorderLayout.SOUTH);
f.setSize(new Dimension(320,256));
f.setResizable(false);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
/************************Java計算器 計算方法區***************************/
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if(s.equals("復制(C)"))
{
String temp = tResult.getText().trim();
Board.replace(0, Board.length(), temp);
mPaste.setEnabled(true);
}
else if(s.equals("粘貼(p)"))
{
tResult.setText(Board.toString());
}
else if(s.equals("CE"))
{
//如果是CE則清除文本框
tResult.setText("0.");
}
else if(s.equals("BackSpace"))
{
if(!tResult.getText().trim().equals("0."))
{
//如果文本框中有內容
if(str.length()!=1 && str.length()!=0)
{
tResult.setText(str.delete(str.length()-1,str.length()).toString());
}
else
{
tResult.setText("0.");
str.setLength(0);
}
}
op2 = Double.parseDouble(tResult.getText().trim());
}
else if(s.equals("C"))
{
//如果是C刪除當前計算
tResult.setText("0.");
op1 = op2 = 0;
str.replace(0, str.length(), " ");
preOp = currentOp = 0;
}
else if(s.equals("MC"))
{
//如果是MC則清除緩沖區
String temp = "";
memory.replace(0, memory.length(), temp);
bM.setText(" ");
}
else if(s.equals("MR"))
{
//如果按鍵為MR則恢復緩沖區的數到文本框
tResult.setText(memory.toString());
}
else if(s.equals("MS"))
{
//如果按鍵為MS則將文本框的數存入緩沖區
String s1 = tResult.getText().trim();
memory.replace(0, memory.length(), s1);
bM.setText("M");
}
else if(s.equals("M+"))
{
//如果按鍵為MS則將文本框值與緩沖區的數相加但不顯示結果
String temp1 = tResult.getText().trim();
double dtemp = Double.parseDouble(temp1);
String temp2 = memory.toString();
dtemp += Double.parseDouble(temp2);
temp1 = String.valueOf(dtemp);
memory.replace(0, memory.length(), temp1);
}
else if(s.equals("1/x"))
{
//如果按鍵為1/x則將文本框中的數據為它的倒數
String temp = tResult.getText().trim();
double dtemp = Double.parseDouble(temp);
tResult.setText(""+1/dtemp);
}
else if(s.equals("sqrt"))
{
//如果按鍵為sqrt則將文本框中的內容求平方根
String temp = tResult.getText().trim();
double dtemp = Double.parseDouble(temp);
tResult.setText(""+Math.sqrt(dtemp));
}
else if(s.equals("+"))
{
str.setLength(0);
if(currentOp==0)
{
preOp = currentOp = 1;
op2 = 0;
tResult.setText(""+op1);
}
else
{
currentOp = preOp;
preOp = 1;
switch(currentOp){
case 1:
op1 += op2;
tResult.setText(""+op1);
break;
case 2:
op1 -= op2;
tResult.setText(""+op1);
break;
case 3:
op1 *= op2;
tResult.setText(""+op1);
break;
case 4:
op1 /= op2;
tResult.setText(""+op1);
break;
}
}
}
else if(s.equals("-")){
str.setLength(0);
if(currentOp==0)
{
preOp=currentOp=2;//op1=op2;op2=0;
tResult.setText(""+op1);
}
else
{
currentOp =preOp;
preOp =2;
switch(currentOp){
case 1:op1=op1+op2;tResult.setText(""+op1);break;
case 2:op1=op1-op2;tResult.setText(""+op1);break;
case 3:op1=op1*op2;tResult.setText(""+op1);break;
case 4:op1=op1/op2;tResult.setText(""+op1);break;
}
}
}
else if(s.equals("*"))//*
{
str.setLength(0);
if(currentOp==0)
{
preOp=currentOp=3;//op1=op2;op2=1;
tResult.setText(""+op1);//op1=op2;
}
else
{
currentOp =preOp;
preOp =3;
switch(currentOp){
case 1:op1=op1+op2;tResult.setText(""+op1);break;
case 2:op1=op1-op2;tResult.setText(""+op1);break;
case 3:op1=op1*op2;tResult.setText(""+op1);break;
case 4:op1=op1/op2;tResult.setText(""+op1);break;
}
}
}
else if(s.equals("/"))// /
{
str.setLength(0);
if(currentOp==0)
{
preOp=currentOp=4;//op2=1;
tResult.setText(""+op1);//op1=op2;
}
else
{
currentOp =preOp;
preOp =4;
switch(currentOp){
case 1:op1=op1+op2;tResult.setText(""+op1);break;
case 2:op1=op1-op2;tResult.setText(""+op1);break;
case 3:op1=op1*op2;tResult.setText(""+op1);break;
case 4:op1=op1/op2;tResult.setText(""+op1);break;
}
}
}
else if(s.equals("="))// =
{
if(currentOp==0)
{
str.setLength(0);
tResult.setText(""+op2);
}
else
{
str.setLength(0);
currentOp =preOp;
switch(currentOp){
case 1:op1=op1+op2;tResult.setText(""+op1);break;
case 2:op1=op1-op2;tResult.setText(""+op1);break;
case 3:op1=op1*op2;tResult.setText(""+op1);break;
case 4:op1=op1/op2;tResult.setText(""+op1);break;
}
currentOp=0;
op2=0;
}
}
else if(s.equals("."))
{
isDouble=true;
if(tResult.getText().trim().indexOf('.')!=-1);
else {
if(tResult.getText().trim().equals("0")) {
str.setLength(0);
tResult.setText((str.append("0"+s)).toString());
}
//else if(tResult.getText().trim().equals("")){}//如果初時顯示為空則不做任何操作
else {
tResult.setText((str.append(s)).toString());
}
}
}
else if(s.equals("0"))//如果選擇的是"0"這個數字鍵
{
if(tResult.getText().trim().equals("0.")){}
else{
tResult.setText(str.append(s).toString());
op2=Double.parseDouble(tResult.getText().trim());
}
}
else{
tResult.setText(str.append(s).toString());
op2=Double.parseDouble(tResult.getText().trim());
if(currentOp==0)
op1=op2;
}
}//end actionPerformed
public static void main(String[] args) {
new Calculator();
}
}
這個就是了,看看功能是否一樣
㈤ 什麼是java源代碼 怎麼查看
不知道你說的是瀏覽器的還是什麼的,
如果是瀏覽器的那麼簡單找到工具-查看源代碼,你就能看見代碼了,
還有一個就是被編譯成class文件的java用反編譯工具可以看到源代碼,
如果以上都不是你想要的答案,那麼你所說的代碼就是程序員寫好的代碼文件
㈥ 求用JAVA編寫俄羅斯方塊游戲的源代碼
俄羅斯方塊——java源代碼提供 import java.awt.*; import java.awt.event.*; //俄羅斯方塊類 public class ERS_Block extends Frame{ public static boolean isPlay=false; public static int level=1,score=0; public static TextField scoreField,levelField; public static MyTimer timer; GameCanvas gameScr; public static void main(String[] argus){ ERS_Block ers = new ERS_Block("俄羅斯方塊游戲 V1.0 Author:Vincent"); WindowListener win_listener = new WinListener(); ers.addWindowListener(win_listener); } //俄羅斯方塊類的構造方法 ERS_Block(String title){ super(title); setSize(600,480); setLayout(new GridLayout(1,2)); gameScr = new GameCanvas(); gameScr.addKeyListener(gameScr); timer = new MyTimer(gameScr); timer.setDaemon(true); timer.start(); timer.suspend(); add(gameScr); Panel rightScr = new Panel(); rightScr.setLayout(new GridLayout(2,1,0,30)); rightScr.setSize(120,500); add(rightScr); //右邊信息窗體的布局 MyPanel infoScr = new MyPanel(); infoScr.setLayout(new GridLayout(4,1,0,5)); infoScr.setSize(120,300); rightScr.add(infoScr); //定義標簽和初始值 Label scorep = new Label("分數:",Label.LEFT); Label levelp = new Label("級數:",Label.LEFT); scoreField = new TextField(8); levelField = new TextField(8); scoreField.setEditable(false); levelField.setEditable(false); infoScr.add(scorep); infoScr.add(scoreField); infoScr.add(levelp); infoScr.add(levelField); scorep.setSize(new Dimension(20,60)); scoreField.setSize(new Dimension(20,60)); levelp.setSize(new Dimension(20,60)); levelField.setSize(new Dimension(20,60)); scoreField.setText("0"); levelField.setText("1"); //右邊控制按鈕窗體的布局 MyPanel controlScr = new MyPanel(); controlScr.setLayout(new GridLayout(5,1,0,5)); rightScr.add(controlScr); //定義按鈕play Button play_b = new Button("開始游戲"); play_b.setSize(new Dimension(50,200)); play_b.addActionListener(new Command(Command.button_play,gameScr)); //定義按鈕Level UP Button level_up_b = new Button("提高級數"); level_up_b.setSize(new Dimension(50,200)); level_up_b.addActionListener(new Command(Command.button_levelup,gameScr)); //定義按鈕Level Down Button level_down_b =new Button("降低級數"); level_down_b.setSize(new Dimension(50,200)); level_down_b.addActionListener(new Command(Command.button_leveldown,gameScr)); //定義按鈕Level Pause Button pause_b =new Button("游戲暫停"); pause_b.setSize(new Dimension(50,200)); pause_b.addActionListener(new Command(Command.button_pause,gameScr)); //定義按鈕Quit Button quit_b = new Button("退出遊戲"); quit_b.setSize(new Dimension(50,200)); quit_b.addActionListener(new Command(Command.button_quit,gameScr)); controlScr.add(play_b); controlScr.add(level_up_b); controlScr.add(level_down_b); controlScr.add(pause_b); controlScr.add(quit_b); setVisible(true); gameScr.requestFocus(); } } //重寫MyPanel類,使Panel的四周留空間 class MyPanel extends Panel{ public Insets getInsets(){ return new Insets(30,50,30,50); } } //游戲畫布類 class GameCanvas extends Canvas implements KeyListener{ final int unitSize = 30; //小方塊邊長 int rowNum; //正方格的行數 int columnNum; //正方格的列數 int maxAllowRowNum; //允許有多少行未削 int blockInitRow; //新出現塊的起始行坐標 int blockInitCol; //新出現塊的起始列坐標 int [][] scrArr; //屏幕數組 Block b; //對方快的引用 //畫布類的構造方法 GameCanvas(){ rowNum = 15; columnNum = 10; maxAllowRowNum = rowNum - 2; b = new Block(this); blockInitRow = rowNum - 1; blockInitCol = columnNum/2 - 2; scrArr = new int [32][32]; } //初始化屏幕,並將屏幕數組清零的方法 void initScr(){ for(int i=0;i<rowNum;i++) for (int j=0; j<columnNum;j++) scrArr[j]=0; b.reset(); repaint(); } //重新刷新畫布方法 public void paint(Graphics g){ for(int i = 0; i < rowNum; i++) for(int j = 0; j < columnNum; j++) drawUnit(i,j,scrArr[j]); } //畫方塊的方法 public void drawUnit(int row,int col,int type){ scrArr[row][col] = type; Graphics g = getGraphics(); tch(type){ //表示畫方快的方法 case 0: g.setColor(Color.black);break; //以背景為顏色畫 case 1: g.setColor(Color.blue);break; //畫正在下落的方塊 case 2: g.setColor(Color.magenta);break; //畫已經落下的方法 } g.fill3DRect(col*unitSize,getSize().height-(row+1)*unitSize,unitSize,unitSize,true); g.dispose(); } public Block getBlock(){ return b; //返回block實例的引用 } //返回屏幕數組中(row,col)位置的屬性值 public int getScrArrXY(int row,int col){ if (row < 0 || row >= rowNum || col < 0 || col >= columnNum) return(-1); else return(scrArr[row][col]); } //返回新塊的初始行坐標方法 public int getInitRow(){ return(blockInitRow); //返回新塊的初始行坐標 } //返回新塊的初始列坐標方法 public int getInitCol(){ return(blockInitCol); //返回新塊的初始列坐標 } //滿行刪除方法 void deleteFullLine(){ int full_line_num = 0; int k = 0; for (int i=0;i<rowNum;i++){ boolean isfull = true; L1:for(int j=0;j<columnNum;j++) if(scrArr[j] == 0){ k++; isfull = false; break L1; } if(isfull) full_line_num++; if(k!=0 && k-1!=i && !isfull) for(int j = 0; j < columnNum; j++){ if (scrArr[j] == 0) drawUnit(k-1,j,0); else drawUnit(k-1,j,2); scrArr[k-1][j] = scrArr[j]; } } for(int i = k-1 ;i < rowNum; i++){ for(int j = 0; j < columnNum; j++){ drawUnit(i,j,0); scrArr[j]=0; } } ERS_Block.score += full_line_num; ERS_Block.scoreField.setText(""+ERS_Block.score); } //判斷游戲是否結束方法 boolean isGameEnd(){ for (int col = 0 ; col <columnNum; col ++){ if(scrArr[maxAllowRowNum][col] !=0) return true; } return false; } public void keyTyped(KeyEvent e){ } public void keyReleased(KeyEvent e){ } //處理鍵盤輸入的方法 public void keyPressed(KeyEvent e){ if(!ERS_Block.isPlay) return; tch(e.getKeyCode()){ case KeyEvent.VK_DOWN:b.fallDown();break; case KeyEvent.VK_LEFT:b.leftMove();break; case KeyEvent.VK_RIGHT:b.rightMove();break; case KeyEvent.VK_SPACE:b.leftTurn();break; } } } //處理控制類 class Command implements ActionListener{ static final int button_play = 1; //給按鈕分配編號 static final int button_levelup = 2; static final int button_leveldown = 3; static final int button_quit = 4; static final int button_pause = 5; static boolean pause_resume = true; int curButton; //當前按鈕 GameCanvas scr; //控制按鈕類的構造方法 Command(int button,GameCanvas scr){ curButton = button; this.scr=scr; } //按鈕執行方法 public void actionPerformed (ActionEvent e){ tch(curButton){ case button_play:if(!ERS_Block.isPlay){ scr.initScr(); ERS_Block.isPlay = true; ERS_Block.score = 0; ERS_Block.scoreField.setText("0"); ERS_Block.timer.resume(); } scr.requestFocus(); break; case button_levelup:if(ERS_Block.level < 10){ ERS_Block.level++; ERS_Block.levelField.setText(""+ERS_Block.level); ERS_Block.score = 0; ERS_Block.scoreField.setText(""+ERS_Block.score); } scr.requestFocus(); break; case button_leveldown:if(ERS_Block.level > 1){ ERS_Block.level--; ERS_Block.levelField.setText(""+ERS_Block.level); ERS_Block.score = 0; ERS_Block.scoreField.setText(""+ERS_Block.score); } scr.requestFocus(); break; case button_pause:if(pause_resume){ ERS_Block.timer.suspend(); pause_resume = false; }else{ ERS_Block.timer.resume(); pause_resume = true; } scr.requestFocus(); break; case button_quit:System.exit(0); } } } //方塊類 class Block { static int[][] pattern = { {0x0f00,0x4444,0x0f00,0x4444},//用十六進至表示,本行表示長條四種狀態 {0x04e0,0x0464,0x00e4,0x04c4}, {0x4620,0x6c00,0x4620,0x6c00}, {0x2640,0xc600,0x2640,0xc600}, {0x6220,0x1700,0x2230,0x0740}, {0x6440,0x0e20,0x44c0,0x8e00}, {0x0660,0x0660,0x0660,0x0660} }; int blockType; //塊的模式號(0-6) int turnState; //塊的翻轉狀態(0-3) int blockState; //快的下落狀態 int row,col; //塊在畫布上的坐標 GameCanvas scr; //塊類的構造方法 Block(GameCanvas scr){ this.scr = scr; blockType = (int)(Math.random() * 1000)%7; turnState = (int)(Math.random() * 1000)%4; blockState = 1; row = scr.getInitRow(); col = scr.getInitCol(); } //重新初始化塊,並顯示新塊 public void reset(){ blockType = (int)(Math.random() * 1000)%7; turnState = (int)(Math.random() * 1000)%4; blockState = 1; row = scr.getInitRow(); col = scr.getInitCol(); dispBlock(1); } //實現「塊」翻轉的方法 public void leftTurn(){ if(assertValid(blockType,(turnState + 1)%4,row,col)){ dispBlock(0); turnState = (turnState + 1)%4; dispBlock(1); } } //實現「塊」的左移的方法 public void leftMove(){ if(assertValid(blockType,turnState,row,col-1)){ dispBlock(0); col--; dispBlock(1); } } //實現塊的右移 public void rightMove(){ if(assertValid(blockType,turnState,row,col+1)){ dispBlock(0); col++; dispBlock(1); } } //實現塊落下的操作的方法 public boolean fallDown(){ if(blockState == 2) return(false); if(assertValid(blockType,turnState,row-1,col)){ dispBlock(0); row--; dispBlock(1); return(true); }else{ blockState = 2; dispBlock(2); return(false); } } //判斷是否正確的方法 boolean assertValid(int t,int s,int row,int col){ int k = 0x8000; for(int i = 0; i < 4; i++){ for(int j = 0; j < 4; j++){ if((int)(pattern[t][s]&k) != 0){ int temp = scr.getScrArrXY(row-i,col+j); if (temp<0||temp==2) return false; } k = k >> 1; } } return true; } //同步顯示的方法 public synchronized void dispBlock(int s){ int k = 0x8000; for (int i = 0; i < 4; i++){ for(int j = 0; j < 4; j++){ if(((int)pattern[blockType][turnState]&k) != 0){ scr.drawUnit(row-i,col+j,s); } k=k>>1; } } } } //定時線程 class MyTimer extends Thread{ GameCanvas scr; public MyTimer(GameCanvas scr){ this.scr = scr; } public void run(){ while(true){ try{ sleep((10-ERS_Block.level + 1)*100); } catch(InterruptedException e){} if(!scr.getBlock().fallDown()){ scr.deleteFullLine(); if(scr.isGameEnd()){ ERS_Block.isPlay = false; suspend(); }else scr.getBlock().reset(); } } } } class WinListener extends WindowAdapter{ public void windowClosing (WindowEvent l){ System.exit(0); } } 22
㈦ 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屬性,