我试图添加一个水平和垂直滚动条,以在打开整个文本文件后读取它。因为我的结构方式仅部分显示了文件。请参见下面的图像和代码。我做错了什么吗?更新的代码
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);
}
}
我不知道为什么它如图所示显示。请告知。
答案 0 :(得分:0)
在初始化JTextArea对象时,请考虑将行数和列数作为构造函数参数。
答案 1 :(得分:0)
您必须使用
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
}
});
}
}