当前位置: 技术问答>java相关
Applet问题:这是为什么?
来源: 互联网 发布时间:2017-03-29
本文导语: applet原代码: package test; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Button2 extends JApplet { JButton b1=new JButton("Button 1"), b2=new JButton("Button 2"); JTextField txt=new JTextField(10); class BL impl...
applet原代码:
package test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Button2 extends JApplet
{
JButton
b1=new JButton("Button 1"),
b2=new JButton("Button 2");
JTextField txt=new JTextField(10);
class BL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String name=((JButton)e.getSource()).getText();
txt.setText(name);
}
}
BL a1=new BL();
public void init()
{
b1.addActionListener(a1);
b2.addActionListener(a1);
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(b1);
cp.add(b2);
cp.add(txt);
}
}
html文件:
HTML Test Page
untitled1.valia will appear below in a Java enabled browser.
为什么只能看见两个button,而不能看见textfield呢?
package test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Button2 extends JApplet
{
JButton
b1=new JButton("Button 1"),
b2=new JButton("Button 2");
JTextField txt=new JTextField(10);
class BL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String name=((JButton)e.getSource()).getText();
txt.setText(name);
}
}
BL a1=new BL();
public void init()
{
b1.addActionListener(a1);
b2.addActionListener(a1);
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(b1);
cp.add(b2);
cp.add(txt);
}
}
html文件:
HTML Test Page
untitled1.valia will appear below in a Java enabled browser.
为什么只能看见两个button,而不能看见textfield呢?
|
如果不注释掉下边这一句,无法运行;注释掉后,可以看到文本框。
//package test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Button2 extends JApplet
{
JButton
b1=new JButton("Button 1"),
b2=new JButton("Button 2");
JTextField txt=new JTextField(10);
class BL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String name=((JButton)e.getSource()).getText();
txt.setText(name);
}
}
BL a1=new BL();
public void init()
{
b1.addActionListener(a1);
b2.addActionListener(a1);
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(b1);
cp.add(b2);
cp.add(txt);
}
}
//package test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Button2 extends JApplet
{
JButton
b1=new JButton("Button 1"),
b2=new JButton("Button 2");
JTextField txt=new JTextField(10);
class BL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String name=((JButton)e.getSource()).getText();
txt.setText(name);
}
}
BL a1=new BL();
public void init()
{
b1.addActionListener(a1);
b2.addActionListener(a1);
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(b1);
cp.add(b2);
cp.add(txt);
}
}
|
1、我根据你的意思,做了简单的修改,运行正常。
2、你的Html文件写的有问题:
code = "Button2.class"应该为:
code = "test.Button2.class",因为你使用包了!!!
3、文件如下:
//:ButtonTest.java
package test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonTest extends JApplet
{
JButton
b1=new JButton("Button 1"),
b2=new JButton("Button 2");
JTextField txt=new JTextField(10);
class BL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String name=((JButton)e.getSource()).getText();
txt.setText(name);
}
}
BL a1=new BL();
public void init()
{
b1.addActionListener(a1);
b2.addActionListener(a1);
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(b1);
cp.add(b2);
cp.add(txt);
}
}
//web3.html
HTML Test Page
untitled1.valia will appear below in a Java enabled browser.
4、假定文件路径和列表如下:
f:javaweb3.html
f:javaButtonTest.java
5、运行:
javac ButtonTest.java -d .
appletviewer web3.html
2、你的Html文件写的有问题:
code = "Button2.class"应该为:
code = "test.Button2.class",因为你使用包了!!!
3、文件如下:
//:ButtonTest.java
package test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonTest extends JApplet
{
JButton
b1=new JButton("Button 1"),
b2=new JButton("Button 2");
JTextField txt=new JTextField(10);
class BL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String name=((JButton)e.getSource()).getText();
txt.setText(name);
}
}
BL a1=new BL();
public void init()
{
b1.addActionListener(a1);
b2.addActionListener(a1);
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(b1);
cp.add(b2);
cp.add(txt);
}
}
//web3.html
HTML Test Page
untitled1.valia will appear below in a Java enabled browser.
4、假定文件路径和列表如下:
f:javaweb3.html
f:javaButtonTest.java
5、运行:
javac ButtonTest.java -d .
appletviewer web3.html
|
我用的是APPLETVIEWER,另外我的是J2SDK1.4.0_01
|
请注意运行步骤如下:
5、运行:
javac ButtonTest.java -d .
appletviewer web3.html
5、运行:
javac ButtonTest.java -d .
appletviewer web3.html