Write a few of their own small code (JSP calendar and JFrame Calculator) __js

Source: Internet
Author: User
Tags gettext stringbuffer

Calendar:

<% @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, Cld.get. Year) + "years" + (Cld.get (CLD). MONTH) (+1) + "month");
Out.println (Cld.gettime ());
Transfer calendar to the first day of the month
Cld.add (CLD). Date,1-cld.get (CLD). DATE));
Get the number of weeks of the day
int weekoffirstday = Cld.get (CLD. Day_of_week);
%>

<table border=1>
<tr bgcolor= "227799" >
<td> Day </td>
<td> a </td>
<td> two </td>
<td> three </td>
<td> Four </td>
<td> Five </td>
<td> Six </td>
</tr>
<tr>
<%
The number of previous blank spaces in the calendar is just the number of weeks on the first day of the month
for (int i=0;i<weekoffirstday-1;i++) {
Out.println ("<td></td>");
}

Cycle length is the maximum number of days of the month
for (an int i=0;i<cld.getactualmaximum (CLD). DATE); i++) {

Sunday on the front with "<tr>"
if (((weekoffirstday+i-1)% 7) ==0) {
Out.println ("<tr>");
}
I+1 is the number of dates
Out.println ("<td>" + (i+1) + "</td>");

Saturday on the back with "</tr>" based on HTML language can save </tr> so also can be removed
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 ()); The case conversion number is invalid
}
%>
</select>
<input type= "Submit" value= "submitted" >
</form>
</div>

Array full arrangement

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 ();
}
}
}

Calculator

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 ("Edit (E)");
JMenu m2 = new JMenu ("View (V)");
JMenu m3 = new JMenu ("Help (H)");

JMenuItem mi11=new JMenuItem ("Copy (C) Ctrl + +");
JMenuItem mi12=new JMenuItem ("Paste (P) Ctrl + V");
JCheckBoxMenuItem mi21=new JCheckBoxMenuItem ("standard type (T)");
JCheckBoxMenuItem mi22=new JCheckBoxMenuItem ("Scientific type (S)");
JCheckBoxMenuItem mi23=new JCheckBoxMenuItem ("Digital Grouping (I)");
JMenuItem mi31=new JMenuItem ("Help topic (H)");
JMenuItem mi32=new JMenuItem ("About Calculator (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 () {//anonymous inner class
public void windowclosing (WindowEvent e) {
System.exit (0);
}
});


Add 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 ();
   
   //perform +-*/operations
   if (operator== "+") {
     r+=n;
   }else{
    if (operator== "-") {
     r- =n;
    }else{
     if (operator== "*") {
       r*=n;
     }else{
      if (operator== "/") {
        r=r/n;
      }
     }
    }
   }//+-*/Operations

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);
}
};
*/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.