import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
@SuppressWarnings("serial")
public class readWriteFile extends javax.swing.JFrame {
protected static final String LINE_SEPARATOR = System.getProperty("Line.separator");
private JButton Jbutton1;
private JTextArea JtextArea1;
private JTextField JtextField1;
public static void main(String []args){
readWriteFile rwf=new readWriteFile();
rwf.setLocationRelativeTo(null );
rwf.setVisible(true);
}
public readWriteFile(){
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
initGui();
}
private void initGui() {
// TODO 自动生成的方法存根
{
Jbutton1 = new JButton();
Jbutton1.setBounds(400, 20, 80, 40);
Jbutton1.setText("\u8f6c\u5230");
getContentPane().add(Jbutton1);
Jbutton1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
way_gui();
}
});
}
{
JtextArea1 =new JTextArea();
JScrollPane jSp =new JScrollPane(JtextArea1);
jSp.setBounds(10,60, 470, 300);
getContentPane().add(jSp);
}
{
JtextField1 =new JTextField();
JtextField1.setBounds(10, 20, 380, 40);
getContentPane().add(JtextField1);
JtextField1.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyChar()==KeyEvent.VK_ENTER)
way_gui();
}
});
}
pack();
this.setSize(515,425);
this.setLocationRelativeTo(null);
}
public void way_gui() {
String path=JtextField1.getText();
File f_dir= new File(path);
if ( f_dir.exists() && f_dir.isDirectory()){
JtextArea1.setText("");
String [] names = f_dir.list();
for(String name : names) {
JtextArea1.append(name+LINE_SEPARATOR);
}
}
}
}
为毛不换行,我快疯了
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
@SuppressWarnings("serial")
public class readWriteFile extends javax.swing.JFrame {
protected static final String LINE_SEPARATOR = System.getProperty("Line.separator");
private JButton Jbutton1;
private JTextArea JtextArea1;
private JTextField JtextField1;
public static void main(String []args){
readWriteFile rwf=new readWriteFile();
rwf.setLocationRelativeTo(null );
rwf.setVisible(true);
}
public readWriteFile(){
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
initGui();
}
private void initGui() {
// TODO 自动生成的方法存根
{
Jbutton1 = new JButton();
Jbutton1.setBounds(400, 20, 80, 40);
Jbutton1.setText("\u8f6c\u5230");
getContentPane().add(Jbutton1);
Jbutton1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
way_gui();
}
});
}
{
JtextArea1 =new JTextArea();
JScrollPane jSp =new JScrollPane(JtextArea1);
jSp.setBounds(10,60, 470, 300);
getContentPane().add(jSp);
}
{
JtextField1 =new JTextField();
JtextField1.setBounds(10, 20, 380, 40);
getContentPane().add(JtextField1);
JtextField1.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyChar()==KeyEvent.VK_ENTER)
way_gui();
}
});
}
pack();
this.setSize(515,425);
this.setLocationRelativeTo(null);
}
public void way_gui() {
String path=JtextField1.getText();
File f_dir= new File(path);
if ( f_dir.exists() && f_dir.isDirectory()){
JtextArea1.setText("");
String [] names = f_dir.list();
for(String name : names) {
JtextArea1.append(name+LINE_SEPARATOR);
}
}
}
}
为毛不换行,我快疯了