import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class Outer implements ActionListener {
JTextArea text;
JFrame frame;
JPanel panel;
JButton button;
public void go(){
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(500,500,500,500);
button= new JButton("change color");
Font bigFont = new Font("serif",Font.BOLD,28);
button.setFont(bigFont);
frame.setVisible(true);
text = new JTextArea(10,8);
JScrollPane scroller = new JScrollPane(text);
text.setLineWrap(true);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
text.setText("qwejuijdowijwojodwjjoskpsdko");
button.addActionListener(this);
panel=new JPanel();
panel.add(scroller);
frame.getContentPane().add(BorderLayout.NORTH,panel);
frame.getContentPane().add(BorderLayout.SOUTH,button);
text.selectAll();
text.requestFocus();
System.out.println(text.getText());
}
public static void main (String[] args) {
Outer out = new Outer();
out.go();
}
public void actionPerformed(ActionEvent ev){
text.append("jidsjojiosd \n");
}
}
TextArea在构造的时候已经说明了大小为8(字宽),然后将textarea的对象text放在scrollpane上,再把scrollpane的对象scroller放在panel上,再把panel放在pane上,运行出来的text很小,如果想让出来的这个text是占满格的,宽和frame一样,具体要怎么设置?还要我这个跑出来以后这个frame是空白的,只有点击最大化才能看到我加的这些组件,然后再最小化又看不全了,这个要怎么解决?