如何在点击按钮后从一个表单调用另一个表单的小程序。

huangapple 未分类评论44阅读模式
英文:

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.

&lt;html&gt;
&lt;body&gt;
&lt;object  width=&quot;400&quot;  height=&quot;300&quot;&gt;
  &lt;param name=&quot;code&quot; value=&quot;Crapplet.class&quot;&gt;
  Didn&#39;t load applet.
&lt;/object&gt;
&lt;/body&gt;
&lt;/html&gt;

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(&quot;show window&quot;);
        add(button);
        button.addActionListener( evt-&gt;{
            JFrame frame = new JFrame(&quot;what&quot;);
            Extra e = new Extra();
            e.init();
            frame.add(e);
            frame.pack();
            frame.setVisible(true);
        });
    }

}
class Extra extends Applet{
    public void init(){
        add(new JLabel(&quot;about this&quot;));
    }
}

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.

huangapple
  • 本文由 发表于 2020年4月5日 17:00:40
  • 转载请务必保留本文链接:https://java.coder-hub.com/61040208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定