There are a few things missing in your simple code.
You must call SwingUtilities to place the Swing components in the event dispatch stream.
You should call setDefaultCloseOperation on a JFrame .
You must call the JFrame methods in the correct order. The setSize or pack method is setVisible , then the setVisible method is called the last.
public class SimpleFrame implements Runnable { @Override public void run() { JFrame frame = new JFrame("JScroll Pane Test"); JTextArea txtNotes = new JTextArea(); txtNotes.setText("Hello World"); JScrollPane scrollPane = new JScrollPane(txtNotes); frame.add(scrollPane); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(new Dimension(800, 600)); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new SimpleFrame()); } }
source share