重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
/*计算器*/
创新互联建站于2013年开始,先为曲麻莱等服务建站,曲麻莱等地企业,进行企业商务咨询服务。为曲麻莱企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
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]; /*数字按钮*/
JButton b2[]=new JButton[6]; /*操作按钮*/
boolean isNumber;/*判断是否输入多位数字的变量*/
double number;/*存储输入数值、显示结果的变量*/
double result;/*存储中间运算结果的变量*/
char operator;/*存储当前操作符的成员变量*/
public Calculator(){
frame=new JFrame("计算器");
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);/*设置文本域中文本的对齐方式*/
isNumber=true;/*初始值设置*/
number=0;/*初始值设置*/
result=0;/*初始值设置*/
operator=' ';/*初始值设置*/
for(int i=0;ib1.length;i++){
b1[i]=new JButton(Integer.toString(i));/*创建数字按钮*/
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]);/*创建操作按钮*/
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);/*设置框架窗口的默认窗口关闭操作*/
frame.setVisible(true);/*设置框架可见*/
}
public double getDisplay(){/*返回要显示的结果*/
return number;
}
public void reDisplay(){/*刷新文本域的内容*/
tfShow.setText(""+getDisplay());
}
/*对输入数字的处理*/
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);/*对多位数字的处理*/
}else{
this.number=num;
}
isNumber=true;/*输入连续数字(即多位数字)时为真*/
}
public void operationProcess(char operator){/*根据输入的操作符改变当前操作符*/
switch(operator){
case '-':
this.operator='-';
break;
case '+':
this.operator='+';
break;
case '*':
this.operator='*';
break;
case '/':
this.operator='/';
break;
}
result=number;
isNumber=false;/*输入操作符时表示输入连续数字的标记变量为假*/
}
public void clear(){
number=0;
result=0;
}
public void equal(){/*计算运算结果*/
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; /*把运算结果赋值给显示变量*/
isNumber=false;
operator=' ';
}
public static void main(String args[]){
Calculator cal=new Calculator();/*创建计算器*/
}
public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();/*获取按钮激发的操作事件的命令名称*/
char c=command.charAt(0);/*将按钮命令名称的第一个字符赋值给一个字符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);/*输入数字的处理*/
break;
case '+':
case '-':
case '*':
case '/':
operationProcess(c);/*算数运算符的处理*/
break;
case '=':equal();break;/*计算运算结果*/
case 'C':clear();break;/*清零*/
}
reDisplay(); /*在文本域中显示信息*/
}
}
运行截图:
代码如下:
public class HelloWorld {
public static void main(String []args) {
int a = 3, b = 7 ;
System.out.println("Hello World!");
}
public static int f(int a, int b){
return a*a + a*b + b*b;
}
}
结果如下:
具体代码如下:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Calculator extends JFrame implements ActionListener {
private JFrame jf;
private JButton[] allButtons;
private JButton clearButton;
private JTextField jtf;
public Calculator() {
//对图形组件实例化
jf=new JFrame("任静的计算器1.0:JAVA版");
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(){
System.exit(0);
}
});
allButtons=new JButton[16];
clearButton=new JButton("清除");
jtf=new JTextField(25);
jtf.setEditable(false);
String str="123+456-789*0.=/";
for(int i=0;iallButtons.length;i++){
allButtons[i]=new JButton(str.substring(i,i+1));
}
}
public void init(){
//完成布局
jf.setLayout(new BorderLayout());
JPanel northPanel=new JPanel();
JPanel centerPanel=new JPanel();
JPanel southPanel=new JPanel();
northPanel.setLayout(new FlowLayout());
centerPanel.setLayout(new GridLayout(4,4));
southPanel.setLayout(new FlowLayout());
northPanel.add(jtf);
for(int i=0;i16;i++){
centerPanel.add(allButtons[i]);
}
southPanel.add(clearButton);
jf.add(northPanel,BorderLayout.NORTH);
jf.add(centerPanel,BorderLayout.CENTER);
jf.add(southPanel,BorderLayout.SOUTH);
addEventHandler();
}
//添加事件监听
public void addEventHandler(){
jtf.addActionListener(this);
for(int i=0;iallButtons.length;i++){
allButtons[i].addActionListener(this);
}
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Calculator.this.jtf.setText("");
}
});
}
//事件处理
public void actionPerformed(ActionEvent e) {
//在这里完成事件处理 使计算器可以运行
String action=e.getActionCommand();
if(action=="+"||action=="-"||action=="*"||action=="/"){
}
}
public void setFontAndColor(){
Font f=new Font("宋体",Font.BOLD,24);
jtf.setFont(f);
jtf.setBackground(new Color(0x8f,0xa0,0xfb));
for(int i=0;i16;i++){
allButtons[i].setFont(f);
allButtons[i].setForeground(Color.RED);
}
}
public void showMe(){
init();
setFontAndColor();
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new Calculator().showMe();
}
}
这是我晓得的最简单的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);
//创建驱动
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();
//重定向至查询页面
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() {
}
}