Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Set size of the frame:

Code Block

setSize(400, 300);  // set frame size to 400x300

...

Code Block
 JLabel infoLbl = new JLabel();

 infoLbl.setText("Enter text here:");
 controlPnl.add(infoLbl);

JTextField infoTF = new TextField("default text");
infoTF.setPreferredSize(new java.awt.Dimension(75, 23)); // set default text field size to 75x23 pixels. 
infoTF.addActionListener(new ActionListener() {

      /**
       * This method runs when "Enter" is pressed after entering text into the text field.
       */
      public void actionPerformed(ActionEvent evt) {
           String text = infoTF.getText();  // the text of the text field.
           // Do stuff
      }
   });

controlPnl.add(infoTF);

 A text area with scroll bars in the center of the frame:

Code Block


JTextArea resultsTA = new JTextArea("initial text"
JScrollPane scrollPn = new JScrollPane();
contentPane.add(scrollPn, BorderLayout.CENTER);  // No center panel needed. ScrollPane added directly to frame.
JTextArea displayTA = new JTextArea("initial text");
scrollPn.setViewportView(displayTA);  // put the text area in the scroll pane