import?java.applet.Applet;
創(chuàng)新互聯(lián)于2013年開始,先為官渡等服務(wù)建站,官渡等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為官渡企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
import?java.awt.Button;
import?java.awt.Color;
import?java.awt.Graphics;
import?java.awt.TextField;
import?java.awt.event.ActionEvent;
public?class?Nicki?extends?Applet{
private?static?final?long?serialVersionUID?=?1L;
private?Button?ok;
private?int?num=32;
private?int?resu=0;
private?boolean?isRig=false;
private?TextField?iPut;
public?Nicki(){
this.setLayout(null);
ok=new?Button("OK");
ok.setActionCommand(getName());
ok.setBounds(150,?150,?40,?20);
iPut=new?TextField();
this.add(iPut);
iPut.setBounds(100,?150,?40,?20);
this.add(ok);
ok.addActionListener(new?ButtonAction(this));
}
public?void?paint(Graphics?g){
g.setColor(Color.white);
g.fillRect(0,?0,?this.getWidth(),?this.getHeight());
g.setColor(Color.BLACK);
g.drawString("Please?guess?a?number?",?10,?20);
g.drawString("between?1?and?100",?10,?40);
if(isRig==falseresu!=0){
if(resunum){
g.drawString(""+resu+"?is?too?big?!",?10,?100);
}else?if(resunum){
g.drawString(resu+"is?too?small?!",?10,?100);
}
}else?if(isRig==true){
g.setColor(Color.GREEN);
g.drawString("Yes,"+resu+"?is?the?right?number",?10,?80);
g.drawString("Your?are?great!?",?10,?100);
g.setColor(Color.red);
g.drawString(resu+"!",?70,?120);
}
iPut.setText("");
g.drawString("Input?the?number:",?0,?150);
}
public?void?ButtonActionPerformed(ActionEvent?e){
if(e.getActionCommand().equals("panel0")){
resu=Integer.parseInt(iPut.getText());
if(num==resu){
isRig=true;
}else{
isRig=false;
}
repaint();
}
}
}
class?ButtonAction?implements?java.awt.event.ActionListener{
Nicki?su;
public?ButtonAction(Nicki?bun){
this.su=bun;
}
public?void?actionPerformed(ActionEvent?e)?{
su.ButtonActionPerformed(e);
}
}
這是我曉得的最簡(jiǎn)單的java小程序代碼了你可以看看:
package com.kenki.emp;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.SQLException;
import java.sql.*;
public class emp extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String code = request.getParameter("code");
String name = request.getParameter("name");
String pay = request.getParameter("pay");
System.out.println("empcode:" + code);
System.out.println("name:" + name);
System.out.println("pay:" + pay);
//創(chuàng)建驅(qū)動(dòng)
new com.microsoft.jdbc.sqlserver.SQLServerDriver();
String strd =
"jdbc:microsoft:sqlserver://localhost:1433;databasename=emp_dates";
String username = "sa";
String pws = "";
try {
java.sql.Connection conn = java.sql.DriverManager.getConnection(
strd, username, pws);
String strs = "insert into emp values(?,?,?)";
java.sql.PreparedStatement pre = conn.prepareStatement(strs);
pre.setString(1, code);
pre.setString(2, name);
pre.setString(3, pay);
pre.execute();
pre.close();
conn.close();
//重定向至查詢頁(yè)面
out.println("成功保存??!");
response.sendRedirect("emp.html");
} catch (SQLException ss) {
ss.printStackTrace();
response.sendRedirect("/WebModule1/error.html");
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
/*計(jì)算器*/
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
public class Calculator implements ActionListener{
JFrame frame;
JPanel panel;
JTextField tfShow;/*定義顯示文本框*/
JButton b1[]=new JButton[10]; /*數(shù)字按鈕*/
JButton b2[]=new JButton[6]; /*操作按鈕*/
boolean isNumber;/*判斷是否輸入多位數(shù)字的變量*/
double number;/*存儲(chǔ)輸入數(shù)值、顯示結(jié)果的變量*/
double result;/*存儲(chǔ)中間運(yùn)算結(jié)果的變量*/
char operator;/*存儲(chǔ)當(dāng)前操作符的成員變量*/
public Calculator(){
frame=new JFrame("計(jì)算器");
frame.setSize(300,300);/*指定框架窗口的大小*/
frame.setResizable(false);/*使框架窗口不可改變大小*/
JPanel contentPane=(JPanel)frame.getContentPane();
contentPane.setBorder(new EmptyBorder(20,20,20,20));/*繪制框架的指定大小的空透明邊框*/
tfShow=new JTextField("0",25);/*指定屬性的文本域*/
tfShow.setHorizontalAlignment(JTextField.RIGHT);/*設(shè)置文本域中文本的對(duì)齊方式*/
isNumber=true;/*初始值設(shè)置*/
number=0;/*初始值設(shè)置*/
result=0;/*初始值設(shè)置*/
operator=' ';/*初始值設(shè)置*/
for(int i=0;ib1.length;i++){
b1[i]=new JButton(Integer.toString(i));/*創(chuàng)建數(shù)字按鈕*/
b1[i].setActionCommand(Integer.toString(i));
b1[i].addActionListener(this);
b1[i].setForeground(Color.blue);
}
String bs[]={"/","*","-","C","+","="};
for(int i=0;ib2.length;i++){
b2[i]=new JButton(bs[i]);/*創(chuàng)建操作按鈕*/
b2[i].setActionCommand(bs[i]);
b2[i].addActionListener(this);
b2[i].setForeground(Color.red);
}
panel=new JPanel();
panel.setLayout(new GridLayout(4,5));
panel.add(b1[1]);
panel.add(b1[2]);
panel.add(b1[3]);
panel.add(b2[0]);
panel.add(b1[4]);
panel.add(b1[5]);
panel.add(b1[6]);
panel.add(b2[1]);
panel.add(b1[7]);
panel.add(b1[8]);
panel.add(b1[9]);
panel.add(b2[2]);
panel.add(b1[0]);
panel.add(b2[3]);
panel.add(b2[4]);
panel.add(b2[5]);
frame.add(tfShow,BorderLayout.NORTH);/*將文本框放置在框架上方*/
frame.add(panel,BorderLayout.CENTER);/*將裝有按鈕組的panel放在框架的中心*/
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/*設(shè)置框架窗口的默認(rèn)窗口關(guān)閉操作*/
frame.setVisible(true);/*設(shè)置框架可見(jiàn)*/
}
public double getDisplay(){/*返回要顯示的結(jié)果*/
return number;
}
public void reDisplay(){/*刷新文本域的內(nèi)容*/
tfShow.setText(""+getDisplay());
}
/*對(duì)輸入數(shù)字的處理*/
public void numberProcess(int num){
if(isNumbernum!=0){
String s1=Integer.toString(num);
String s2=Integer.toString((int)(this.number));
this.number=Double.parseDouble(s2+s1);/*對(duì)多位數(shù)字的處理*/
}else{
this.number=num;
}
isNumber=true;/*輸入連續(xù)數(shù)字(即多位數(shù)字)時(shí)為真*/
}?
public void operationProcess(char operator){/*根據(jù)輸入的操作符改變當(dāng)前操作符*/
switch(operator){
case '-':
this.operator='-';
break;
case '+':
this.operator='+';
break;
case '*':
this.operator='*';
break;
case '/':
this.operator='/';
break;
}
result=number;
isNumber=false;/*輸入操作符時(shí)表示輸入連續(xù)數(shù)字的標(biāo)記變量為假*/
}?
public void clear(){
number=0;
result=0;
}??
public void equal(){/*計(jì)算運(yùn)算結(jié)果*/
switch(operator){
case '-':
result=result-number;
break;
case '+':
result=result+number;
break;
case '*':
result=result*number;
break;
case '/':
result=result/number;
break;
case ' ':
result=number;
break;
}
number=result; /*把運(yùn)算結(jié)果賦值給顯示變量*/
isNumber=false;
operator=' ';?
}?
public static void main(String args[]){
Calculator cal=new Calculator();/*創(chuàng)建計(jì)算器*/
}
public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();/*獲取按鈕激發(fā)的操作事件的命令名稱*/
char c=command.charAt(0);/*將按鈕命令名稱的第一個(gè)字符賦值給一個(gè)字符c*/
switch(c){
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
int number=Integer.parseInt(command);
numberProcess(number);/*輸入數(shù)字的處理*/
break;
case '+':
case '-':
case '*':
case '/':
operationProcess(c);/*算數(shù)運(yùn)算符的處理*/
break;
case '=':equal();break;/*計(jì)算運(yùn)算結(jié)果*/
case 'C':clear();break;/*清零*/
}
reDisplay(); /*在文本域中顯示信息*/
}
}
運(yùn)行截圖:
分享題目:java動(dòng)畫小程序源代碼 java動(dòng)畫小程序源代碼怎么寫
文章起源:http://muchs.cn/article18/dohodgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、網(wǎng)站建設(shè)、微信公眾號(hào)、定制開發(fā)、網(wǎng)站內(nèi)鏈、域名注冊(cè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)