简体   繁体   English

如何在JTextArea中添加垂直和水平滚动条以查看完整文件-JAVA

[英]How to add a vertical and horizontal scroll bar in a JTextArea to view complete file - JAVA

I am trying to add a horizontal and vertical scroll bar to read the entire text file after opening it. 我试图添加一个水平和垂直滚动条,以在打开它后读取整个文本文件。 Because the way I have it structured the file is only partially displayed. 因为我的结构方式仅部分显示了文件。 Please see the image below and code. 请参见下面的图像和代码。 Is there something that I am doing incorrectly? 我做错了什么吗? Updated Code 更新的代码

更新图片

  public class OpenFile {

private JFrame frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                OpenFile window = new OpenFile();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public OpenFile() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //frame.getContentPane().setLayout(null);

    JTextArea jTextArea1 = new JTextArea();
    jTextArea1.setBounds(15, 45, 398, 183);
    //frame.getContentPane().add(new JScrollPane(jTextArea1));


    JButton btnNewButton = new JButton("Read Text");
    frame.getContentPane().add(btnNewButton, BorderLayout.SOUTH);
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            JFileChooser chooser = new JFileChooser();
            chooser.showOpenDialog(null);
            File f = chooser.getSelectedFile();
            String nameFile = f.getAbsolutePath();

            try {

                FileReader reader = new FileReader(nameFile);
                BufferedReader br = new BufferedReader(reader);
                jTextArea1.read(br, null);
                br.close();
                jTextArea1.requestFocus();
            }
            catch(Exception e){
                JOptionPane.showMessageDialog(null, e);
            }


        }
    });
    btnNewButton.setBounds(15, 16, 115, 29);
    frame.getContentPane().add(btnNewButton);
}
}

I have no clue why it is displaying as shown in the picture. 我不知道为什么它如图所示显示。 Please advise. 请指教。

初始化JTextArea对象时,请考虑将行数和列数作为构造函数参数。

You have to use 你必须用

JScrollPane scrollBar = new JScrollPane(jTextArea1);
scrollBar.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollBar.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

And correct the position of the JButton. 并更正JButton的位置。 Try this code: 试试这个代码:

public class OpenFile {

private JFrame frame;

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
    public void run() {
        try {
            OpenFile window = new OpenFile();
            window.frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});
}

public OpenFile() {
initialize();
}

private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

JTextArea jTextArea1 = new JTextArea();
jTextArea1.setText("Here is a text ... ");
jTextArea1.setBounds(10, 39, 402, 199);

frame.getContentPane().add(jTextArea1);

JScrollPane scrollBar = new JScrollPane(jTextArea1);
scrollBar.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollBar.setHorizontalScrollBarPolicy(
    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(scrollBar);
scrollBar.setBounds(13, 39, 413, 214);

JButton btnNewButton = new JButton("Read the text");
btnNewButton.setBounds(149, 10, 130, 21);
frame.getContentPane().add(btnNewButton);

btnNewButton.addActionListener(new ActionListener () {
    public void actionPerformed(ActionEvent arg0) {
        JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(null);
        File f = chooser.getSelectedFile();
        String nameFile = f.getAbsolutePath();

        try {

            FileReader reader = new FileReader(nameFile);
            BufferedReader br = new BufferedReader(reader);
            jTextArea1.read(br, null);
            br.close();
            jTextArea1.requestFocus();
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }


    }

    @Override
    public void actionPerformed(java.awt.event.ActionEvent e) {
        // TODO Auto-generated method stub

    }
});
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM