英文:
how to call one applet form to another form after clicking on button
问题
我在小程序中创建了一个按钮,并希望当我点击此按钮时打开另一个小程序窗口。请解释我该如何做到这一点。
英文:
I have created a Button in an applet and want another applet window to be opened when I click this button.
Please explain how can I do that.
答案1
得分: 0
再次强调,我不建议使用小程序。我安装的 JDK 之一带有小程序查看器,所以我创建了这个示例。它有一个按钮。您点击按钮,它会打开另一个窗口。我使用以下命令运行此程序:
`$appletviewer crapplet.html`。
首先是用于显示小程序的 `crapplet.html` 文件。
<html>
<body>
<object width="400" height="300">
<param name="code" value="Crapplet.class">
未加载小程序。
</object>
</body>
</html>
其次是小程序源代码,`Crapplet.java`。
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.applet.Applet;
public class Crapplet extends Applet{
public void init(){
JButton button = new JButton("显示窗口");
add(button);
button.addActionListener( evt->{
JFrame frame = new JFrame("什么");
Extra e = new Extra();
e.init();
frame.add(e);
frame.pack();
frame.setVisible(true);
});
}
}
class Extra extends Applet{
public void init(){
add(new JLabel("关于此事"));
}
}
按钮被点击后,会触发动作事件并调用监听器。在监听器内部,会创建一个 JFrame,并创建下一个 Applet 类。小程序被初始化并添加到 JFrame 中,然后显示出来。
我之所以这样做,是因为它与 Swing 相差不远。您完全可以用 JFrame 和 main 方法完全替代小程序。
英文:
Again, I do not recommend using applets. One of the jdk's I have installed has an applet viewer, so I whipped up this example. It has a button. You click the button, it opens another window. I run this program with:
$appletviewer crapplet.html
.
First the crapplet.html
file that is used to display the applet.
<html>
<body>
<object width="400" height="300">
<param name="code" value="Crapplet.class">
Didn't load applet.
</object>
</body>
</html>
Second the applet source, Crapplet.java
.
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.applet.Applet;
public class Crapplet extends Applet{
public void init(){
JButton button = new JButton("show window");
add(button);
button.addActionListener( evt->{
JFrame frame = new JFrame("what");
Extra e = new Extra();
e.init();
frame.add(e);
frame.pack();
frame.setVisible(true);
});
}
}
class Extra extends Applet{
public void init(){
add(new JLabel("about this"));
}
}
One the button is clicked, it fire of the action event and calls the listener. Inside of the listener a JFrame is created and the next Applet class is created. The applet is init and added to the JFrame then displayed.
The only reason I did this is because it is not too far from swing. You can completely replace the Applet with a JFrame and a main method.
专注分享java语言的经验与见解,让所有开发者获益!
评论