Problems and solutions of GUI in Java

Source: Internet
Author: User

Record the experience in the learning process, just for the future do not repeat the error.
So many times when you finish GUI interface, a very interesting question is that you have to resize the window to show the component, not to display the component immediately, so Google discovers that the window (JFrame) is set to visible before the component has been added to the panel (setvisible ()), Control does not throw a redraw. The workaround is simple enough to set the JFrame setvisible () to true only when all the components are ready. Or if you're stubborn and you have to add components after calling setvisible (), you can do the same: Getcontentpane (). invalidate ();
Incidentally, another problem encountered, the addition of the Password box and text box component in the Java.swing JFrame is not displayed. The reason is that the default layout for JFrame is BorderLayout, which displays the last added component in the Borderlayout.center position in the form of a full area. Credit GetLayout () look at the layout of the JFrame, everything will be the truth.
The source code used in this experiment:

public class Xiaozujian extends JFrame {private JLabel loginl;
	Private JLabel passwordl;
	Private JTextField login;
	Private Container Containter;
	Private JTextArea textarea;
	private JPasswordField password;
		Public Xiaozujian () {initframe ();
		Init ();
		Login.settext ("" +getlayout ());
		SetVisible (TRUE);//Leave this method behind so that all components are immediately visible, is critical} public void Initframe () {Settitle ("Widgets");
	
		SetSize (400,400);
	Containter=getcontentpane ();
		SetBackground (Color.Blue);	
	Setdefaultcloseoperation (Jframe.exit_on_close);
		public void init () {loginl=new JLabel ("Logon name:");
		Login=new JTextField (10);
		Containter.add (LOGINL);
		Containter.add (login);
		Passwordl=new JLabel ("Password:");
		Password=new JPasswordField (10);
		Containter.setlayout (New FlowLayout ());//The layout must be set, otherwise the Containter.add (PASSWORDL) is not displayed;
		Containter.add (password);
		Textarea=new JTextArea (8,20);
		Containter.add (New JScrollPane (textarea)); Password.addactionlistener (new ActionListener () {public void ActionpeRformed (ActionEvent e) {textarea.append ("sign-in Name:" +login.gettext () + "" + "Password:" +password.getselectedtext ());//getselec
		   Tedtext () Displays the selected Textarea.append ("\ n");
	}

		
		});
	public static void Main (string[] args) {//TODO auto-generated method stub Xiaozujian x=new Xiaozujian (); }

}

Ma, the text part of the adjustment is still more than a gray area, Gray often do not understand, this makes Virgo I do

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.