使用swing组件在框架中打开文本文件 [英] Opening a text file in a frame using swing components

查看:149
本文介绍了使用swing组件在框架中打开文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用swing组件在框架中打开文本文件,最好使用突出显示功能。我在第一帧中的文本中获取文本文件的名称,并希望在第二帧中打开文本文件。我的代码是

I want to open a text file in a frame using swing components, preferably with highlighting facility. I get the name of the text file in a text filed in the first frame and want to open the text file in the second frame.My code is

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class FirstGUI extends JFrame {

private JLabel label;
private JTextField textfield;
private JButton button;

public FirstGUI() {
    setLayout(new FlowLayout());

    label = new JLabel("Enter the file path:");
    add(label);

    textfield = new JTextField();
    add(textfield);

    button = new JButton("Open");
    add(button);

    AnyClass ob = new AnyClass();
    button.addActionListener(ob);
}

public class AnyClass implements ActionListener {
    public void actionPerformed(ActionEvent obj) {
        //SecondGUI s =new SecondGUI();
        //s.setVisible(true);
    }
}

public static void main(String[] args) {

    FirstGUI obj= new FirstGUI();
    obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    obj.setSize(600,600);
    obj.setLocation(100,100);
    obj.setVisible(true);
}
}






我应该在第二帧中使用什么swing组件来打开文本文件?如果可能的话,请提供代码大纲!!


What swing component should I use in my second frame to open a text file in it ? If possible, please provide the outline of the code !!

推荐答案

最简单的选择是 JTextArea

另一个更好的选择是 JEditorPane

您可以查看文本组件教程,可以更好地理解它们并选择所需的最佳组合。

You can take a look at this text components tutorial for understanding them better and choosing the best you need.

这篇关于使用swing组件在框架中打开文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆