将类作为JApplet或JFrame运行

  • 本文关键字:JFrame 运行 JApplet java
  • 更新时间 :
  • 英文 :


如果我想创建一个既可以作为JApplet又可以作为JFrame运行的类,我认为这个类所要做的就是扩展JApplet。我之前在一篇帖子中问过这个问题,但我不知道如何将这个问题扩展到那个问题。我在这段代码中包含了一个小程序的生命周期,以及主要方法。

import java.awt.*;
import java.applet.Applet;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class LifeCycle extends Applet
{
    private static final long serialVersionUID = 1L;
    String output = "test";
    String event;
    public void init()
    {
                gui(); //I am not certain if this needs to be there. 
        event = "nInitializing...";
        printOutput();
    }
    public void start()
    {
        event = "nStarting..."; 
        printOutput();
    }
    public void stop()
    {
        event = "nStopping...";
        printOutput();
    }
    public void destroy()
    {
        event = "nDestroying...";
        printOutput();
    }
    private void printOutput()
    {
        System.out.println(event);
        output += event;
        repaint();
    }
    private void gui() {
        JFrame f = new JFrame("Not resizable");
        JPanel d = new JPanel();
        d.setBackground(Color.BLACK);
        f.add(d);
        f.setSize(745,440); 
        f.setResizable(false);
        f.setLocationRelativeTo(null);
        f.setTitle("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void paint(Graphics g)
    {
        System.out.println("Graphics Paint Method!");
        g.drawString(output, 100, 100);
    }
    public static void main(String[] args) {
        LifeCycle l = new LifeCycle();
        l.gui();
    }
}

如果您需要可以在JFrameJApplet中运行的东西,请将所有逻辑和东西放入JPanel中,并根据需要生成JFrameJApplet,然后将JPanel添加到其中。

编辑所有这些生命周期的东西,你可以让小程序或框架在你的面板上根据需要调用它们。

编辑:示例

public class MyPanel extends JPanel {
    public MyPanel()
    {
        setBackground(Color.BLACK);
        setForeground(Color.WHITE);
    }
    String output = "test";
    String event;
    public void init()
    {
        event = "nInitializing...";
        printOutput();
    }
    // Put all the other methods in here too
    private void printOutput()
    {
        System.out.println(event);
        output += event;
        repaint();
    }
    @Override
    public void paintComponent(Graphics g)
    {
        System.out.println("Graphics Paint Method!");
        g.setColor(this.getBackground());
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        g.setColor(this.getForeground());
        g.drawString(output, 100, 100);
    }
}
public class MyFrame extends JFrame {
    MyPanel myPanel;
    public MyFrame()
    {
        super("Test frame");
        setSize(745,440); 
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myPanel=new MyPanel();
        add(myPanel,BorderLayout.CENTER);
    }
    public static void main(String[] args)
    {
        MyFrame f = new MyFrame();
        f.setVisible(true);
    }
}
public class MyApplet extends JApplet {
    private static final long serialVersionUID = 1L;
    MyPanel myPanel;
    @Override
    public void init()
    {
        myPanel = new MyPanel();
        getContentPane().add(myPanel,BorderLayout.CENTER);
    }
}

然后向JFrameJApplet添加方法,以调用myPanel 上的相关方法

对于JFrame,我推荐WindowListener等等。对于JApplet,您可以只使用init、start、destroy和stop方法来调用面板上的这些方法。

对于WindowListener或更方便的WindowAdapter,此链接似乎是一个很好的参考:http://way2java.com/awt-components/java-frame-closing-windowadapter/

只需将小程序add() JFrame,然后在适当的事件上调用小程序的生命周期方法(构造后为init(),框架关闭时为destroy()等)

编辑:是的,对gui()的调用应该在那里,而不是在主中

最新更新