MainFrame.java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class MainFrame extends JFrame{
private JTextArea textArea;
private JButton btn;
private TextPanel textPanel;
public MainFrame() {
super("My First JAVA Swing Window");
setLayout(new BorderLayout());
textArea = new JTextArea();
btn = new JButton("Click Me");
textPanel = new TextPanel();
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.append("Button has been clicked!\n");
//textPanel.appendText("Button has been clicked!\n");
}
});
add(textArea, BorderLayout.CENTER);
add(btn, BorderLayout.SOUTH);
setSize(600,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
My goal is to add a text (Button has been clicked.) to the text area if I click the button, I managed to do it but I tried to seperate the text area in a different class, and if I seperate it. it doesnt work anymore.. What I see is it dont even add the text area to the border layout..: This is how I try to seperate and thats the part thats not working. MainFrame.java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class MainFrame extends JFrame{
//private JTextArea textArea;
private JButton btn;
private TextPanel textPanel;
public MainFrame() {
super("My First JAVA Swing Window");
setLayout(new BorderLayout());
//textArea = new JTextArea();
btn = new JButton("Click Me");
textPanel = new TextPanel();
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//textArea.append("Button has been clicked!\n");
textPanel.appendText("Button has been clicked!\n");
}
});
//add(textArea, BorderLayout.CENTER);
add(btn, BorderLayout.SOUTH);
setSize(600,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
TextPanel.java
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class TextPanel extends JPanel{
private JTextArea textArea;
public TextPanel() {
textArea = new JTextArea();
setLayout(new BorderLayout());
add(textArea, BorderLayout.CENTER);
}
public void appendText(String text) {
textArea.append(text);
}
}
And of course there is a Main class which runs the MainFrame...
The problem is that you called add(textArea, BorderLayout.CENTER);
in your MainFrame.java
before the refactoring. The add
method there adds the textArea
to the layout of the JFrame
.
But afterwards you don't add the TextPanel
to the JFrame
, but only to a local BorderLayout
of the JPanel
your extending of. That BorderLayout
isn't added anywhere.
It should work, if you still call add(textPanel, BorderLayout.CENTER)
;
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.