简体   繁体   中英

Why can't I type in a JTextArea unless I resize the window?

I've placed a JTextArea within a JFrame and come across an issue where I can only type within my JTextArea unless I resize the window. How can I get JTextArea to let me type as soon as the window runs without having to resize it?

public class Frame extends JFrame{

    public Note() {
        this.setSize(new Dimension(1000, 1000));
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public JScrollPane createContent(){
        JTextArea textArea = new JTextArea();
        JScrollPane scrollPane = new JScrollPane(textArea);
        this.add(scrollPane, BorderLayout.CENTER);
        return null;
    }
    public static void main(String[] args) {
        new Note();
        Note mainWindow = new Note();
    }
}

public class Note extends JFrame{

private static final long serialVersionUID = 1L;

public Note() {
    createContent(); // add this line into your code.
    int x = 400;
    int y = 300;
    this.setSize(new Dimension(x, y));
    this.setTitle("Post-It Note");
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public JScrollPane createContent(){
    Color textAreaColor = new Color(248, 247, 235);
    JTextArea textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(textArea);
    scrollPane.setBorder(null);
    textArea.setBackground(textAreaColor);
    scrollPane.setBackground(textAreaColor);
    textArea.setMargin(new Insets(10, 15, 20, 20));
    this.add(scrollPane, BorderLayout.CENTER);
    return null;
}

public static void main(String[] args) {
    new Note();
  //  mainWindow.createContent(); comment this line...
}

}

Note:

into your older code,

new Note(); // this one create 1-frame..
...
// Note mainWindow = new Note(); this one also create another frame so need to comment it
//  mainWindow.createContent(); does not required because already this method called by constructor...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM