简体   繁体   中英

simple scrollable textArea in java swing

im learning swing i wrote a code to show simple text Area in jpanel but there is only panel shows not the text Area .

main frame class

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 TextPanel textPanel;
private JButton button;

public MainFrame(){

    super("Hello World!");
    this.textPanel = new TextPanel();
    this.button = new JButton("Click me");

    this.setLayout(new BorderLayout());

    this.add(textPanel,BorderLayout.CENTER);
    this.add(button,BorderLayout.SOUTH);

    button.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

        }
    });

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(600, 500);
    this.setVisible(true);

}

private void add(TextPanel textPanel2, String center) {
    // TODO Auto-generated method stub
};
}

and the second class that contain the panel and the text area is

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public class TextPanel extends JPanel {

private JTextArea textArea;

public TextPanel() {


    textArea = new JTextArea();

    setLayout(new BorderLayout());

    add(new JScrollPane(textArea),BorderLayout.CENTER);

}
}

在此处输入图片说明

but there is no text area just the panel , also there is no errors in console im using java 7 , so what is wrong with my code .

Remove this method which is overriding Container's add method

private void add(TextPanel textPanel2, String center) {
    // TODO Auto-generated method stub
};

Seems like TextPanel is not giving any new functionality. Why not just add the JScrollPane component directly to the frame?

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