IM requirements
Today we started the development of IM projects as a comprehensive practice in our near-stage learning.
The project needs are as follows:
Since the previous SVN project I was responsible for the project background logic processing, so this time I want to try the foreground UI design. After nearly three hours of struggle tonight, I also checked out a lot of knowledge points, summed up here:
Summary of Knowledge points
1.
Remove window decoration this.setundecorated (true);
2.
JComboBox JCB = new JComboBox (); jcb.seteditable (true); Jcb.setbounds (new Rectangle (260, 75, 240, 35));
This is JComboBox.
Write
GetSelectedItem ()
Setselecteditem (String str)
3.
JLabel jl3 = new JLabel ("registration"), Jl3.setbounds (New Rectangle (188,, +)), Jl3.setfont (New Font ("Dialog", 1,)); Jl3. Setforeground (Color.Blue);
4.
Setting the window to minimize
SetState (frame.iconified);
Set Program to close
System.exit (0);
For custom minimize and Close buttons, DIY is a cool thing to do!
5.
Load the picture onto JLabel JLabel label_background = new JLabel (New ImageIcon ("image/2.jpg")); label_background.setbounds (0, 0, This.getwidth (), This.getheight ()), or//equivalent to JLabel affixed to the bottom layer of the second panel This.getlayeredpane (). Add (Label_background, New Integer ( Integer.min_value));//JComponent is the parent class of JPanel, which is used to get the first panel of the window jcomponent JC = (jcomponent) this.getcontentpane ();// Set the first layer panel transparent Jc.setopaque (false);
Source code sharingView Package StructureThis is the structure of my view package, where chat is the chatting interface class, listener is the listener class, main is the main window class, which is similar to the QQ Friend List window, start is the beginning of the login and registration interface. (Hope to learn more about the canonical naming method)
Startframe
Package View.start;import Java.awt.borderlayout;import Java.awt.color;import java.awt.dimension;import Java.awt.flowlayout;import Java.awt.font;import Java.awt.rectangle;import Java.awt.event.mouseadapter;import Java.awt.event.mouseevent;import Javax.swing.imageicon;import Javax.swing.jbutton;import Javax.swing.JComboBox; Import Javax.swing.jcomponent;import javax.swing.jframe;import Javax.swing.jlabel;import Javax.swing.JPanel;import Javax.swing.jpasswordfield;import Javax.swing.jtextfield;import View.listener.myactionlistener;import View.listener.mymouseadapter;public class Startframe extends JFrame {public static startframe sf;public static void Main ( String[] args) {SF = new startframe ();} Public Startframe () {initstartframe ();} private void Initstartframe () {this.setsize (550, 280); This.setlocationrelativeto (null); this.setresizable (false); This.setlayout (null); this.setdefaultcloseoperation (3);//Remove window decoration this.setundecorated (true); JLabel jl1 = new JLabel ("username") Jl1.setbounds (New Rectangle (200, 65, 50, 50)); JComboBox JCB = new JComboBox (); jcb.seteditable (true); Jcb.setbounds (new Rectangle (260, 75, 240, 35)); JLabel jl2 = new JLabel ("password"); Jl2.setbounds (new Rectangle (200, 120, 50, 50)); JPasswordField JPF = new JPasswordField, Jpf.setbounds (New Rectangle (260, 130, 240, 35)); JButton jb1 = new JButton ("login"); Jb1.setbounds (new Rectangle (260, 187, 240, 35)); JLabel jl3 = new JLabel ("registration"), Jl3.setbounds (New Rectangle (188,, +)), Jl3.setfont (New Font ("Dialog", 1,)); Jl3. Setforeground (Color.Blue); Mymouseadapter MMA = new Mymouseadapter (JCB, JPF); Jl3.addmouselistener (MMA);//Add picture JPanel JP = new JPanel (); jp.setbounds (New Rectangle (Jp.setbackground), color.white);///Zoom Out button JButton Button_narrow = new JButton ("narrow"); Button_narrow.setbounds (new Rectangle (450, 0, 50, 35));//Close button JButton button_close = new JButton ("close"); Button_ Close.setbounds (New Rectangle (500, 0, 50, 35)); Myactionlistener mal = new Myactionlistener (JCB, JPF); Jb1.addactionlistener (MAL); ButtoN_close.addactionlistener (MAL); Button_narrow.addactionlistener (MAL);//Load picture onto JLabel jlabel label_background = new JLabel (New ImageIcon ("Image/2.jpg")) label_background.setbounds (0, 0, this.getwidth (), This.getheight ());// The equivalent of JLabel is affixed to the bottom this.getlayeredpane () of the second Layer panel. Add (Label_background, New Integer (Integer.min_value));// JComponent is the parent class of JPanel, which is used to get the first panel of the window jcomponent JC = (jcomponent) this.getcontentpane ();//Set the first panel transparency Jc.setopaque (false ); This.add (Button_narrow); This.add (button_close); This.add (JP); This.add (JL1); This.add (JCB); This.add (JL2); This.add (JPF); This.add (JB1); This.add (JL3); this.setvisible (True);}}
Myactionlistener
Package View.listener;import Java.awt.frame;import Java.awt.event.actionevent;import Java.awt.event.ActionListener ; Import Javax.swing.jbutton;import Javax.swing.jcombobox;import Javax.swing.jpasswordfield;import Javax.swing.jtextfield;import View.start.startframe;public class Myactionlistener implements ActionListener {private JComboBox jcb;private JPasswordField jpf;public myactionlistener (JComboBox JCB, JPasswordField jpf) {THIS.JCB = Jcb;this . JPF = JPF;} public void actionperformed (ActionEvent e) {JButton JB = (JButton) e.getsource (); String type = Jb.gettext ();//Gets the user name and password String name = (string) jcb.getselecteditem (); String psd = jpf.gettext (); switch (type) {case "login"://TODO Login Operation System.out.println ("login" + name + PSD);//startframe.sf.s Etvisible (false); Break;case "narrow": StartFrame.sf.setState (frame.iconified); Break;case "Close": System.exit (0); break;} Jcb.setselecteditem (""); Jpf.settext ("");}}
Mymouselistener
Package View.listener;import Java.awt.event.mouseadapter;import Java.awt.event.mouseevent;import Javax.swing.jcombobox;import Javax.swing.jpasswordfield;import View.start.startframe;public class MyMouseAdapter Extends Mouseadapter {private JComboBox jcb;private JPasswordField jpf;public mymouseadapter (JComboBox JCB, JPasswordField jpf) {THIS.JCB = JCB;THIS.JPF = JPF;} public void mouseclicked (MouseEvent e) {//obtains the user name and password string name = (string) jcb.getselecteditem (); String psd = Jpf.gettext ();//TODO Register Operation SYSTEM.OUT.PRINTLN ("register" + name + PSD);//StartFrame.sf.setVisible (FALSE);}}
Run Results (login and registration Interface)
self-feelingThe interface is still a prototype and must be further perfected (swing is so ugly ...). With the progress of the project, finally hope to be able to present a better effect, tonight out to do this interface, but also review some of the previous knowledge points. Three hours such as White horse gap, finally understand why the programmer always stay up late, the original is the code knocking, interest came, stop it. (I heard most of the situation is to catch the project ...) I am too young after all ... )
Im project Process (i)