当前位置: 技术问答>java相关
帮我看看一面一段程序,有什么错?(我是初学者)
来源: 互联网 发布时间:2015-01-16
本文导语: 这段程序我是想捕捉JTEXTFIELD1的Change事件: package javapro; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import com.borland.jbcl.layout.*; import java.beans.*; import java.io.*; public class MyFrame extends J...
这段程序我是想捕捉JTEXTFIELD1的Change事件:
package javapro;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import com.borland.jbcl.layout.*;
import java.beans.*;
import java.io.*;
public class MyFrame extends JFrame implements Serializable,DocumentListener//注意这里!
{
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JTextField jTextField3 = new JTextField();
JTextField jTextField4 = new JTextField();
JTextField jTextField5 = new JTextField();
JTextField jTextField6 = new JTextField();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JPanel jPanel1 = new JPanel();
JPasswordField jPasswordField1 = new JPasswordField();
/**Construct the frame*/
public MyFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(MyFrame.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(640, 480));
this.setTitle("MyFrame");
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(WindowEvent e) {
this_windowOpened(e);
}
});
jTextField1.setText("jTextField1");
//在下面出错:
jTextField1.getDocument().addDocumentListener(new javax.swing.event.DocumentListener()
{
public void insertUpdate(DocumentEvent e)
{
}
public void removeUpdate(DocumentEvent e)
{
}
public void changedUpdate(DocuementEvent e)
{
}
});
jTextField1.addHierarchyListener(new java.awt.event.HierarchyListener() {
public void hierarchyChanged(HierarchyEvent e) {
jTextField1_hierarchyChanged(e);
}
});
jTextField1.addVetoableChangeListener(new java.beans.VetoableChangeListener() {
public void vetoableChange(PropertyChangeEvent e) throws PropertyVetoException {
jTextField1_vetoableChange(e);
}
});
jTextField1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
jTextField1_propertyChange(e);
}
});
jTextField1.addInputMethodListener(new java.awt.event.InputMethodListener() {
public void inputMethodTextChanged(InputMethodEvent e) {
}
public void caretPositionChanged(InputMethodEvent e) {
jTextField1_caretPositionChanged(e);
}
});
jTextField2.setText("jTextField2");
jTextField3.setText("jTextField3");
jTextField4.setText("jTextField4");
jTextField5.setText("jTextField5");
jTextField6.setText("jTextField6");
jButton1.setText("Close");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jButton2.setText("Init");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
contentPane.addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentShown(ComponentEvent e) {
contentPane_componentShown(e);
}
});
jPasswordField1.setText("jPasswordField1");
contentPane.add(jTextField1, new XYConstraints(87, 47, 122, -1));
contentPane.add(jTextField2, new XYConstraints(91, 89, 115, -1));
contentPane.add(jTextField3, new XYConstraints(251, 48, 120, -1));
contentPane.add(jTextField4, new XYConstraints(250, 92, 123, -1));
contentPane.add(jTextField5, new XYConstraints(419, 49, 122, -1));
contentPane.add(jTextField6, new XYConstraints(418, 93, 127, -1));
contentPane.add(jPanel1, new XYConstraints(329, 60, -1, -1));
contentPane.add(jButton2, new XYConstraints(165, 196, 129, -1));
contentPane.add(jButton1, new XYConstraints(298, 196, 131, -1));
contentPane.add(jPasswordField1, new XYConstraints(90, 127, 115, -1));
}
/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void jTextField1_caretPositionChanged(InputMethodEvent e) {
jTextField2.setText("JTextField1 hava changed by CarePositionChange!");
}
void jButton1_actionPerformed(ActionEvent e) {
}
void jTextField1_propertyChange(PropertyChangeEvent e) {
jTextField2.setText("jTextField1 have changed by ProperChange");
}
void jButton2_actionPerformed(ActionEvent e) {
jTextField2.setText("be Init!");
}
void contentPane_componentShown(ComponentEvent e) {
jTextField2.setText("Panel Init!");
}
void this_windowOpened(WindowEvent e) {
jTextField2.setText("Window Open Event");
}
void jTextField1_vetoableChange(PropertyChangeEvent e) throws PropertyVetoException {
jTextField2.setText("VetoableChange!");
}
void jTextField1_hierarchyChanged(HierarchyEvent e) {
jTextField2.setText("Hierarchy change");
}
private void writeObject(ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject();
}
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
ois.defaultReadObject();
}
}
---------------------------------------------------------
下面的这些不用看它,这个程序我是用JB5编辑的!
package javapro;
import javax.swing.UIManager;
import java.awt.*;
public class MyApplication {
boolean packFrame = false;
/**Construct the application*/
public MyApplication() {
MyFrame frame = new MyFrame();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
/**Main method*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new MyApplication();
}
}
////////////////////////////////////////////////////////////
错误提示:
"MyFrame.java": Error #: 454 : class javapro.MyFrame should be declared abstract; it does not define method changedUpdate(javax.swing.event.DocumentEvent) in interface javax.swing.event.DocumentListener at line 11, column 8
"MyFrame.java": Error #: 300 : class DocuementEvent not found in anonymous class of method jbInit() at line 58, column 37
"MyFrame.java": Error #: 454 : anonymous class of method jbInit() should be declared abstract; it does not define method changedUpdate(javax.swing.event.DocumentEvent) in interface javax.swing.event.DocumentListener at line 51, column 5
package javapro;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import com.borland.jbcl.layout.*;
import java.beans.*;
import java.io.*;
public class MyFrame extends JFrame implements Serializable,DocumentListener//注意这里!
{
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JTextField jTextField3 = new JTextField();
JTextField jTextField4 = new JTextField();
JTextField jTextField5 = new JTextField();
JTextField jTextField6 = new JTextField();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JPanel jPanel1 = new JPanel();
JPasswordField jPasswordField1 = new JPasswordField();
/**Construct the frame*/
public MyFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(MyFrame.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(640, 480));
this.setTitle("MyFrame");
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(WindowEvent e) {
this_windowOpened(e);
}
});
jTextField1.setText("jTextField1");
//在下面出错:
jTextField1.getDocument().addDocumentListener(new javax.swing.event.DocumentListener()
{
public void insertUpdate(DocumentEvent e)
{
}
public void removeUpdate(DocumentEvent e)
{
}
public void changedUpdate(DocuementEvent e)
{
}
});
jTextField1.addHierarchyListener(new java.awt.event.HierarchyListener() {
public void hierarchyChanged(HierarchyEvent e) {
jTextField1_hierarchyChanged(e);
}
});
jTextField1.addVetoableChangeListener(new java.beans.VetoableChangeListener() {
public void vetoableChange(PropertyChangeEvent e) throws PropertyVetoException {
jTextField1_vetoableChange(e);
}
});
jTextField1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
jTextField1_propertyChange(e);
}
});
jTextField1.addInputMethodListener(new java.awt.event.InputMethodListener() {
public void inputMethodTextChanged(InputMethodEvent e) {
}
public void caretPositionChanged(InputMethodEvent e) {
jTextField1_caretPositionChanged(e);
}
});
jTextField2.setText("jTextField2");
jTextField3.setText("jTextField3");
jTextField4.setText("jTextField4");
jTextField5.setText("jTextField5");
jTextField6.setText("jTextField6");
jButton1.setText("Close");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jButton2.setText("Init");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
contentPane.addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentShown(ComponentEvent e) {
contentPane_componentShown(e);
}
});
jPasswordField1.setText("jPasswordField1");
contentPane.add(jTextField1, new XYConstraints(87, 47, 122, -1));
contentPane.add(jTextField2, new XYConstraints(91, 89, 115, -1));
contentPane.add(jTextField3, new XYConstraints(251, 48, 120, -1));
contentPane.add(jTextField4, new XYConstraints(250, 92, 123, -1));
contentPane.add(jTextField5, new XYConstraints(419, 49, 122, -1));
contentPane.add(jTextField6, new XYConstraints(418, 93, 127, -1));
contentPane.add(jPanel1, new XYConstraints(329, 60, -1, -1));
contentPane.add(jButton2, new XYConstraints(165, 196, 129, -1));
contentPane.add(jButton1, new XYConstraints(298, 196, 131, -1));
contentPane.add(jPasswordField1, new XYConstraints(90, 127, 115, -1));
}
/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void jTextField1_caretPositionChanged(InputMethodEvent e) {
jTextField2.setText("JTextField1 hava changed by CarePositionChange!");
}
void jButton1_actionPerformed(ActionEvent e) {
}
void jTextField1_propertyChange(PropertyChangeEvent e) {
jTextField2.setText("jTextField1 have changed by ProperChange");
}
void jButton2_actionPerformed(ActionEvent e) {
jTextField2.setText("be Init!");
}
void contentPane_componentShown(ComponentEvent e) {
jTextField2.setText("Panel Init!");
}
void this_windowOpened(WindowEvent e) {
jTextField2.setText("Window Open Event");
}
void jTextField1_vetoableChange(PropertyChangeEvent e) throws PropertyVetoException {
jTextField2.setText("VetoableChange!");
}
void jTextField1_hierarchyChanged(HierarchyEvent e) {
jTextField2.setText("Hierarchy change");
}
private void writeObject(ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject();
}
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
ois.defaultReadObject();
}
}
---------------------------------------------------------
下面的这些不用看它,这个程序我是用JB5编辑的!
package javapro;
import javax.swing.UIManager;
import java.awt.*;
public class MyApplication {
boolean packFrame = false;
/**Construct the application*/
public MyApplication() {
MyFrame frame = new MyFrame();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
/**Main method*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new MyApplication();
}
}
////////////////////////////////////////////////////////////
错误提示:
"MyFrame.java": Error #: 454 : class javapro.MyFrame should be declared abstract; it does not define method changedUpdate(javax.swing.event.DocumentEvent) in interface javax.swing.event.DocumentListener at line 11, column 8
"MyFrame.java": Error #: 300 : class DocuementEvent not found in anonymous class of method jbInit() at line 58, column 37
"MyFrame.java": Error #: 454 : anonymous class of method jbInit() should be declared abstract; it does not define method changedUpdate(javax.swing.event.DocumentEvent) in interface javax.swing.event.DocumentListener at line 51, column 5
|
public class MyFrame extends JFrame implements Serializable,DocumentListener中,没有写changedUpdate(javax.swing.event.DocumentEvent),当然不成了,你实现了DocumentListener接口的呀,需要重写changeUpdate方法的,以及removeUpdate和insertUpdate方法。
|
你的changeUpdate()方法那边DocumentEvent拼错了,写成docuementEvent了
|
你程序里写的都是匿名类,就没必要implements DocumentListener了,要不你就要在MyFrame里定义
changedUpdate()
insertUpdate()
removeUpdate()
三个方法。
changedUpdate()
insertUpdate()
removeUpdate()
三个方法。