拈幾個自己寫的小代碼(jsp的日曆和jFrame的計算機)__js

來源:互聯網
上載者:User

日曆:

<%@page contentType="text/html;charset=GBK"%>
<%@page import="java.util.Calendar"%>
<%//@page errorPage="handleErrorPage.jsp"%>

<div align=center style="position: absolute; top: 12%; width=100%">
<% 
 boolean requested = false;
 Calendar cld = Calendar.getInstance();
if(request.getParameter("year")!=null){
cld.set(cld.YEAR,Integer.parseInt(request.getParameter("year")));
requested= true;
//out.println("year: "+cld.get(cld.YEAR));
}
if(request.getParameter("month")!=null){
cld.set(cld.MONTH,Integer.parseInt(request.getParameter("month"))-1);
requested= true;
//out.println("month: "+(cld.get(cld.MONTH)+1));
}

out.println(cld.get(cld.YEAR)+"年"+(cld.get(cld.MONTH)+1)+"月");
//out.println(cld.getTime());
//將日曆轉到當月第一天
 cld.add(cld.DATE,1-cld.get(cld.DATE));
//擷取這一天的星期數
 int weekOfFirstDay = cld.get(cld.DAY_OF_WEEK);
%>

<table border=1>
<tr bgColor="227799">
<td>日</td>
<td>一</td>
<td>二</td>
<td>三</td>
<td>四</td>
<td>五</td>
<td>六</td>
</tr>
<tr>
<%
//日曆的前空白欄框數 剛好是當月第一天的星期數
for(int i=0;i<weekOfFirstDay-1;i++){
out.println("<td></td>");
}

//迴圈長度是當月的最大天數
for(int i=0;i<cld.getActualMaximum(cld.DATE);i++){

//周日就前面加"<tr>"
if(((weekOfFirstDay+i-1) % 7)==0){
 out.println("<tr>");
}
//i+1是日期數
out.println("<td>"+(i+1)+"</td>");

//周六就後面加"</tr>" 基於HTML語言可以省去</tr> 所以也可去掉
//if(((weekOfFirstDay+i)% 7)==0){
// out.println("</tr>");
//}
}
%>
</table>
<form>
<select name ="year">
<%
for(int i=0; i<80;i++){
out.println("<option");
if(requested&&(1949+i)==Integer.parseInt(request.getParameter("year"))){
out.println(" selected");
}
out.println(">"+(1949+i));
}
%>
</select>
<select name ="month">
<%
for(int i=0; i<12;i++){
out.println("<option");
if(requested&&i+1==Integer.parseInt(request.getParameter("month"))){
out.println(" selected");
}
out.println(">"+(new Integer(i+1)).toString().toUpperCase());//英文大小寫轉換 數字無效
}
%>
</select>
<input type="submit" value="提交">
</form>
</div> 

數組全排列

public class Test{
 public static void main(String[] args){
  int a[]={2,5,6,7};
  for(int i=0;i<4;i++){
   for(int j=0;j<4;j++){
    if(j==i)continue;
    for(int k=0;k<4;k++){
     if(k==i||k==j)continue;
     System.out.print(a[i]*1000+a[j]*100+a[k]*10+a[6-i-j-k]);
     System.out.print(" ");
    }
   }
   System.out.println();
  }
 }
}

 

計算機

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyCalc
{
 String result=new String();
 StringBuffer numAppend = new StringBuffer();
 String operator=new String();

 JFrame f = null;
 JMenuBar mb = null;

 JMenu m1 = new JMenu("編輯(E)");
 JMenu m2 = new JMenu("查看(V)");
 JMenu m3 = new JMenu("協助(H)");

 JMenuItem mi11=new JMenuItem("複製(C) Ctrl+C");
 JMenuItem mi12=new JMenuItem("粘貼(P) Ctrl+V");
 JCheckBoxMenuItem mi21=new JCheckBoxMenuItem("標準型(T)");
 JCheckBoxMenuItem mi22=new JCheckBoxMenuItem("科學型(S)");
 JCheckBoxMenuItem mi23=new JCheckBoxMenuItem("數字分組(I)");
 JMenuItem mi31=new JMenuItem("說明主題(H)");
 JMenuItem mi32=new JMenuItem("關於計算機(A)");

 JTextField tf =new JTextField(32);

 public MyCalc(String s,int x,int y,int w,int h){

  f = new JFrame(s);
  mb = new JMenuBar();
  f.setJMenuBar(mb);

  mb.add(m1);
  mb.add(m2);
  mb.add(m3);

  m1.add(mi11);
  m1.add(mi12);
  m2.add(mi21);
  m2.add(mi22);
  m2.addSeparator();
  m2.add(mi23);
  m3.add(mi31);
  m3.addSeparator();
  m3.add(mi32);
  
  JPanel pp = new JPanel();
  pp.setLayout(new BorderLayout(5,5));

  JPanel pForTextF = new JPanel(new FlowLayout(FlowLayout.LEFT));
  pForTextF.add(tf);
  f.add(pp);

  JPanel p =new JPanel();

  JPanel p1 = new JPanel();
  JPanel p2 = new JPanel();
  JPanel p3 = new JPanel();
  JPanel p4 = new JPanel();

//  Rectangle rect = p.getBounds();
//  int width = ((Double)rect.getWidth()).intValue();
//  int height = ((Double)rect.getHeight()).intValue();
//  System.out.println(width+""+height);
  
  int width=w*11/12;
  int height=(h-12)*4/6;
  p1.setBounds(0,0,width/7,height/6);
  p2.setBounds(width/7+10,0,width*6/7-8,height/6);
  p3.setBounds(0,height/6+10,width/7,height*5/6-8);
  p4.setBounds(width/7+10,height/6+10,width*6/7-8,height*5/6-8);

//     p1.setBackground(Color.BLUE);
//     p2.setBackground(Color.GREEN);
 //    p3.setBackground(Color.YELLOW);
 //    p4.setBackground(Color.MAGENTA);
 
  JTextField tf1 = new JTextField(3);
  tf1.setEditable(false);
  p1.add(tf1);
  
  JButton b21 = new JButton("Backspace");
  JButton b22 = new JButton("CE");
  JButton b23 = new JButton("C");
  b21.setForeground(Color.RED);
   b22.setForeground(Color.RED);
  b23.setForeground(Color.RED);

  p2.setLayout(new GridLayout(1,3,3,3));
  p2.add(b21);
  p2.add(b22);
  p2.add(b23);

  JButton b31 = new JButton("MC");
  JButton b32 = new JButton("MR");
  JButton b33 = new JButton("MS");
  JButton b34 = new JButton("M+");
  b31.setForeground(Color.RED);
  b32.setForeground(Color.RED);
  b33.setForeground(Color.RED);
  b34.setForeground(Color.RED);

  b31.setSize(5,5);
  p3.setLayout(new GridLayout(4,1,3,3));
  p3.add(b31);
  p3.add(b32);
  p3.add(b33);
  p3.add(b34);

 

  String[] sArr =
   {"7","8","9","/","sqrt","4","5","6","*","%"
           ,"1","2","3","-","1/x","0","+/-",".","+","="};
  JButton[] buttons = new JButton[sArr.length];
  p4.setLayout(new GridLayout(4,5,3,3));
  for(int i=0;i<sArr.length;i++){
   buttons[i] = new JButton(sArr[i]);

   if (sArr[i]=="+"||sArr[i]=="-"||sArr[i]=="*"||sArr[i]=="/"||sArr[i]=="=")
   {
    buttons[i].addActionListener(new OperatorListenter());
    buttons[i].setForeground(Color.RED);
   }else{
     buttons[i].setForeground(Color.BLUE);
   }

//   if (sArr[i].compareTo("9")>0||sArr[i].compareTo("0")<0)
//   {
//    buttons[i].setForeground(Color.RED);
//   }

//   if(i%5==3){
//    buttons[i].setForeground(Color.RED);
//   }
   p4.add(buttons[i]);
  }

  p.setLayout(null);
  p.add(p1);
  p.add(p2);
  p.add(p3);
  p.add(p4);

  pp.add(p,BorderLayout.CENTER);
  f.setLayout(null);
  pp.setBounds(10,3,w-8,h-8);
  f.add(pp);

  pp.add(pForTextF,BorderLayout.NORTH);
  f.setBounds(x,y,w,h);
  f.setBackground(new Color(212,208,200));
  //f.setResizable(false);
  f.setVisible(true);
//  f.addWindowListener(new WindowClosing());
  f.addWindowListener(new WindowAdapter(){//匿名內部類
   public void windowClosing(WindowEvent e){
    System.exit(0);
   }
  });


  //加Listener
  for (int i=0;i<sArr.length;i++ )
  {
   if((buttons[i].getText().compareTo("0")>=0)&&(buttons[i].getText().compareTo("9")<=0)&&(buttons[i].getText()!="1/x")){
    buttons[i].addActionListener(new NumButtonListener());
   };
  }
 }


 public static void main(String[] args){

  new MyCalc("MyCalc",0,0,400,280);
 
 }


 class NumButtonListener implements ActionListener
 {
  public void actionPerformed(ActionEvent a){
   tf.setText(numAppend.append(a.getActionCommand()).toString());
  }
 };


 class OperatorListenter implements ActionListener{
  public void actionPerformed(ActionEvent a){

//  if(numAppend.toString()!=null){
  if((operator.length()>0)&&(numAppend.length()>0)&&(result.length()>0)){
   double n =  new Double(numAppend.toString()).doubleValue();
   double r = new Double(result).doubleValue();
   
   //執行+-*/運算
   if(operator=="+"){
    r+=n;
   }else{
    if(operator=="-"){
     r-=n;
    }else{
     if(operator=="*"){
      r*=n;
     }else{
      if(operator=="/"){
       r=r/n;
      }
     }
    }
   }//+-*/運算

   result = new Double(r).toString();
   tf.setText(result);
  }else{
   if(numAppend.length()>0){
    result = numAppend.toString();
    }
  }

  if(operator=="="){
   tf.setText(result);
   operator=null;   
  }

  operator = a.getActionCommand();
  numAppend=new StringBuffer();
  }
 }

}
/*

class WindowClosing extends WindowAdapter
{
 public void windowClosing(WindowEvent we){
 System.exit(0);
 }
};
*/

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.