为什么我按照他所说的编写的与他所编写的源代码不同呢?(100分)

F

fuheli

Unregistered / Unconfirmed
GUEST, unregistred user!
我所编写的源代码如下:
package url;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import com.borland.jbcl.layout.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Frame1 extends JFrame {
JPanel contentPane;
JTextField jTextField1 = new JTextField();
XYLayout xYLayout1 = new XYLayout();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JButton jButton1 = new JButton();
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
jTextField1.setFont(new java.awt.Font("Dialog", 0, 13));
jTextField1.setText("http://www.sina.com.cn:80/index.html");
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
jLabel1.setFont(new java.awt.Font("Dialog", 0, 13));
jLabel1.setText("");
jLabel2.setFont(new java.awt.Font("Dialog", 0, 13));
jLabel2.setText("");
jLabel3.setFont(new java.awt.Font("Dialog", 0, 13));
jLabel3.setText("");
jLabel4.setFont(new java.awt.Font("Dialog", 0, 13));
jLabel4.setText("");
jButton1.setFont(new java.awt.Font("Dialog", 0, 13));
jButton1.setText("查询URL信息");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
contentPane.add(jTextField1, new XYConstraints(6, 12, 386, 28));
contentPane.add(jLabel1, new XYConstraints(15, 55, 361, 31));
contentPane.add(jLabel2, new XYConstraints(16, 94, 361, 31));
contentPane.add(jLabel3, new XYConstraints(16, 136, 361, 31));
contentPane.add(jLabel4, new XYConstraints(17, 180, 361, 31));
contentPane.add(jButton1, new XYConstraints(80, 237, 206, 34));
}
//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 jButton1_actionPerformed(ActionEvent e) {
try{
URL myurl = new URL(jTextField1.getText());
jLabel1.setText("你输入的URL协议为:"+myurl.getProtocol());
jLabel2.setText("你输入的确良URL的主机为:"+myurl.getHost());
jLabel3.setText("你输入的URL文件名为:"+myurl.getFile());
jLabel4.setText("你输入的URL连接的端口号为:"+myurl.getPort());
}
catch(MalformedURLException ex){
ex.printStackTrace();
}
}
}
class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
而书本的源代码为:
package url;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.net.URL;
import java.net.*;
/**
* <p>Title: an example</p>
* <p>Description:an no</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: home</p>
* @author liujun
* @version 1.0
*/
public class Frame1 extends JFrame {
private JPanel contentPane;
private JTextField jTextField1 = new JTextField();
private XYLayout xYLayout1 = new XYLayout();
private JLabel jLabel1 = new JLabel();
private JLabel jLabel2 = new JLabel();
private JLabel jLabel3 = new JLabel();
private JLabel jLabel4 = new JLabel();
private JButton jButton1 = new JButton();
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
jTextField1.setText("http://news.sina.com.cn:80/index.html");
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(387, 226));
this.setTitle("Frame Title");
jLabel1.setText("jLabel1");
jLabel2.setText("jLabel1");
jLabel3.setText("jLabel1");
jLabel4.setText("jLabel1");
jButton1.setText("查询URL信息");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
contentPane.add(jTextField1, new XYConstraints(33, 8, 318, -1));
contentPane.add(jLabel1, new XYConstraints(32, 41, 317, 22));
contentPane.add(jLabel2, new XYConstraints(32, 72, 317, 22));
contentPane.add(jLabel3, new XYConstraints(32, 105, 317, 22));
contentPane.add(jLabel4, new XYConstraints(32, 135, 317, 22));
contentPane.add(jButton1, new XYConstraints(91, 168, 186, 24));
}
//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 jButton1_actionPerformed(ActionEvent e) {
try {
URL myurl=new URL(jTextField1.getText());
jLabel1.setText("您输入的URL的协议为:"+myurl.getProtocol());
jLabel2.setText("您输入的URL的主机为:"+myurl.getHost());
jLabel3.setText("您输入的URL的文件名为:"+myurl.getFile());
jLabel4.setText("您输入的URL连接的端口为:"+myurl.getPort());
}
catch (MalformedURLException ex) {
ex.printStackTrace();
}
}
}
结果我的会多出现rame1_jButton1_actionAdapter,这是为什么呢?
 
L

lnboy

Unregistered / Unconfirmed
GUEST, unregistred user!
跟它 这段代码是等效的。
只是建立了一个内部的class。

--------------------------------------------------------
你的代码
--------------------------------------------------------
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}

-------------------------------------------------------------------
--------------------------------------------------------
别的代码
--------------------------------------------------------

jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
---------------------------------------------------------------------

它们实现的效果是一样的。
 
顶部