下面这段代码为什么在点击“选择路径”按钮后会报错呢。请哪位朋友帮忙指点一下,十分感谢!
import java.awt.EventQueue;import javax.swing.JFrame;
import java.awt.Font;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JList;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Vector;import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EtchedBorder;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
public class MainFrame extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JTextField FilePath;
private JTextArea textArea_file;
private JTextArea textArea_log;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /**
* Create the frame.
*/
public MainFrame() {
setFont(new Font("宋体", Font.PLAIN, 12));
getContentPane().setFont(new Font("宋体", Font.PLAIN, 12));
JLabel Label1 = new JLabel("文件存放路径:");
FilePath = new JTextField();
JButton Button1 = new JButton("选择路径");
JButton Button2 = new JButton("上传文件");
JTextArea textArea_log = new JTextArea();
textArea_log.setLineWrap(true);
textArea_log.setEditable(false);
textArea_log.setColumns(100);
JButton Button3 = new JButton("退出");
Button1.addActionListener(this);
Button2.addActionListener(this);
Button3.addActionListener(this);
JScrollPane scrollPane = new JScrollPane();
JTextArea textArea_file = new JTextArea();
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(99)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(Label1)
.addGap(18)
.addComponent(FilePath, GroupLayout.PREFERRED_SIZE, 411, GroupLayout.PREFERRED_SIZE)
.addGap(30)
.addComponent(Button1)
.addPreferredGap(ComponentPlacement.UNRELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(textArea_file, GroupLayout.DEFAULT_SIZE, 725, Short.MAX_VALUE))
.addComponent(textArea_log, Alignment.TRAILING, 0, 0, Short.MAX_VALUE))
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(103))
.addGroup(groupLayout.createSequentialGroup()
.addGap(153)
.addComponent(Button2, GroupLayout.PREFERRED_SIZE, 98, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 321, Short.MAX_VALUE)
.addComponent(Button3, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE)
.addGap(153))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(43)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(Label1)
.addComponent(FilePath, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(Button1))
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(25)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(18)
.addComponent(textArea_file, GroupLayout.PREFERRED_SIZE, 242, GroupLayout.PREFERRED_SIZE)))
.addGap(44)
.addComponent(textArea_log, GroupLayout.PREFERRED_SIZE, 240, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(Button2, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
.addComponent(Button3, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
.addGap(29))
);
getContentPane().setLayout(groupLayout);
setBounds(100, 100, 850, 750);
setLocationRelativeTo(null);//使窗口居中
setTitle("上传xml文件");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void select_dir(){
String file_ext;
JFileChooser chooser = new JFileChooser("./");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setVisible(true);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
FilePath.setText(chooser.getSelectedFile().getAbsolutePath());
//列举目录下的文件
/* File[] flist = FileTest.listFile1("d:\\", FileTest.TYPE_FILE);
for (File file : flist) {
file_ext=file.getPath().substring(file.getPath().lastIndexOf(".")).toLowerCase();
if (file_ext.equals(".xml"))
System.out.println(file.getPath());
*/
//textArea_file.append(file.getPath());
textArea_file.append("hello"); }
}
}
@Override
public void actionPerformed(ActionEvent e) {
// 判断是哪个按钮的动作filelist
if (e.getActionCommand().equals("选择路径"))
{ // 选择路径
select_dir();
//textArea_file.append("hello");
}
else if (e.getActionCommand().equals("退出"))
{
System.exit(0);
}
}
}
import java.awt.EventQueue;import javax.swing.JFrame;
import java.awt.Font;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JList;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Vector;import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EtchedBorder;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
public class MainFrame extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JTextField FilePath;
private JTextArea textArea_file;
private JTextArea textArea_log;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /**
* Create the frame.
*/
public MainFrame() {
setFont(new Font("宋体", Font.PLAIN, 12));
getContentPane().setFont(new Font("宋体", Font.PLAIN, 12));
JLabel Label1 = new JLabel("文件存放路径:");
FilePath = new JTextField();
JButton Button1 = new JButton("选择路径");
JButton Button2 = new JButton("上传文件");
JTextArea textArea_log = new JTextArea();
textArea_log.setLineWrap(true);
textArea_log.setEditable(false);
textArea_log.setColumns(100);
JButton Button3 = new JButton("退出");
Button1.addActionListener(this);
Button2.addActionListener(this);
Button3.addActionListener(this);
JScrollPane scrollPane = new JScrollPane();
JTextArea textArea_file = new JTextArea();
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(99)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(Label1)
.addGap(18)
.addComponent(FilePath, GroupLayout.PREFERRED_SIZE, 411, GroupLayout.PREFERRED_SIZE)
.addGap(30)
.addComponent(Button1)
.addPreferredGap(ComponentPlacement.UNRELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(textArea_file, GroupLayout.DEFAULT_SIZE, 725, Short.MAX_VALUE))
.addComponent(textArea_log, Alignment.TRAILING, 0, 0, Short.MAX_VALUE))
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(103))
.addGroup(groupLayout.createSequentialGroup()
.addGap(153)
.addComponent(Button2, GroupLayout.PREFERRED_SIZE, 98, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 321, Short.MAX_VALUE)
.addComponent(Button3, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE)
.addGap(153))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(43)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(Label1)
.addComponent(FilePath, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(Button1))
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(25)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(18)
.addComponent(textArea_file, GroupLayout.PREFERRED_SIZE, 242, GroupLayout.PREFERRED_SIZE)))
.addGap(44)
.addComponent(textArea_log, GroupLayout.PREFERRED_SIZE, 240, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(Button2, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
.addComponent(Button3, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
.addGap(29))
);
getContentPane().setLayout(groupLayout);
setBounds(100, 100, 850, 750);
setLocationRelativeTo(null);//使窗口居中
setTitle("上传xml文件");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void select_dir(){
String file_ext;
JFileChooser chooser = new JFileChooser("./");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setVisible(true);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
FilePath.setText(chooser.getSelectedFile().getAbsolutePath());
//列举目录下的文件
/* File[] flist = FileTest.listFile1("d:\\", FileTest.TYPE_FILE);
for (File file : flist) {
file_ext=file.getPath().substring(file.getPath().lastIndexOf(".")).toLowerCase();
if (file_ext.equals(".xml"))
System.out.println(file.getPath());
*/
//textArea_file.append(file.getPath());
textArea_file.append("hello"); }
}
}
@Override
public void actionPerformed(ActionEvent e) {
// 判断是哪个按钮的动作filelist
if (e.getActionCommand().equals("选择路径"))
{ // 选择路径
select_dir();
//textArea_file.append("hello");
}
else if (e.getActionCommand().equals("退出"))
{
System.exit(0);
}
}
}
解决方案 »
- lava poi处理word文档的问题??
- 酷狗
- JSP读数据和取数据问题
- 急急,我是新手,问下quartz中的JobDataMap问题。。。
- tomcat报错:Servlet.service() for servlet jsp threw exception
- struts1.3里的validate plugin怎么实现???急死我了快。。。。
- spring+hibernate错误,请高手进指点
- 如何在Struts单个页面中多个按钮实现多个功能
- 修饰浮的private和protected一起用的问题
- 各位java高手,我有一个简单问题请教?
- 奇怪的乱码?
- 求最新,最清的Spring电子书,PDF格式的。
textArea_file.append("hello");
JTextArea textArea_file = new JTextArea();
这不算创建吗?报错的内容还真看不出来错在哪里。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
请哪位做GUI的兄弟帮帮忙
中的
JTextArea去掉,试看看,还会出现这种异常?