import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class B_FileDialog
{
JFrame frm;
JMenuBar bar;
JMenu m;
JMenuItem itemOpen;
JMenuItem itemSave;
JMenuItem itemExit;
//JScrollPane sp;
JTextArea ta;
FileDialog openDia;
JFileChooser openChooser;
//JFileChooser saveDia;
void initGUI()
{
frm=new JFrame("frame");
bar=new JMenuBar();
m=new JMenu("file");
itemOpen=new JMenuItem("open");
itemSave=new JMenuItem("save");
itemExit=new JMenuItem("exit");
//sp=new JScrollPane();
ta=new JTextArea();
openDia=new FileDialog(frm,"which are u going to open",FileDialog.LOAD);
//openChooser=new JFileChooser();
frm.setBounds(300, 300, 360, 360);
//frm.setLayout(new FlowLayout());
frm.setVisible(true);
m.add(itemOpen);
m.add(itemSave);
m.add(itemExit);
bar.add(m);
//sp.add(ta);
frm.setJMenuBar(bar);
frm.add(ta);
//frm.add(openChooser);
}
void addEvent()
{
frm.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
itemOpen.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
openDia.setVisible(true);
String dir=openDia.getDirectory();
String fileName=openDia.getFile();
if(dir==null||fileName==null) return ;
ta.setText("");
File file=new File(dir,fileName);
BufferedReader br=null;
try
{
String line=null;
br=new BufferedReader(new FileReader(file));
while((line=br.readLine())!=null) ta.append(line+"\n");//ta.setText(str);
}
catch(IOException e)
{
throw new RuntimeException();
}
finally
{
try
{
if(br!=null) br.close();
}
catch(IOException e)
{
System.out.println("close err");
}
}
}
});
}
B_FileDialog()
{
initGUI();
addEvent();
}
public static void main(String[] args)
{
new B_FileDialog();
}
}
这是我写的一个带JTextArea的GUI程序。
问题是:
JTextArea不带滚动条,我知道TextArea会带,那么在swing类中如何实现JTextArea带滚动条?
|
|