因为是刚学的小菜鸟,所以有很多的不懂,这个也是参考别人的程序做的,但就是有问题,不知道要怎么解决,希望高手帮帮我,谢谢!
源代码:package kj;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2008</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class ks { public static void main(String[] args){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException ce)
{
System.out.println(ce);
}
MenuFrame frame = new MenuFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}}
class MenuFrame extends JFrame{
public JTextArea textarea;
public JTextField text;
public JTextField text1;
public JTextField text2;
String url = "jdbc:odbc:test";
public MenuFrame() {
setTitle("电子词典");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
JMenu fileMenu = new JMenu("文件");
JMenuItem ECItem = fileMenu.add(new TestAction("英汉词典")); JMenuItem CEItem = fileMenu.add(new TestAction("汉英词典"));
JMenuItem BItem = fileMenu.add(new TestAction("备份词库"));
fileMenu.add(new AbstractAction("退出") {
public void actionPerformed(ActionEvent event) {
System.exit(0); }
});
JMenu EditMenu = new JMenu("编辑");
JMenuItem addItem = EditMenu.add(new TestAction("添加词汇"));
JMenuItem updateItem = EditMenu.add(new TestAction("修改词汇"));
JMenuItem deleteItem = EditMenu.add(new TestAction("删除词汇"));
JMenu HelpMenu = new JMenu("帮助");
JMenuItem aboutMenu = HelpMenu.add(new TestAction("关于"));
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(fileMenu);
menuBar.add(EditMenu);
menuBar.add(HelpMenu); JPopupMenu popup = new JPopupMenu(); Action cutAction = new TestAction("剪切");
Action copyAction = new TestAction("复制");
Action pasteAction = new TestAction("剪切");
popup = new JPopupMenu();
popup.add(cutAction);
popup.add(copyAction);
popup.add(pasteAction);
JPanel panel=new JPanel(); add(panel);
panel.addMouseListener(new MouseAdapter() {});
JTextArea textarea= new JTextArea();
textarea.setEditable(false); panel.add(textarea); textarea = new JTextArea();
JLabel Label1= new JLabel("请输入英文:");
JLabel Label2= new JLabel("请输入中文:");
JTextField text = new JTextField(4);
text.setText("");
JTextField text1 = new JTextField(4);
JButton selectButton = new JButton("查询");
selectButton.addActionListener(new ActionListener(){//查询
public void actionPerformed(ActionEvent event){try {
String url = "jdbc:odbc:test";
Connection con = DriverManager.getConnection(url);
String str = "SELECT * FROM dictionary where rtrim(Chinese)='"
+new MenuFrame().text.getText()+"'or rtrim(English)='"+new MenuFrame().text1.getText()+"'"; Statement s = con.createStatement();
ResultSet rs = s.executeQuery(str);
while (rs.next()) {
new MenuFrame().textarea.setText(rs.getString(1) + "\n"+rs.getString(2));
}
rs.close();
s.close();
con.close();
}
catch (SQLException ce)
{
System.out.println(ce);
}
}}); JButton addButton = new JButton("添加");
JButton updateButton = new JButton("修改");
JButton deleteButton = new JButton("删除");
JButton fyButton = new JButton("发音");
JToolBar bar=new JToolBar();
bar.add(Label1);
bar.add(text);
bar.add(Label2);
bar.add(text1);
bar.add(selectButton);
bar.add(fyButton);
bar.add(addButton);
bar.add(updateButton);
bar.add(deleteButton); add(bar,BorderLayout.NORTH); add(textarea,BorderLayout.CENTER);
add(new JScrollPane(textarea));
panel.addMouseListener(new MouseAdapter() {});textarea.setComponentPopupMenu(popup);
} public static final int DEFAULT_WIDTH = 700;
public static final int DEFAULT_HEIGHT = 500;
class TestAction extends AbstractAction {
public TestAction(String name) {
super(name);
};
public void actionPerformed(ActionEvent event) {
System.out.println(getValue(Action.NAME) + "selected.");
}
}
}运行后出现的错误:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at keshe.MenuFrame$3.actionPerformed(ks.java:108)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
源代码:package kj;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2008</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class ks { public static void main(String[] args){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException ce)
{
System.out.println(ce);
}
MenuFrame frame = new MenuFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}}
class MenuFrame extends JFrame{
public JTextArea textarea;
public JTextField text;
public JTextField text1;
public JTextField text2;
String url = "jdbc:odbc:test";
public MenuFrame() {
setTitle("电子词典");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
JMenu fileMenu = new JMenu("文件");
JMenuItem ECItem = fileMenu.add(new TestAction("英汉词典")); JMenuItem CEItem = fileMenu.add(new TestAction("汉英词典"));
JMenuItem BItem = fileMenu.add(new TestAction("备份词库"));
fileMenu.add(new AbstractAction("退出") {
public void actionPerformed(ActionEvent event) {
System.exit(0); }
});
JMenu EditMenu = new JMenu("编辑");
JMenuItem addItem = EditMenu.add(new TestAction("添加词汇"));
JMenuItem updateItem = EditMenu.add(new TestAction("修改词汇"));
JMenuItem deleteItem = EditMenu.add(new TestAction("删除词汇"));
JMenu HelpMenu = new JMenu("帮助");
JMenuItem aboutMenu = HelpMenu.add(new TestAction("关于"));
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(fileMenu);
menuBar.add(EditMenu);
menuBar.add(HelpMenu); JPopupMenu popup = new JPopupMenu(); Action cutAction = new TestAction("剪切");
Action copyAction = new TestAction("复制");
Action pasteAction = new TestAction("剪切");
popup = new JPopupMenu();
popup.add(cutAction);
popup.add(copyAction);
popup.add(pasteAction);
JPanel panel=new JPanel(); add(panel);
panel.addMouseListener(new MouseAdapter() {});
JTextArea textarea= new JTextArea();
textarea.setEditable(false); panel.add(textarea); textarea = new JTextArea();
JLabel Label1= new JLabel("请输入英文:");
JLabel Label2= new JLabel("请输入中文:");
JTextField text = new JTextField(4);
text.setText("");
JTextField text1 = new JTextField(4);
JButton selectButton = new JButton("查询");
selectButton.addActionListener(new ActionListener(){//查询
public void actionPerformed(ActionEvent event){try {
String url = "jdbc:odbc:test";
Connection con = DriverManager.getConnection(url);
String str = "SELECT * FROM dictionary where rtrim(Chinese)='"
+new MenuFrame().text.getText()+"'or rtrim(English)='"+new MenuFrame().text1.getText()+"'"; Statement s = con.createStatement();
ResultSet rs = s.executeQuery(str);
while (rs.next()) {
new MenuFrame().textarea.setText(rs.getString(1) + "\n"+rs.getString(2));
}
rs.close();
s.close();
con.close();
}
catch (SQLException ce)
{
System.out.println(ce);
}
}}); JButton addButton = new JButton("添加");
JButton updateButton = new JButton("修改");
JButton deleteButton = new JButton("删除");
JButton fyButton = new JButton("发音");
JToolBar bar=new JToolBar();
bar.add(Label1);
bar.add(text);
bar.add(Label2);
bar.add(text1);
bar.add(selectButton);
bar.add(fyButton);
bar.add(addButton);
bar.add(updateButton);
bar.add(deleteButton); add(bar,BorderLayout.NORTH); add(textarea,BorderLayout.CENTER);
add(new JScrollPane(textarea));
panel.addMouseListener(new MouseAdapter() {});textarea.setComponentPopupMenu(popup);
} public static final int DEFAULT_WIDTH = 700;
public static final int DEFAULT_HEIGHT = 500;
class TestAction extends AbstractAction {
public TestAction(String name) {
super(name);
};
public void actionPerformed(ActionEvent event) {
System.out.println(getValue(Action.NAME) + "selected.");
}
}
}运行后出现的错误:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at keshe.MenuFrame$3.actionPerformed(ks.java:108)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
楼主截止到2008-06-18 19:54:12的汇总数据:
发帖数:1
结贴数:0
结贴率: 0.00%
如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
你看一下第108行是否有语法或其它错误.
class MenuFrame extends JFrame implements ActionListener{
}
控件是一个对象的属性,不被对象所共享,除非定义为static
public JTextArea textarea;
public JTextField text;
public JTextField text1;
public JTextField text2;
String url = "jdbc:odbc:test";
public MenuFrame() {
setTitle("电子词典");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
JMenu fileMenu = new JMenu("文件");
JMenuItem ECItem = fileMenu.add(new TestAction("英汉词典")); JMenuItem CEItem = fileMenu.add(new TestAction("汉英词典"));
JMenuItem BItem = fileMenu.add(new TestAction("备份词库"));
fileMenu.add(new AbstractAction("退出") {
public void actionPerformed(ActionEvent event) {
System.exit(0); }
});
Debug一下
慢慢看,就知道了!
+new MenuFrame().text.getText()+"'or rtrim(English)='"+new MenuFrame().text1.getText()+"'"; 你这里是什么意思?是要从界面获得你输入的类容嘛?
如果是这个样子,那么,既然要从界面获得,你就不能 new MenuFrame()。这样产生的对象再去.text.getText()肯定是要报空指针异常的。