我想通过点击检查按钮从我的textField中读取文件。如果我点击检查按钮它没有显示任何内容。它应该在textFiled_2中显示内容。但它不是从textField读取文件。 请帮忙。谢谢。
public class HLS {
private JFrame frmHttpsLiveStreaming;
private JTextField textField_2;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
HLS window = new HLS();
window.frmHttpsLiveStreaming.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public HLS() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmHttpsLiveStreaming = new JFrame();
frmHttpsLiveStreaming.setTitle("HTTPS Live Streaming");
frmHttpsLiveStreaming.setBounds(100, 100, 494, 322);
frmHttpsLiveStreaming.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmHttpsLiveStreaming.getContentPane().setLayout(null);
JButton btnBrowse = new JButton("Open File");
btnBrowse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser fs=new JFileChooser(new File("c:\\"));
fs.setDialogTitle("Open a file");
fs.setFileFilter(new FileTypeFilter(".m3u8",""));
fs.setFileFilter(new FileTypeFilter(".ts",""));
fs.setFileFilter(new FileTypeFilter(".m3u",""));
fs.showOpenDialog(null);
File file = fs.getSelectedFile();
textField.setText(file.getAbsolutePath());
}
});
btnBrowse.setBounds(336, 7, 89, 23);
frmHttpsLiveStreaming.getContentPane().add(btnBrowse);
JButton btnNewButton_1 = new JButton("Clear");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField_2.setText("");
}
});
btnNewButton_1.setBounds(237, 39, 89, 23);
frmHttpsLiveStreaming.getContentPane().add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("Save Log File");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser fs=new JFileChooser(new File("c:\\"));
fs.setDialogTitle("Save Log File");
fs.setFileFilter(new FileTypeFilter(".txt","TEXT FILE"));
fs.setFileFilter(new FileTypeFilter(".doc","DOCUMENT FILE"));
int result=fs.showSaveDialog(null);
if (result==JFileChooser.APPROVE_OPTION){
String content=textField_2.getText();
File file=fs.getSelectedFile();
try{
FileWriter fw=new FileWriter(file.getPath());
fw.write(content);
fw.flush();
fw.close();
}catch (Exception e2){
JOptionPane.showMessageDialog(null,e2.getMessage());
}
}
}
});
btnNewButton_2.setBounds(168, 239, 131, 23);
frmHttpsLiveStreaming.getContentPane().add(btnNewButton_2);
textField_2 = new JTextField();
textField_2.setToolTipText("TOP");
textField_2.setBounds(27, 73, 418, 155);
frmHttpsLiveStreaming.getContentPane().add(textField_2);
textField_2.setColumns(10);
JLabel lblUrl = new JLabel("URL");
lblUrl.setBounds(83, 11, 24, 14);
frmHttpsLiveStreaming.getContentPane().add(lblUrl);
textField = new JTextField();
textField.setBounds(116, 11, 210, 19);
frmHttpsLiveStreaming.getContentPane().add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("Check");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
***
try{
BufferedReader br=new BufferedReader(new FileReader(file));
textField_2.read(br,null);
br.close();
textField_2.requestFocus();
}
catch(Exception e){
}
}
});
btnNewButton.setBounds(126, 39, 89, 23);
frmHttpsLiveStreaming.getContentPane().add(btnNewButton);
}
}
请帮忙。
答案 0 :(得分:0)
编译并测试:
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.*;
public class HLS {
private JFrame frmHttpsLiveStreaming;
private JTextField textField_2;
private JTextField textField;
// file is accessed to the whole class
private File file;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
HLS window = new HLS();
window.frmHttpsLiveStreaming.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public HLS() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmHttpsLiveStreaming = new JFrame();
frmHttpsLiveStreaming.setTitle("HTTPS Live Streaming");
frmHttpsLiveStreaming.setBounds(100, 100, 494, 322);
frmHttpsLiveStreaming.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmHttpsLiveStreaming.getContentPane().setLayout(null);
JButton btnBrowse = new JButton("Open File");
btnBrowse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser fs = new JFileChooser(new File("c:\\"));
fs.setDialogTitle("Open a file");
fs.setFileFilter(new FileTypeFilter(".m3u8", ""));
fs.setFileFilter(new FileTypeFilter(".ts", ""));
fs.setFileFilter(new FileTypeFilter(".m3u", ""));
fs.showOpenDialog(null);
file = fs.getSelectedFile();
textField.setText(file.getAbsolutePath());
}
});
btnBrowse.setBounds(336, 7, 89, 23);
frmHttpsLiveStreaming.getContentPane().add(btnBrowse);
JButton btnNewButton_1 = new JButton("Clear");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField_2.setText("");
}
});
btnNewButton_1.setBounds(237, 39, 89, 23);
frmHttpsLiveStreaming.getContentPane().add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("Save Log File");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser fs = new JFileChooser(new File("c:\\"));
fs.setDialogTitle("Save Log File");
fs.setFileFilter(new FileTypeFilter(".txt", "TEXT FILE"));
fs.setFileFilter(new FileTypeFilter(".doc", "DOCUMENT FILE"));
int result = fs.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
String content = textField_2.getText();
File file = fs.getSelectedFile();
try {
FileWriter fw = new FileWriter(file.getPath());
fw.write(content);
fw.flush();
fw.close();
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, e2.getMessage());
}
}
}
});
btnNewButton_2.setBounds(168, 239, 131, 23);
frmHttpsLiveStreaming.getContentPane().add(btnNewButton_2);
textField_2 = new JTextField();
textField_2.setToolTipText("TOP");
textField_2.setBounds(27, 73, 418, 155);
frmHttpsLiveStreaming.getContentPane().add(textField_2);
textField_2.setColumns(10);
JLabel lblUrl = new JLabel("URL");
lblUrl.setBounds(83, 11, 24, 14);
frmHttpsLiveStreaming.getContentPane().add(lblUrl);
textField = new JTextField();
textField.setBounds(116, 11, 210, 19);
frmHttpsLiveStreaming.getContentPane().add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("Check");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// HERE IS THE PROBLEM.
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
// System.out.println(file.toString());
// read text from file until the end. add the content
// to the existing data.
while((line = br.readLine()) != null){
// "\n" will be useful for JTextArea
textField_2.setText(textField_2.getText() + "\n" + line);
//System.out.println(line);
}
//textField_2.read(br, null);
br.close();
//textField_2.requestFocus();
} catch (Exception e) {
}
}
});
btnNewButton.setBounds(126, 39, 89, 23);
frmHttpsLiveStreaming.getContentPane().add(btnNewButton);
}
}