-
Do not add components to
JFrame
after theJFrame
is visible (setVisible(true)
) -
Not really good practice to call
setSize()
on frame rather callpack()
(CausesJFrame
to be sized to fit the preferred size and layouts of its subcomponents) and letLayoutManager
handle the size. -
Use EDT (Event-Dispatch-Thread)
-
call
JFrame#setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
as said by @Gilbert Le Blanc (+1 to him) or else your EDT/Initial thread will remain active even afterJFrame
has been closed
Like so:
public static void main(String[] args) {
//Create GUI on EDT Thread
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("JScroll Pane Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea txtNotes = new JTextArea();
txtNotes.setText("Hello World");
JScrollPane scrollPane = new JScrollPane(txtNotes);
frame.add(scrollPane);//add components
frame.pack();
frame.setVisible(true);//show (after adding components)
}
});
}