A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘云龙 中级黑马   /  2012-8-1 03:11  /  2231 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


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带滚动条?

2 个回复

倒序浏览
哥们儿,你确定你运行这个程序了吗,如果你现拿回去直接运行,就一个空窗口,什么都没有。你的setVisible放的地方不对。

java.awt.TextArea 在内部处理滚动。JTextArea 的不同之处在于,它不管理滚动,但实现了 swing Scrollable 接口。这允许把它放置在 JScrollPane 的内部(如果需要滚动行为),或者直接使用(如果不需要滚动)。

自己研究一下,印象很深刻的
回复 使用道具 举报
看看..................
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马