问个问题~高手帮帮忙~谢谢!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class lf77 extends JFrame implements ActionListener{
JTextArea t2=new JTextArea(9,10);
JButton b=new JButton( "COPY ");
List list=new List(10);
public lf77(){
super( "图形 ");
Container c=getContentPane();
c.setLayout(new FlowLayout());
c.add(list);
c.add(b);
c.add(t2);
list.add( "1 ");
list.add( "2 ");
list.add( "3 ");
list.select(2);
setSize(500,500);
setVisible(true);
b.addActionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){System.exit(0);}});
}
public void actionPerformed(ActionEvent e)
{
t2.setText(list.getSelectedItem());
}
public static void main(String arg[])
{ new lf77();
}}
这个程序是把list里的文件给文本框,可是有个问题就是1给了以后在给2时就替换1了~怎么才能把1,2,3都给文本框的?谢谢帮忙了
------解决方案--------------------t2.setText(t2.getText()+list.getSelectedItem());
把先前的内容和后面的内容连在一起
------解决方案--------------------加换行符
t2.setText(t2.getText()+(t2.getText().equals( " ")? " ": "\n ")+list.getSelectedItem());