我有一个正在运行的Java应用程序,它随机化一个字符串,每当你点击它按钮时它会显示随机文本。因此,当有人点击按钮时,它会告诉他们他们在游戏中的位置,然后将他们带到一个空白的屏幕,等待下一个玩家点击,等等。它完美无缺,但是当我将代码复制到新文件时类型:
extends JApplet
eclipse中的窗口只是空白,没有任何内容。运行时,我在控制台中没有出现任何错误。我的基本目标是使这个应用程序成为一个java applet,这样我就可以在线发布它,并希望在没有下载的情况下在其他设备上打开它,就像iPhone一样。
代码:
package main;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main extends JApplet{
private static JFrame window = new JFrame();
private static JPanel one = new JPanel();
private static JPanel two = new JPanel();
//1-4 Players
private static JLabel job1 = new JLabel();
private static JLabel job2 = new JLabel();
private static JLabel job3 = new JLabel();
private static JLabel job4 = new JLabel();
private static JLabel jobprint = new JLabel();
private static JButton next = new JButton("Click To Find Your Position");
private static int countertwo = 0;
private static int counter = 0;
public static void shuffleArray(String[] a) {
int n = a.length;
Random random = new Random();
random.nextInt();
for (int i = 0; i < n; i++) {
int change = i + random.nextInt(n - i);
swap(a, i, change);
}
}
private static void swap(String[] a, int i, int change) {
String helper = a[i];
a[i] = a[change];
a[change] = helper;
}
public static void main(String[] args) {
String[] a = new String[] { "Murderer", "Judge", "Innocent", "Innocent"};
shuffleArray(a);
next.setVisible(true);
job1.setVisible(false);
job2.setVisible(false);
job3.setVisible(false);
job4.setVisible(false);
for (String i : a) {
System.out.println(i);
counter++;
if(counter == 1){
job1.setText(i);
}
if(counter == 2){
job2.setText(i);
}
if(counter == 3){
job3.setText(i);
}
if(counter == 4){
job4.setText(i);
}
}
next.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(countertwo == 0){
jobprint.setVisible(true);
jobprint.setText(job1.getText());
next.setText("Click Again");
countertwo++;
}else if(countertwo == 1){
jobprint.setText("");
next.setText("Click To Find Your Position");
countertwo++;
}else if(countertwo == 2){
jobprint.setText(job2.getText());
next.setText("Click Again");
countertwo++;
}else if(countertwo == 3){
jobprint.setText("");
next.setText("Click To Find Your Position");
countertwo++;
}else if(countertwo == 4){
jobprint.setText(job3.getText());
next.setText("Click Again");
countertwo++;
}else if(countertwo == 5){
jobprint.setText("");
next.setText("Click To Find Your Position");
countertwo++;
}else if(countertwo == 6){
jobprint.setText(job4.getText());
countertwo++;
}else if(countertwo == 7){
jobprint.setText("Finished - Application by Fletcher Henneman");
next.setVisible(false);
}
}
});
window.setLayout(new GridLayout(5, 1));
window.setSize(500,400);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
window.add(one);
window.add(two);
one.setLayout(new GridLayout(3, 4));
two.setLayout(new GridLayout(3, 4));
one.add(jobprint);
two.add(next);
window.setVisible(true);
}
}