----Basics
Create a Form Object
JFrame frame = new JFrame ();
Set Window size
Frame.setsize (300, 200);
Specify where the window appears
Frame.setlocation (300, 200);
Specify the title bar of the window
Frame.settitle ("MY FRIST windows!");
Get the contents panel of a window
Container C = Frame.getcontentpane ();
Set the background color of the content panel
Color color = new Color (10, 30, 20);
C.setbackground (color);
Display window
Frame.setvisible (TRUE);
Set when closing the window is the end of the program at the same time.
Frame.setdefaultcloseoperation (Jframe.exit_on_close);
Creating components
JButton btn = new JButton ("button 1");
Add components to a form ...
This.add (BTN, Borderlayout.north);
This.add (BTN1, borderlayout.west);
This.add (BTN2, Borderlayout.south);
This.add (Btn3, borderlayout.east);
This.add (Btn4, Borderlayout.center);
This.setsize (300, 400);
This.setvisible (TRUE);
This.setdefaultcloseoperation (Jframe.exit_on_close);
Set layout mode (flow layout)
This.setlayout (New FlowLayout (flowlayout.leading));
This.setlayout (New GridLayout (4, 4));
-----Small Applications
Package GUI;
Import java.awt.*;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.util.Random;
Import javax.swing.*;
public class Test07 extends JFrame {
JTextField Txtnum;
JLabel label;
JButton btn1, btn2, btn3;
JPanel Panel;
int num = 0;
public static void Main (string[] args) {
TODO auto-generated Method Stub
Test07 frame = new Test07 ();
}
private void Initcontrol () {
Txtnum = new JTextField (20);
label = new JLabel ("Please enter a random number (1-100)");
BTN1 = new JButton ("Guess number ... ");
Btn1.addactionlistener (New Btnclick ());
BTN2 = new JButton ("Re-play ... ");
Btn3 = new JButton ("Exit ... ");
panel = new JPanel ();
Panel.add (BTN1);
Panel.add (BTN2);
Panel.add (BTN3);
This.add (Txtnum, Borderlayout.north);
This.add (label, Borderlayout.center);
This.add (panel, Borderlayout.south);
}
Public Test07 () {
Initcontrol ();
Generate a random number ...
Random random = new random ();
Generate a random number ...
num = Random.nextint ();
This.setsize (300, 200);
This.setvisible (TRUE);
This.setlocationrelativeto (NULL);
This.setdefaultcloseoperation (Jframe.exit_on_close);
}
public class Btnclick implements ActionListener {
@Override
public void actionperformed (ActionEvent e) {
TODO auto-generated Method Stub
Click on the button to get the event source ...
JButton Btnclick = (JButton) e.getsource ();
Get text on a button
String text = Btnclick.gettext ();
if (text.equals ("Guess number ... ")) {
int input = integer.valueof (Txtnum.gettext ());
if (input = = num) {
Label.settext ("Congratulations! Performance Program!!! ");
} else if (Input < num) {
Label.settext ("Guess small!!!" ");
} else if (Input > num) {
Label.settext ("Guess Big!!!" ");
} else if (Text.equals ("re-play ... ")) {
Generate a random number ...
Random random = new random ();
Generate a random number ...
num = Random.nextint () + 1;
} else {
System.exit (0);
}
}
}
}
}
Package GUI;
Import java.awt.*;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import javax.swing.*;
public class Test08 extends JFrame {
jbutton[][] Btns = new JBUTTON[10][10];
Default color
Color defaultcolor = null;
JButton btn = new JButton ();
public static void Main (string[] args) {
TODO auto-generated Method Stub
Test08 frame = new Test08 ();
}
Initialize the required components in the form
private void Initcontrol () {
for (int i = 0; i < btns.length; i++) {
for (int j = 0; J < Btns[i].length; J + +) {
JButton btn = new JButton ();
Add the row and column information to the button in the command ...
Btn.setactioncommand (i + "," + j);
Register button's Click event
Btn.addactionlistener (new ActionListener () {
@Override
public void actionperformed (ActionEvent e) {
Get Click button
JButton Btnclick = (JButton) e.getsource ();
Gets the command parameter of the button (which is the row and column number of the button in the matrix)
String command = Btnclick.getactioncommand ();
Use character segmentation to get the line number and column number of a button
string[] STRs = Command.split (",");
Get the line number and column number for shaping
int row = Integer.parseint (Strs[0]);
int col = integer.parseint (strs[1]);
Declare the buttons around the top and bottom
JButton topbtn = null;
JButton downbtn = null;
JButton leftbtn = null;
JButton rigthbtn = null;
Get the top button
if (row-1 >= 0) {
TOPBTN = Btns[row-1][col];
}
Get the button on the left
if (col-1 >= 0) {
LEFTBTN = Btns[row][col-1];
}
Get the button below
if (row + 1 < btns.length) {
DOWNBTN = Btns[row + 1][col];
}
Get the button on the right
if (col + 1 < btns.length) {
RIGTHBTN = Btns[row][col + 1];
}
jbutton[] Arrbtns = new JBUTTON[5];
Arrbtns[0] = Btnclick;
ARRBTNS[1] = topbtn;
ARRBTNS[2] = downbtn;
ARRBTNS[3] = leftbtn;
ARRBTNS[4] = rigthbtn;
Changcolor (ARRBTNS);
/*
* Btnclick.setbackground (color.red);
* Topbtn.setbackground (color.red);
* Downbtn.setbackground (color.red);
* Leftbtn.setbackground (color.red);
* Rigthbtn.setbackground (color.red);
*/
}
});
BTNS[I][J] = BTN;
This.add (BTN);
}
}
}
Change buttons and surrounding colors
private void Changcolor (jbutton[] nearbtns) {
for (int i = 0; i < nearbtns.length; i++) {
if (nearbtns[i]! = null) {
if (nearbtns[i].getbackground () = = color.red) {
To become the original color
Nearbtns[i].setbackground (DefaultColor);
} else {
Nearbtns[i].setbackground (color.red);
}
}
}
}
Construction method
Public Test08 () {
Initcontrol ();
This.setlayout (New GridLayout (10, 10));
Gets the default color of the button
DefaultColor = Btn.getbackground ();
This.setsize (300, 350);
This.setvisible (TRUE);
This.setlocationrelativeto (NULL);
This.setdefaultcloseoperation (Jframe.exit_on_close);
}
}
Java GUI Programming