gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:41:13 25 4
gpt4 key购买 nike

我有这个简单的石头、剪刀、布java类,我似乎无法将它转换成小程序。我已经研究这个主题一个多星期了,并且尝试了几个教程等。我似乎错过了一些东西;另外,小程序对我来说是一个全新的概念......

这是我希望将其转换为小程序的 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>

但是,就像我说的,我对此完全陌生,并且我正在尝试......如果有人可以帮助我向我展示将原始代码制作成小程序所需的确切更改,我将不胜感激,并且对我的学习大有帮助......

最佳答案

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

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

您应该将这些顶级容器的管理责任与 UI 的其余部分分开,这样您“隔离”的组件就不会在“窗口”可能不存在时尝试监视它们,例如

它提高了它的可重用性并降低了它的耦合性。

测试面板

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);
}

}

测试小程序

import javax.swing.JApplet;

public class TestApplet extends JApplet {

private TestPane testPane;

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

}

这是(自动生成的)HTML 文件,用于在小程序查看器中显示 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 包中;)

测试框架

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);
}
});
}

}

现在,话虽如此,我强烈建议您不要使用小程序,它们生活在自己的小安全沙箱中,这只会增加开发的复杂性,相反,我会鼓励您看看 Java Web Start

关于java - 如何将一个简单的 GUI java 类转换为 applet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30158584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com