2016-04-18 18 views
0

Ich habe eine JTextArea in einem JFrame platziert und stoße auf ein Problem, bei dem ich nur in meine JTextArea tippen kann, wenn ich die Größe des Fensters nicht verändere. Wie kann ich JTextArea dazu bringen, dass ich tippen kann, sobald das Fenster ausgeführt wird, ohne dass die Größe geändert werden muss?Warum kann ich keinen JTextArea eingeben, wenn ich nicht die Größe des Fensters ändere?

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

Sie rufen sollte 'setVisible (true) 'nur wenn alle Komponenten hinzugefügt wurden t o der 'JFrame'. – Berger

Antwort

1

public class Hinweis erweitert 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... 
} 

}

Hinweis:

in Ihrem älteren 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... 
Verwandte Themen