- Use
Event Dispatch Thread to create GUI components. - Do not call
setVisible(..) before all components have been added to the JFrame (this code snippet above shows the actual +1 error for @Clark) - Do not
JFrame class JFrame - Do not call
setSize(..) , but just call JFrame#pack() before setting the JFrame visible - Don't call
setSize() on a JTextField rather look at its constructor: JTextField(String text,int columns) - Use the appropriate
LayoutManager s, see a few examples here: Visual Guide for LayoutManager Managers
Here is an example I made (basically your patch code):
import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class JavaSwingTextfield { private JTextField myTextField; public JavaSwingTextfield() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myTextField = new JTextField("Start");
source share