带有hello的paint类是我想要使用的主要变量。我该怎么做?我想移动变量,这样我就可以在猜出正确或不正确的字母时画出字符串条件。
import static java.lang.System.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.applet.*;
import static java.lang.System.*;
public class testing extends Applet
{
public static void main(String[]args)throws IOException
{
JFrame jp1 = new JFrame();
testing a =new testing ();
jp1.getContentPane().add(a, BorderLayout.CENTER);
jp1.setSize(new Dimension(500,500));
jp1.setVisible(false);
Random random = new Random();
Scanner scanner = new Scanner(new File("C:\\Users\\s612153\\Documents\\Java files\\words.data"));
ArrayList <String> arraylist = new ArrayList<String>();
ArrayList <String> arrayinco = new ArrayList<String>();
ArrayList <String> arraycorr = new ArrayList<String>();
int c = scanner.nextInt();
int variable = random.nextInt(c) + 1;
for(int x = 0; x < c; x++)
{
String string = scanner.next();
arraylist.add(string);
}
String word = arraylist.get(variable);
word = word.toUpperCase();
out.println(word);
int length = word.length();
ArrayList <String> arrayundr = new ArrayList<String>();
for(int x = 0; x < length; x++)
{
arrayundr.add("_ ");
}
out.println(arrayundr);
int attempts = 7;
int var = 0;
for(int x = var; x < attempts; x = x)
{
String guess = JOptionPane.showInputDialog("Enter a letter: ");
char Char = guess.charAt(0);
Char = Character.toUpperCase(Char);
if(word.indexOf(Char) >= 0)
{
String let = Character.toString(Char);
int gerb = word.indexOf(Char);
int kirby = word.indexOf(Char, gerb + 1);
out.println("That is correct");
arraycorr.add(let);
arrayundr.set(gerb, let);
out.println("WORD: " + arrayundr);
attempts = attempts + 1;
}
else if(word.indexOf(Char) != 0)
{
String bet = Character.toString(Char);
out.println("incorrect!");
arrayinco.add(bet);
x = x + 1;
}
}
out.println(arrayinco);
}
我想使用我的主要变量的paint类!!!
public void paint (Graphics g)
{
g.drawString("hello",40,30);
}
}
答案 0 :(得分:0)
更好的方法是使字符串成为测试类的属性,然后可以使用applet上的句柄设置它,例如:
public class testing extends Applet {
private String message = "";
public static void main(String[]args)throws IOException
{
JFrame jp1 = new JFrame();
testing a =new testing ();
// ..
a.message = "Hello";
// ..
}
public void paint (Graphics g) {
g.drawString(message,40,30);
}
}