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

标签 java eclipse swing

我正在尝试添加水平和垂直滚动条以在打开后读取整个文本文件。因为我构建文件的方式仅部分显示。请看下面的图片和代码。我做错了什么吗?更新代码

Updated picture

  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);
}
}

我不知道为什么它显示如图所示。请指教。

最佳答案

你必须使用

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

并更正 JButton 的位置。试试这个代码:

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

    }
});
  }
}

关于java - 如何在 JTextArea 中添加垂直和水平滚动条以查看完整文件 - JAVA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53486852/

相关文章:

java - 带 mouseEvent 处理的 JPanel 边框

java - 打开数据文件、追加行和分割数据

java - 如何在 JRadioButtons 中使用图像而不是文本

javascript - Vaadin onbeforeunload 事件

java - 为什么不鼓励间接访问静态成员?

java - Eclipse 在调试时显示 Java 字节数组的红色值

java - 在 GWT 中创建独立代码

java - 格式化 JEditorPane 的光标、工具提示、链接

java - 有没有办法使用youtube java库获取用户创建的播放列表?

java - 使用 HTML 中的 ITextRenderer 在每个页面上添加页眉或页脚