如何将简单的GUI java类转换为applet?

时间:2015-05-11 01:48:53

标签: java swing applet

我有这个简单的摇滚,剪刀,纸质java类,我似乎无法将其转换为applet。我已经研究了这个主题一个多星期了,我已经尝试了几个教程等等。我似乎错过了一些东西;另外,小程序对我来说是一个全新的概念...

这是我希望转换为applet的TestClass.java的java代码:

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Component;
import java.awt.Container;

public class TestClass extends JFrame implements ActionListener{
    public JLabel myLbl_P1 = new JLabel("enter Player 1 Name:");
    public JTextField myTxtTemp1 = new JTextField(4);
    public JLabel myLbl_P2 = new JLabel("enter Player 2 Name:");
    public JTextField myTxtTemp2 = new JTextField(4);
    public JButton myBtnPlay=new JButton("Play");
    public JLabel myLblResults = new JLabel("                         ");

    public static void main(String args[]) {
        new TestClass();

    }
    TestClass() {
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel p = new JPanel();
        myBtnPlay.addActionListener(this);
        p.add(myLbl_P1);
        p.add(myTxtTemp1);
        p.add(myLbl_P2);
        p.add(myTxtTemp2);
        p.add(myBtnPlay);
        p.add(myLblResults);

        p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
        p.add(myLbl_P1);
        p.add(myTxtTemp1);
        p.add(myLbl_P2);
        p.add(myTxtTemp2);
        p.add(myBtnPlay);
        p.add(myLblResults);

        this.getContentPane().add (p);
        this.setSize(400, 300);

    }

        //------------------------------------------------
            public void actionPerformed(ActionEvent e) {
                String winnerName, P1, P2, winnerStr, myStrResult;
                winnerName="";
                String Pl1=myTxtTemp1.getText();
                String Pl2=myTxtTemp2.getText();
                int winner;
                myStrResult="";

                if(Pl1.isEmpty() || Pl2.isEmpty()){
                    JOptionPane.showMessageDialog(null,"Invalid Player Name","Error",JOptionPane.WARNING_MESSAGE);
                }

                RPSPlayer player1 = new RPSPlayer(Pl1);
                RPSPlayer player2 = new RPSPlayer(Pl2);

                RockPaperScissorsGame x = new RockPaperScissorsGame(player1, player2);

                String[] result=x.rps(player1, player2);
                P1=result[0];
                P2=result[1];
                winnerStr=result[2];
                winner=Integer.valueOf(winnerStr).intValue();

                myStrResult=player1.getName()+" chose "+P1+"   ";
                myStrResult=myStrResult+player2.getName()+" chose "+P2+"   ";

                if(winner==0){
                    myStrResult=myStrResult+"   It was a Draw!";
                }
                else{
                    if(winner==1){
                        winnerName=player1.getName();
                    }
                    if(winner==2){
                        winnerName=player2.getName();
                    }
                    myStrResult=myStrResult+winnerName+" Won!";
                }
                //System.out.println(myStrResult);
                myLblResults.setText(myStrResult);

    }
}

我尝试过改变:

public class TestClass extends JFrame implements ActionListener{   ***to***
public class TestClass extends JApplet implements ActionListener{

public static void main(String args[]) {   ***to***
void init() {

this.getContentPane().add (p);  ***to***
this.add (p);

this.setSize(400, 300);   ***to***
//this.setSize(400, 300);

this.setVisible(true);   ***to***
//this.setVisible(true);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   ***to***
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

我不确定我是否还需要在开头导入任何其他东西 - 也许是导入java.applet。*; ??? 此外,这是我试图加载代码的html页面 - 我认为这是正确的:

<!DOCTYPE html>
<html> 
<head>
    <title></title>
</head>
<body>
    <applet code=applet.class width="400" height="400">
    Your browser does not support the applet tag.
    </applet>
</body>
</html>

但是,就像我说我对这个全新,我正在努力...... 如果有人可以帮我看看我需要将原始代码变成applet的确切更改,我将非常感激并为我的学习做很长的准备......

1 个答案:

答案 0 :(得分:2)

要核心内容,请将其移至JPanel。如果要在窗口中显示它,请使用JFrame,当您想将其显示为小程序时使用JApplet

这就是为什么它通常不鼓励直接从顶级容器扩展(并将核心逻辑直接放在它们上面)。将您的UI分成面板,这些面板可以添加到您想要的容器中。

您应该将这些顶级容器的管理责任与您的UI的其余部分分开,这样您就可以隔离&#34;组件并不试图监控窗户&#34;当它们可能不存在时,例如

它增加了它的可重用性并减少了它的耦合。

TestPane

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TestPane extends JPanel implements ActionListener {
    public JLabel myLbl_P1 = new JLabel("enter Player 1 Name:");
    public JTextField myTxtTemp1 = new JTextField(4);
    public JLabel myLbl_P2 = new JLabel("enter Player 2 Name:");
    public JTextField myTxtTemp2 = new JTextField(4);
    public JButton myBtnPlay = new JButton("Play");
    public JLabel myLblResults = new JLabel("                         ");

    TestPane() {
        myBtnPlay.addActionListener(this);
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        add(myLbl_P1);
        add(myTxtTemp1);
        add(myLbl_P2);
        add(myTxtTemp2);
        add(myBtnPlay);
        add(myLblResults);
    }

    //------------------------------------------------
    @Override
    public void actionPerformed(ActionEvent e) {
        String winnerName;
        String P1;
        String P2;
        String winnerStr;
        String myStrResult;
        winnerName = "";
        String Pl1 = myTxtTemp1.getText();
        String Pl2 = myTxtTemp2.getText();
        int winner;
        myStrResult = "";
        if (Pl1.isEmpty() || Pl2.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Invalid Player Name", "Error", JOptionPane.WARNING_MESSAGE);
        }
        RPSPlayer player1 = new RPSPlayer(Pl1);
        RPSPlayer player2 = new RPSPlayer(Pl2);

        RockPaperScissorsGame x = new RockPaperScissorsGame(player1, player2);

        String[] result = x.rps(player1, player2);
        P1 = result[0];
        P2 = result[1];
        winnerStr = result[2];
        winner = Integer.valueOf(winnerStr).intValue();

        myStrResult = player1.getName() + " chose " + P1 + "   ";
        myStrResult = myStrResult + player2.getName() + " chose " + P2 + "   ";

        if (winner == 0) {
            myStrResult = myStrResult + "   It was a Draw!";
        } else {
            if (winner == 1) {
                winnerName = player1.getName();
            }
            if (winner == 2) {
                winnerName = player2.getName();
            }
            myStrResult = myStrResult + winnerName + " Won!";
        }
        //System.out.println(myStrResult);
        myLblResults.setText(myStrResult);
    }

}

TestApplet

import javax.swing.JApplet;

public class TestApplet extends JApplet {

    private TestPane testPane;

    @Override
    public void init() {
        testPane = new TestPane();
        add(testPane);
    }

}

这是(自动生成的)HTML文件,用于在applet查看器中显示TestApplet

<HTML>
    <HEAD>
        <TITLE>Applet HTML Page</TITLE>
    </HEAD>
    <BODY>

        <H3><HR WIDTH="100%">Applet HTML Page<HR WIDTH="100%"></H3>

        <P>
    <APPLET codebase="classes" code="test/TestApplet.class" width=350 height=200></APPLET>
</P>

<HR WIDTH="100%"><FONT SIZE=-1><I>Generated by NetBeans IDE</I></FONT>
</BODY>
</HTML>

nb 我有test包中的文件;)

TestFrame

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestFrame {

    public static void main(String[] args) {
        new TestFrame();    
    }

    public TestFrame() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

现在,说了这么多,我强烈反对你使用applet,他们生活在他们自己的小安全沙箱中,只会增加开发的复杂性,而是鼓励你看看{{3} }