除非我调整窗口大小,否则为什么我不能输入JTextArea?

时间:2016-04-18 15:31:33

标签: java jframe jtextarea

我在JFrame中放置了一个JTextArea,遇到了一个我只能在JTextArea中输入的问题,除非我调整窗口大小。我怎样才能让JTextArea让我在窗口运行时立即输入而不必调整大小?

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();
    }
}

1 个答案:

答案 0 :(得分:1)

public class Note扩展了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...
}

}

注意:

进入旧代码,

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...