Java to do the interface thinking arrangement

Source: Internet
Author: User
Tags class operator gettext

Speaking of a freshman to learn C + +, but never contact VC + +, as for the interface is not until the beginning of learning Java, so he is a novice ah ...

Step into the subject, through their own two small procedures, the interface to do the idea of finishing.

First, imagine what the interface you want to implement is like. You can draw a profile on the paper (I did it ...). ), as far as possible, such as the click of a button to achieve a page jump, the page after the jump is also drawn. Why would you do that? All know that the interface is made up of controls and containers, the purpose of which is to clear which components to use, and then organize the containers, and then organize the layout, according to your own interface. It can be a bit cumbersome for layouts, depending on whether your window is fixed or variable in size. If the window is a fixed size, make a point, because it is not necessary to consider the effect of changing the window size on the position of the component. If the window size is variable, try to avoid the use of NULL layout, NULL layout I call it absolute layout (web CSS has an absolute layout, of course, and that is not the same, anyway, so called ...) ), the null layout position does not change, the size of the control in the null layout does not change, so the window variable, it is difficult to see ... However, according to the actual situation, some places are also available. There are many kinds of layouts, which are generally nested. Each layout has its own advantages and disadvantages, the grid layout can divide the panel into n*m, the size of the controls in each grid, and as the window changes, the size of the control is changed, but the relative position remains the same, flow layout, according to the alignment, the components can be sorted, the row is full of newline, the window size changes, The position of the control moves as if it were flowing water. Other ways have not yet been specific practice, will not say.

Then, finish the interface, declare the control, and then start editing the interface. For the interface, the first is a frame frame, for frame, you can set its title settitle (); (the title can also be created when the caption is determined in the constructor.) ), and then the window size, which may be a headache for beginners, in the end to set how much? In fact, it is very simple, in the Java core volume a function, you can get the actual size of the PC window, according to this value to adjust the line. (The code is attached below: Then set whether the property is visible, close properties, etc... Here the frame has been built and the container is added below.


Get the PC window size                Toolkit kit = Toolkit.getdefaulttoolkit ();D imension screensize = kit.getscreensize (); int screenwidth = Screensize.width;  int screenheight = Screensize.height;

frame = new JFrame ("Java Small Classroom"); Frame.setbounds (SCREENWIDTH/3, SCREENHEIGHT/4, 450, 300); Set position and size frame.setdefaultcloseoperation (jframe.exit_on_close); Frame.getcontentpane (). SetBackground ( Color.lightgray); Frame.getcontentpane (). setlayout (NULL); Use absolute layout frame.setresizable (false); Setting window size is not variable

For the container is not difficult to understand, if the frame compared to the artboard, then the container is equivalent to the paper, the sheet is on the surface of the board, so there is a hierarchy of points, the point is that you always know which piece of paper is at the top. The paper here is JPanel, similar to frame, set the size, set the layout, set the background color, set the position. Actually creating any component is also a routine, location, size, content, color and so on. If the front do better, then for the layout, is kept add things on the line.

The following is a code for two programs

Import Java.awt.color;import java.awt.frame;import Java.awt.event.actionevent;import Java.awt.event.ActionListener ; Import Java.io.bufferedreader;import Java.io.file;import java.io.filereader;import Java.io.ioexception;import Javax.swing.buttongroup;import Javax.swing.jbutton;import Javax.swing.jcheckbox;import Javax.swing.JFrame;import Javax.swing.jlabel;import Javax.swing.jtextfield;import Javax.swing.swingconstants;public class question Nineth {public static void Main (string[] args) {//TODO auto-generated method Stubmywindow win = new Mywindow ();}} Class Mywindow extends Frame implements ActionListener {String s, str[] = new STRING[7]; File F = new file ("e:\\", "English.txt"); FileReader Inone; BufferedReader Intwo; JButton Btn_ans, Btn_next, btn_new; Jcheckbox checkbox[]; JTextField test, result; JLabel Label, Label2; Buttongroup Sele = new Buttongroup (); int score = 0; JFrame frame; Mywindow () {frame = new JFrame ("English test");//frame.setbackground (Color.magenta);//frame.getcontentpane (). SetBackground (COLOR.LIght_gray); Frame.setbounds (+, +, +), frame.setdefaultcloseoperation (Jframe.exit_on_close); Frame.getcontentpane (). setlayout (null); btn_next = new JButton ("Next question"); Btn_next.setbounds (38, 197, 93, 23); Frame.getcontentpane (). Add (Btn_next); Btn_next.addactionlistener (this); Btn_ans = new JButton ("result"); Btn_     Ans.setbounds (166, 197, Frame.getcontentpane); add (Btn_ans); Btn_ans.addactionlistener (this); Btn_new = new JButton ("Redo Again"), Btn_new.setbounds (294, 197,); Frame.getcontentpane (). Add (btn_new); btn_ New.addactionlistener (this); result = new JTextField (); Result.setbounds (351, N, a); Frame.getcontentpane (). Add ( result); result.setcolumns; result.seteditable (false); result.setfocusable (false); Label = new JLabel ("Score:"); Label.sethorizontalalignment (Swingconstants.center); Label.setbounds (290, si, si, si), Frame.getcontentpane (). Add (Label); Label2 = new JLabel ("dry:"); Label2.setbounds (Ten, Wu, Wu), Frame.getcontentpane (). Add (Label2), test = new JTextField (); test.setbounds (0, 47, 434); Frame.getcontentpane (). Add (test); Test.setcolumns (+); test.seteditable (false); Test.setfocusable (false) checkbox = new Jcheckbox[4];for (int i = 0; I <= 3; i++) {Checkbox[i] = new Jcheckbox ("New check box"); Sele.add (checkb Ox[i]);} Checkbox[0].setbounds (103, Checkbox[1].setbounds, 151, 103, Max); Checkbox[2].setbounds (224, 100, 103, 23 ); Checkbox[3].setbounds (224, 151, 103, 23°c); for (int i = 0; I <= 3; i++) {Frame.getcontentpane (). Add (Checkbox[i]);} Frame.setvisible (true); try {inone = new FileReader (f); intwo = new BufferedReader (inone);} catch (IOException e) {}gettext ();}    public void GetText () {try {s = intwo.readline (); if (!s.contains ("EndEnd") {//string regex = "#";    str = s.split ("#"); for (int i = 0; i < str.length; i++) {////}//char[] ch = new Char[s.length ()];//s.getchars (0, s.length (), ch, 0);//int CNT = 0, pos = 0;//for (int i = 0; i < s.length (); i++) {////if (ch[i] = = ' # ') {//str[cnt++] = s.substring (pos, i);//po s = i + 1;//}//}//str[cnt++] = s.substring (pos, S.length ()), Test.settext (Str[0]), for (int j = 1; J <= 4; j + +) {Checkbox[j-1].settext (str[j]);}} else {Test.settext ("completed learning"); for (int j = 0; J < 4; J + +) {Checkbox[j].settext ("end"); Intwo.close (); Inone.close ();}} catch (IOException e) {test.settext ("file does not exist");}} public void actionperformed (ActionEvent event) {if (event.getsource () = = btn_new) {score = 0;result.settext (s.valueof ( Score)); try {inone = new FileReader (f); intwo = new BufferedReader (inone);} catch (IOException e) {}for (int j = 0; J < 4; J + +) {checkbox[j].setenabled (true);} Btn_ans.setenabled (True); GetText ();} if (event.getsource () = = Btn_next) {//Read the next question GetText (); for (int j = 0; J < 4; J + +) {checkbox[j].setenabled (true);} Btn_ans.setenabled (TRUE);} if (event.getsource () = = Btn_ans) {for (int j = 0; J < 4; J + +) {if (Checkbox[j].gettext (). Equals (str[5]) && che Ckbox[j].isselected ()) {Score++;result.settext (s.valueof (Score));} Checkbox[j].setenabled (false);} Btn_ans.setenabled (FALSE);}}}

A second

public class Login {    public  static void Main (string[] args) {     new MainWindow ();}    }
Import Java.awt.*;import java.awt.event.*;import javax.swing.*; @SuppressWarnings ("Serial") public class MainWindow Extends JFrame {//*********************************************//first step declares the control required by the program//********************************** JFrame frame;//the top panel controls JButton Btn_add, Btn_query_stu, btn_query_cnt; JPanel Panel_head, Panel_add, Panel_query_stu, panel_query_cnt;//the controls under the second panel JLabel Label_query_name; JTextField text_in; JTextArea Text_result; JButton btn_ok;//The controls under the first panel JButton btn_submit; JLabel Label_name, Label_number, Label_mark; JTextField Text_name, Text_number, Text_mark, text_sta;//the controls under the third Panel JLabel label; JTextArea text_cnt;public MainWindow () {//********************************//second step setting the underlying window properties//************************ Get the current screen size Toolkit kit = Toolkit.getdefaulttoolkit ();D imension screensize = kit.getscreensize (); int ScreenWidth = Screensize.width;int ScreenHeight = screensize.height;//frame refers to the underlying window frame = new JFrame ("Java Small classroom"); Frame.setbounds (SCREENWIDTH/3, ScreenheIGHT/4, 450, 300); Set position and size frame.setdefaultcloseoperation (jframe.exit_on_close); Frame.getcontentpane (). SetBackground ( Color.lightgray); Frame.getcontentpane (). setlayout (NULL); Use absolute layout frame.setresizable (false); Setting window size is immutable//upper panel panel_head = new JPanel ();p anel_head.setbounds (2, 0,, max);p Anel_head.setbackground ( Color.lightgray);p anel_head.setvisible (True);p anel_head.setlayout (New GridLayout (1, 3, 2, 0)), Btn_add = new JButton (" Added "); Btn_add.setfont (New Font (" Arial ", 1, 12)); Sets the font format size btn_add.setvisible (TRUE);p Anel_head.add (Btn_add); Btn_add.addactionlistener (new ActionListener () { public void actionperformed (ActionEvent e) {panel_add.setvisible (true);p anel_query_stu.setvisible (false);p Anel_ Query_cnt.setvisible (FALSE);}}); Btn_query_stu = new JButton ("Query student"); Btn_query_stu.setvisible (True);p Anel_head.add (btn_query_stu); btn_query_ Stu.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {panel_add.setvisible (false); Panel_query_stu.setvisible (true);p Anel_queRy_cnt.setvisible (FALSE);}}); btn_query_cnt = new JButton ("Passing Number"); Btn_query_cnt.setvisible (True);p Anel_head.add (btn_query_cnt); btn_query_ Cnt.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {panel_add.setvisible (false); Panel_query_stu.setvisible (False);p anel_query_cnt.setvisible (true); Text_cnt.settext (Operator.query_num ());}}); Frame.getcontentpane (). Add (Panel_head);//lower panel font font = new Font ("Arial", 1, +);p Anel_add = new JPanel ();p anel_ Add.setbounds (2, 4, 225);p Anel_add.setbackground (Color.lightgray);p anel_add.setlayout (New GridLayout (+ 2, 20, Label_number = new JLabel ("Study No.:"); Label_number.setfont (font); Label_number.sethorizontalalignment ( Swingconstants.center);p Anel_add.add (label_number); text_number = new JTextField (); Text_number.setfont (font);p Anel _add.add (text_number); label_name = new JLabel ("Name:"); Label_name.setfont (font); Label_name.sethorizontalalignment ( Swingconstants.center);p Anel_add.add (label_name); text_name = new JTextField (); text_nAme.setfont (font);p Anel_add.add (text_name) Label_mark = new JLabel ("Score:"); Label_mark.setfont (font); label_ Mark.sethorizontalalignment (Swingconstants.center);p anel_add.add (label_mark); Text_mark = new JTextField (); text_ Mark.setfont (font);p anel_add.add (text_mark); btn_submit = new JButton ("commit"); Btn_submit.setfont (font); btn_ Submit.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {String s = text_mark.gettext (); String sta = Operator.adds (Text_number.gettext (), Text_name.gettext (), double.parsedouble (s)), if (Add succeeded. Equals (STA) {text_number.settext (null); Text_name.settext (null); Text_mark.settext (null);} Text_sta.settext (STA);}); Panel_add.add (btn_submit), Text_sta = new JTextField ();p anel_add.add (Text_sta);p anel_add.setvisible (true); Frame.getcontentpane (). Add (Panel_add);//lower panel panel_query_stu = new JPanel ();p anel_query_stu.setbounds (2, 45, 440, 225 );p Anel_query_stu.setbackground (Color.lightgray);p anel_query_stu.setlayout (null); label_query_name = new JLabel (" Enter to queryStudent's name: "), Label_query_name.setbounds (Ten, Ten, ());p Anel_query_stu.add (label_query_name); text_in = new JTextField ( ); Text_in.setbounds (n., max.);p Anel_query_stu.add (text_in); text_result = new JTextArea (); text_ Result.setbounds, 420, Text_result.setfont (font), text_result.seteditable (false);p Anel_query_stu.add ( Text_result); btn_ok = new JButton ("Query"); Btn_ok.setfont (font); Btn_ok.setbounds (+, +, +); btn_ Ok.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {Text_result.settext ( Operator.query_stu (Text_in.gettext ()));}); Panel_query_stu.add (BTN_OK);p anel_query_stu.setvisible (false); Frame.getcontentpane (). Add (Panel_query_stu);// Lower panel panel_query_cnt = new JPanel (); Panel_query_cnt.setbounds (2, 45, 440, 225); Panel_query_cnt.setbackground (Color.lightgray);  Panel_query_cnt.setlayout (NULL); label = new JLabel ("Pass number:"); Label.setbounds (10, 10, 150, 40); Label.setfont (font);  Panel_query_cnt.add (label); text_cnt = new JTextArea (); re>T_cnt.setfont (New Font ("Arial", 1,30)); Text_cnt.setbounds (10, 60, 420, 140); Panel_query_cnt.add (TEXT_CNT); Frame.getcontentpane (). Add (panel_query_cnt); Panel_query_cnt.setvisible (false);//To set the visible frame.setvisible (true) after the component is added; Set window Visible}}

public class Operator {public static string adds (string number, string name, double mark) {Sql_operator ops = new Sql_oper Ator (); String Datasorce = "Javalecture"; String tableName = "Javareport"; Ops.setdatasourcename (Datasorce); Ops.settablename (tableName); Ops.setnumber (number ); Ops.setname (name); Ops.setmark (Mark); return Ops.addrecord ();} public static string Query_stu (string name) {Sql_operator ops = new Sql_operator (); String Datasorce = "Javalecture"; String tableName = "Javareport"; Ops.setdatasourcename (Datasorce); Ops.settablename (tableName); Ops.setname (name); return Ops.query_stu ();} public static String Query_num () {Sql_operator ops = new Sql_operator (); String Datasorce = "Javalecture"; String tableName = "Javareport"; Ops.setdatasourcename (Datasorce); Ops.settablename (tableName); return ops.query_cnt ( );}}

Import Java.util.*;import java.sql.*;p ublic class Sql_operator {String datasourcename = ""; String tableName = ""; String number = "", name = "";d ouble Mark; Arraylist<stringbuffer> queryresult;public Sql_operator () {try {class.forname ("Com.mysql.jdbc.Driver");} catch (ClassNotFoundException e) {System.out.println (e);}} public void Setdatasourcename (String s) {datasourcename = S.trim ();} public void Settablename (String s) {tableName = S.trim ();} public void Setnumber (String s) {number = S.trim ();} public void SetName (String s) {name = S.trim ();} public void Setmark (double m) {mark = m;} Public String Query_stu () {String queryresult = ""; Connection con; PreparedStatement SQL; ResultSet rs;try {String uri = "jdbc:mysql://localhost:3306/" + datasourcename+ "? characterencoding=utf8&usessl= True "; String id = "root"; String password = ""; con = drivermanager.getconnection (URI, id, password); String insertcondition = "SELECT * from" + tablename+ "WHERE name=?"; sql = Con.preparestatement (Insertcondition); sql.setstring (1, name); rs = Sql.executequery (); while (Rs.next ()) {QueryResult = rs.getstring (1) + "" + Rs.getstring (2) + "" + rs.getstring (3);} Con.close ();} catch (SQLException e) {return e.tostring ();} return queryresult;} Public String query_cnt () {int cnt = 0; Connection con; PreparedStatement SQL; ResultSet rs;try {String uri = "jdbc:mysql://localhost:3306/" + datasourcename+ "? characterencoding=utf8&usessl= True "; String id = "root"; String password = ""; con = drivermanager.getconnection (URI, id, password); String insertcondition = "Select COUNT (DISTINCT number) from" + TableName + "WHERE mark>=60"; sql = Con.preparestatemen T (insertcondition); rs = Sql.executequery ();//Java.sql.SQLException:Before start of result set//workaround: Use Rs.getstring () Before you must add Rs.next ();//Reason: The ResultSet object represents the result set executed by the SQL statement and maintains the cursor to its current data row. Each time the next () method is called, the cursor moves down one line. Initially it is before the first line, so the first call to Next ()//should place the cursor on the first line, making it the current row. With each call to next () will cause the cursor to move down one line. The cursor remains valid until the Resultse object and its T-ancestor statement object are closed. while (Rs.next ()) {cnt = Rs.getint (1);}con.close ();} catch (SQLException e) {return e.tostring ();} Return string.valueof (CNT);} Public String AddRecord () {Connection con; PreparedStatement sql;try {String uri = "jdbc:mysql://localhost:3306/" + datasourcename+ "?characterencoding=utf8& Usessl=true "; String id = "root"; String password = ""; con = drivermanager.getconnection (URI, id, password); String insertcondition = "INSERT into" + tablename+ "VALUES (?,?,?)"; sql = Con.preparestatement (insertcondition); if (9 = = Number.length () &&mark>=0&&mark<=100) { Sql.setstring (1, number); Sql.setstring (2, name); Sql.setdouble (3, mark); int m = Sql.executeupdate (); if (M! = 0) {Con.clos E (); return "add Success";} else {con.close (); return "Add Failed";}} Else{con.close (); if (9 = = Number.length ()) return "Invalid result, insert failed", else return "study number illegal, insert failed";} catch (SQLException e) {return e.tostring ();}}}

The interface of the second program


Java to do the interface thinking arrangement

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.