Java online chat project swt Visual Window Design login box Registration button click to change the window size -- the registration panel appears to enable the login box when the screen is centered,
Click the "register" button in the logon box to change the window size. The registration panel appears.
FirstUse swt to visually design the logon window, for example:
The window height is 578.
The registration height is 301 when you do not click it (you can set it yourself)
Note: Select Title border for the Jpanel of the registered user. The title attribute is "registered user"
Layout select Absolute Layout
NextAfter the dialog box window is designed, double-click the register button to edit the code, and add an if judgment to the listening code of the register button. if it is equal to 301, the window height is changed to 578, otherwise, change to 301.
Because the anonymous internal class is used, you cannot directly use this. keyword to call the getHeight () and setHeight () methods. Therefore, you can use the LoginDialog. this. Method to call it.
The complete code is as follows:
Package com. swift; import java. awt. eventQueue; import java. awt. event. actionEvent; import java. awt. event. actionListener; import java. awt. event. windowAdapter; import java. awt. event. using wevent; import javax. swing. JButton; import javax. swing. JDialog; import javax. swing. JFrame; import javax. swing. JLabel; import javax. swing. JPanel; import javax. swing. JPasswordField; import javax. swing. JTextField; import javax. swing. UIManager; import javax. swing. unsupportedLookAndFeelException; import javax. swing. border. titledBorder; public class logindiextends JDialog {private JPasswordField passwordField_2; private JPasswordField passwordField_1; private JTextField textField_2; private JTextField textField; public static void main (String args []) {JFrame. setdefalooklookandfeeldecorated (true); JDialog. setdefalooklookandfeeldecorated (true); try {UIManager. setLookAndFeel ("javax. swing. plaf. nimbus. nimbusLookAndFeel ");} catch (ClassNotFoundException e1) {// TODO Auto-generated catch block e1.printStackTrace ();} catch (InstantiationException e1) {// TODO Auto-generated catch block e1.printStackTrace ();} catch (IllegalAccessException e1) {// TODO Auto-generated catch block e1.printStackTrace ();} catch (UnsupportedLookAndFeelException e1) {// TODO Auto-generated catch block e1.printStackTrace ();} EventQueue. invokeLater (new Runnable () {public void run () {try {LoginDialog dialog = new LoginDialog (); dialog. addWindowListener (new WindowAdapter () {public void windowClosing (invalid wevent e) {System. exit (0) ;}}); dialog. setVisible (true);} catch (Exception e) {e. printStackTrace () ;}}) ;}public LoginDialog () {super (); setResizable (false); setTitle ("online chat logon box"); getContentPane (). setLayout (null); setBounds (100,100,427,301); // The registration height is 578, not registered as 301 final JButton button_1 = new JButton (); button_1.setText ("login "); button_1.setBounds (230,222,106, 36); getContentPane (). add (button_1); final JTextField textField_1 = new JTextField (); textField_1.setBounds (148, 90,192, 42); getContentPane (). add (textField_1); final JLabel label = new JLabel (); label. setText ("Account Number"); label. setBounds (76,102, 66, 18); getContentPane (). add (label); final JLabel label_1 = new JLabel (); label_1.setText ("password"); label_1.setBounds (76,167, 66, 18); getContentPane (). add (label_1); final JPasswordField passwordField = new JPasswordField (); passwordField. setBounds (148,155,192, 42); getContentPane (). add (passwordField); final JButton button = new JButton (); button. addActionListener (new ActionListener () {public void actionreceivmed (final ActionEvent e) {if (LoginDialog. this. getHeight () = 301) {// use LoginDialog. this. to determine the height. The front side has setResizable (false); LoginDialog. this. setSize (427,578);} else {LoginDialog. this. setSize (427,301) ;}}); button. setText ("register"); button. setBounds (76,222,106, 36); getContentPane (). add (button); final JPanel = new JPanel (); panel. setLayout (null); panel. setBorder (new TitledBorder (null, "registered user", TitledBorder. DEFAULT_JUSTIFICATION, TitledBorder. DEFAULT_POSITION, null, null); panel. setBounds (10,278,401,226); getContentPane (). add (panel); final JLabel label_2 = new JLabel (); label_2.setBounds (44, 41, 65, 18); label_2.setText ("mobile phone number:"); panel. add (label_2); textField = new JTextField (); textField. setBounds (115, 35,225, 30); panel. add (textField); final JButton button_2 = new JButton (); button_2.setText ("Send verification"); button_2.setBounds (243, 75, 97, 30); panel. add (button_2); textField_2 = new JTextField (); textField_2.setBounds (115,104, 95, 30); panel. add (textField_2); final JLabel label_3 = new JLabel (); label_3.setText ("Verification Code:"); label_3.setBounds (44,110, 65, 18); panel. add (label_3); passwordField_1 = new JPasswordField (); passwordField_1.setBounds (115,143,231, 30); panel. add (passwordField_1); passwordField_2 = new JPasswordField (); passwordField_2.setBounds (115,175,231, 30); panel. add (passwordField_2); final JLabel label_4 = new JLabel (); label_4.setText ("Password:"); label_4.setBounds (44,149, 65, 18); panel. add (label_4); final JLabel label_5 = new JLabel (); label_5.setText ("Verification password:"); label_5.setBounds (44,181, 65, 18); panel. add (label_5); final JButton button_3 = new JButton (); button_3.setBounds (47,510, 97, 30); getContentPane (). add (button_3); button_3.setText ("discard"); final JButton button_4 = new JButton (); button_4.setBounds (262,510, 97, 30); getContentPane (). add (button_4); button_4.setText ("registered user ");}}
Center the screen when the logon box is opened
Because the screen center function is required for other windows, see a tool class.
PrincipleIs to obtain the screen width and height
Subtract the width and height of the dialog box
Divide by 2
The Point in the center of the window -- Point
FirstCreate a class com. swift. util. Center
There is a getPoint (int width, int height) method in it)
And a packaged overload method for convenient calling.
GetPoint (Dimension d)
The Code is as follows:
Package com. swift. util; import java. awt. dimension; import java. awt. point; import java. awt. toolkit; public class Center {public static Point getPoint (int width, int height) {Toolkit toolkit = Toolkit. getdefatooltoolkit (); // It should be a singleton int screenW = toolkit. getScreenSize (). width; int screenH = toolkit. getScreenSize (). height; int x = (screenW-width)/2; int y = (screenH-height)/2; Point p = new Point (x, y); return p ;} // function overload. The parameter is the packaging class size -- Dimension public static Point getPoint (Dimension d) {Point p = getPoint (d. width, d. height); return p ;}}
UseTool class centered
This. setLocation (Center. getPoint (this. getSize ()));
After setting the width and height of the dialog box, reset the window position.
LoginDialog. this. setLocation (Center. getPoint (LoginDialog. this. getSize ()));
In the anonymous internal class, when you click the register button and the window size is changed again, the position of the window must be constantly changed. The internal class uses LoginDialog. this.
Modify all the Code as follows:
Package com. swift; import java. awt. dimension; import java. awt. eventQueue; import java. awt. event. actionEvent; import java. awt. event. actionListener; import java. awt. event. windowAdapter; import java. awt. event. using wevent; import javax. swing. JButton; import javax. swing. JDialog; import javax. swing. JFrame; import javax. swing. JLabel; import javax. swing. JPanel; import javax. swing. JPasswordField; import javax. swing. JTextField; import javax. swing. UIManager; import javax. swing. unsupportedLookAndFeelException; import javax. swing. border. titledBorder; import com. swift. util. center; public class LoginDialog extends JDialog {private JPasswordField passwordField_2; private JPasswordField passwordField_1; private JTextField textField_2; private JTextField textField; public static void main (String args []) {JFrame. setdefalooklookandfeeldecorated (true); JDialog. setdefalooklookandfeeldecorated (true); try {UIManager. setLookAndFeel ("javax. swing. plaf. nimbus. nimbusLookAndFeel ");} catch (ClassNotFoundException e1) {// TODO Auto-generated catch block e1.printStackTrace ();} catch (InstantiationException e1) {// TODO Auto-generated catch block e1.printStackTrace ();} catch (IllegalAccessException e1) {// TODO Auto-generated catch block e1.printStackTrace ();} catch (UnsupportedLookAndFeelException e1) {// TODO Auto-generated catch block e1.printStackTrace ();} EventQueue. invokeLater (new Runnable () {public void run () {try {LoginDialog dialog = new LoginDialog (); dialog. addWindowListener (new WindowAdapter () {public void windowClosing (invalid wevent e) {System. exit (0) ;}}); dialog. setVisible (true);} catch (Exception e) {e. printStackTrace () ;}}) ;}public LoginDialog () {super (); setResizable (false); setTitle ("online chat logon box"); getContentPane (). setLayout (null); setBounds (100,100,427,301); // The registration height is 578, and the non-registration value is 301. // set the window to center this. setLocation (Center. getPoint (this. getSize (); final JButton button_1 = new JButton (); button_1.setText ("login"); button_1.setBounds (230,222,106, 36); getContentPane (). add (button_1); final JTextField textField_1 = new JTextField (); textField_1.setBounds (148, 90,192, 42); getContentPane (). add (textField_1); final JLabel label = new JLabel (); label. setText ("Account Number"); label. setBounds (76,102, 66, 18); getContentPane (). add (label); final JLabel label_1 = new JLabel (); label_1.setText ("password"); label_1.setBounds (76,167, 66, 18); getContentPane (). add (label_1); final JPasswordField passwordField = new JPasswordField (); passwordField. setBounds (148,155,192, 42); getContentPane (). add (passwordField); final JButton button = new JButton (); button. addActionListener (new ActionListener () {public void actionreceivmed (final ActionEvent e) {if (LoginDialog. this. getHeight () == 301) {LoginDialog. this. setSize (427,578);} else {LoginDialog. this. setSize (427,301 );}
// The setting window is centered continuously. this. setLocation (Center. getPoint (LoginDialog. this. getSize () ;}}); button. setText ("register"); button. setBounds (76,222,106, 36); getContentPane (). add (button); final JPanel = new JPanel (); panel. setLayout (null); panel. setBorder (new TitledBorder (null, "registered user", TitledBorder. DEFAULT_JUSTIFICATION, TitledBorder. DEFAULT_POSITION, null, null); panel. setBounds (10,278,401,226); getContentPane (). add (panel); final JLabel label_2 = new JLabel (); label_2.setBounds (44, 41, 65, 18); label_2.setText ("mobile phone number:"); panel. add (label_2); textField = new JTextField (); textField. setBounds (115, 35,225, 30); panel. add (textField); final JButton button_2 = new JButton (); button_2.setText ("Send verification"); button_2.setBounds (243, 75, 97, 30); panel. add (button_2); textField_2 = new JTextField (); textField_2.setBounds (115,104, 95, 30); panel. add (textField_2); final JLabel label_3 = new JLabel (); label_3.setText ("Verification Code:"); label_3.setBounds (44,110, 65, 18); panel. add (label_3); passwordField_1 = new JPasswordField (); passwordField_1.setBounds (115,143,231, 30); panel. add (passwordField_1); passwordField_2 = new JPasswordField (); passwordField_2.setBounds (115,175,231, 30); panel. add (passwordField_2); final JLabel label_4 = new JLabel (); label_4.setText ("Password:"); label_4.setBounds (44,149, 65, 18); panel. add (label_4); final JLabel label_5 = new JLabel (); label_5.setText ("Verification password:"); label_5.setBounds (44,181, 65, 18); panel. add (label_5); final JButton button_3 = new JButton (); button_3.setBounds (47,510, 97, 30); getContentPane (). add (button_3); button_3.setText ("discard"); final JButton button_4 = new JButton (); button_4.setBounds (262,510, 97, 30); getContentPane (). add (button_4); button_4.setText ("registered user ");}}