Compile several self-written small codes (JSP calendar and jframe calculator)

Source: Internet
Author: User

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. Get (CLD. Year) + "year" + (CLD. Get (CLD. month) + 1) + "month ");
// Out. println (CLD. gettime ());
// Convert the calendar to the first day of the current month.
ClD. Add (CLD. Date, 1-cld.get (CLD. Date ));
// Obtain the number of weeks of the day
Int weekoffirstday = ClD. Get (CLD. day_of_week );
%>

<Table border = 1>
& Lt; tr bgcolor = "227799" & gt;
<TD> day </TD>
<TD> 1 </TD>
<TD> 2 </TD>
<TD> 3 </TD>
<TD> 4 </TD>
<TD> 5 </TD>
<TD> 6 </TD>
</Tr>
<Tr>
<%
// The number of blank cells in front of the calendar is exactly the number of weeks on the first day of the month
For (INT I = 0; I <weekOfFirstDay-1; I ++ ){
Out. println ("<TD> </TD> ");
}

// The cycle length is the maximum number of days in the month
For (INT I = 0; I <ClD. getactualmaximum (CLD. Date); I ++ ){

// Add "<tr>" before Sunday"
If (weekoffirstday + i-1) % 7) = 0 ){
Out. println ("<tr> ");
}
// I + 1 is the number of days
Out. println ("<TD>" + (I + 1) + "</TD> ");

// Add "</tr>" to the end on Saturday. You can skip this step based on the HTML language. </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 (); // The English case-insensitive conversion number is invalid.
}
%>
</SELECT>
<Input type = "Submit" value = "Submit">
</Form>
</Div>

Full array 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 + C ");
Jmenuitem mi12 = new jmenuitem ("paste (p) Ctrl + V ");
Jcheckboxmenuitem mi21 = new jcheckboxmenuitem ("standard type (t )");
Jcheckboxmenuitem mi22 = new jcheckboxmenuitem ("science type (s )");
Jcheckboxmenuitem mi23 = new jcheckboxmenuitem ("digital grouping (I )");
Jmenuitem mi31 = new jmenuitem ("help topic (h )");
Jmenuitem mi32 = new jmenuitem ("about calculator ()");

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 ));
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 internal 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 actionreceivmed (actionevent ){
TF. settext (numappend. append (A. getactioncommand (). tostring ());
}
};

Class operatorlistenter implements actionlistener {
Public void actionreceivmed (actionevent ){

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

// Execute +-*/Operation
If (operator = "+ "){
R + = N;
} Else {
If (operator = "-"){
R-= N;
} Else {
If (operator = "*"){
R * = N;
} Else {
If (operator = "/"){
R = r/N;
}
}
}
} // +-*/Operation

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 (incluwevent 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.