从Java中的另一个类访问字符串

时间:2015-10-07 09:51:10

标签: java string class variables

这与通常的问题略有不同。我知道如果我可以将字符串设置为public static String passcode;,我会怎么做,但问题是我做不到。我需要一个解决方案,我想在这里实现:

public class EncryptionSubtask {


public static void main(String[] args) {
    Main m2 = new Main();
    m2.setVisible(true);
    m2.setResizable(true);
    m2.setLayout(new FlowLayout());
}

public EncryptionSubtask() {
    log("Running Substask");
    Random rand = new Random();
    int n1 = 33 + rand.nextInt(94);
    int n2 = 33 + rand.nextInt(94);
    int n3 = 33 + rand.nextInt(94);
    int n4 = 33 + rand.nextInt(94);
    int n5 = 33 + rand.nextInt(94);
    int n6 = 33 + rand.nextInt(94);
    int n7 = 33 + rand.nextInt(94);
    int n8 = 33 + rand.nextInt(94);

    String c1 = Character.toString ((char) n1);
    String c2 = Character.toString ((char) n2);
    String c3 = Character.toString ((char) n3);
    String c4 = Character.toString ((char) n4);
    String c5 = Character.toString ((char) n5);
    String c6 = Character.toString ((char) n6);
    String c7 = Character.toString ((char) n7);
    String c8 = Character.toString ((char) n8);


    String passcode = (c1+c2+c3+c4+c5+c6+c7+c8);

    System.out.println(passcode);

}

我想在这里获得“密码”:

public class Main extends Frame implements Runnable, KeyListener, MouseListener, WindowListener {

private static final long serialVersionUID = 1L;
Thread thread;
BufferedImage backbuffer;
Graphics2D g2d;
AffineTransform transform = new AffineTransform();

enum Modes {
    Menu, Encrypt, Decrypt
}

Modes mode = Modes.Menu;

public Image background;

public static void main(String[] args) {
    Main m = new Main();
    m.setVisible(true);
    m.setResizable(true);
    m.setLayout(new FlowLayout());
}

@Override
public void windowActivated(WindowEvent e) {
}

@Override
public void windowClosed(WindowEvent e) {
}

@Override
public void windowClosing(WindowEvent e) {
    dispose();
    log("Exiting...");
    System.exit(0);
}

@Override
public void windowDeactivated(WindowEvent e) {
}

@Override
public void windowDeiconified(WindowEvent e) {
}

@Override
public void windowIconified(WindowEvent e) {
}

@Override
public void windowOpened(WindowEvent e) {
}

@Override
public void mouseClicked(MouseEvent e) {
    int x = e.getX() - 8;
    int y = e.getY() - 30;
    // System.out.println(y);
    if (x >= 50 && x <= 143 && y >= 50 && y <= 75) { //if statement for encrypt button bounds
        log("Button Pressed. Encrypt Screen Opened.");
        // Frame f = new Frame();
        // f.setVisible(true);
        // f.setSize(100, 100);
        mode = Modes.Encrypt;
        new EncryptionSubtask();
    }
    if (x >= 500 && x <= 595 && y >= 50 && y <= 75) { // if statement for decrypt button bounds
        log("Button Pressed. Decrypt Screen Opened.");
        mode = Modes.Decrypt;
    }
}

@Override
public void mouseEntered(MouseEvent e) {
}

@Override
public void mouseExited(MouseEvent e) {
}

@Override
public void mousePressed(MouseEvent e) {
}

@Override
public void mouseReleased(MouseEvent e) {
}

@Override
public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    System.out.println("Key pressed = " + key);
    if (key == 27) {
        mode = Modes.Menu;
    }
}

@Override
public void keyReleased(KeyEvent e) {
}

@Override
public void keyTyped(KeyEvent e) {
}

public enum LogLevel {
    Info, Warn
}

private static int width = 300;
private static int height = width / 16 * 9;
private static int scale = 3;

public Main() {
    addWindowListener(this);
    log("Initialising...");
    try {
        TimeUnit.SECONDS.sleep(1);
    } catch (InterruptedException e1) {
        e1.printStackTrace();
    }
    this.start();
    log("Setting title...");
    try {
        TimeUnit.SECONDS.sleep(1);
    } catch (InterruptedException e1) {
        e1.printStackTrace();
    }
    this.setTitle("LOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOL");
    log("Creating backbuffer for window...");
    setSize(width * scale, height * scale);
    backbuffer = new BufferedImage(640 * 3, 480 * 2, BufferedImage.TYPE_INT_RGB);
    log("Buffering Image, standby...");
    try {
        TimeUnit.SECONDS.sleep(1);
    } catch (InterruptedException e1) {
        e1.printStackTrace();
    }
    g2d = backbuffer.createGraphics();

    File fileThis = new File(System.getProperty("java.class.path"));
    File parentFile = fileThis.getAbsoluteFile().getParentFile(); // the
                                                                    // project
                                                                    // folder
    log(parentFile.getAbsolutePath());
    File resources = new File(parentFile.toString() + "/resources/");
    log(resources.getAbsolutePath());
    try {
        background = ImageIO.read(new File(resources + "/guitar.jpg")); // CHANGE
                                                                        // THIS
                                                                        // IMAGE!
        File tempFile = new File(resources + "/guitar.jpg");
        log(tempFile.getAbsolutePath());
    } catch (IOException e) {
        e.printStackTrace();
    }



    addKeyListener(this);
    addMouseListener(this);
}

@Override
public void run() {
    Thread t = Thread.currentThread();

    while (t == thread) {
        try {
            // log("Running...");
            Thread.sleep(20);
        } catch (Exception e) {
            e.printStackTrace();
        }
        repaint();
    }
}

public void start() {
    thread = new Thread(this);
    thread.start();
}

public void stop() {
    try {
        thread.join();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void update(Graphics g) {
    g2d.setTransform(transform);
    g2d.setPaint(Color.black);
    g2d.fillRect(0, 0, getWidth(), getHeight());
    switch (mode) {
    case Menu:
        g2d.drawImage(background, 0, 0, getWidth(), getHeight(), this);
        g2d.setPaint(Color.blue);
        g2d.fillRect(50, 50, 93, 25);
        g2d.fillRect(500, 50, 95, 25);
        g2d.setPaint(Color.red);
        g2d.setFont(new Font("TimesRoman", Font.PLAIN, 30));
        g2d.drawString("Encrypt", 50, 70);
        g2d.drawString("Decrypt", 500, 70);
        break;
    case Encrypt:
        g2d.setPaint(Color.green);
        g2d.drawString("Encrypt screen", 50, 70);
        break;
    case Decrypt:
        g2d.setPaint(Color.green);
        g2d.drawString("Decrypt Screen", 50, 70);
        break;
    }

    paint(g);
}

public void log(String s) {
    log(s, 0, 0);
}

public void log(String s, int i) {
    log(s, i, 0);
}

public void log(String s, int i, int j) {
    String logmsg = "";
    for (int k = 0; k < i; k++) {
        logmsg += "  ";
    }
    if (j == 0) {
        logmsg = "[Info]" + logmsg;
    } else {
        logmsg = "[Warn]" + logmsg;
    }
    System.out.println("[" + System.currentTimeMillis() + "]["
            + Thread.currentThread().getStackTrace()[1].toString() + "]" + logmsg + s);
}

public void paint(Graphics g) {
    g.drawImage(backbuffer, 8, 31, this);
}

}

这样我就可以用它来画我的画架等。

2 个答案:

答案 0 :(得分:1)

好的,你在mouseClicked方法中调用了EncryptionSubtask的构造函数,你什么都不做。

首先,您需要将其分配给变量,如下所示:

EncryptionSubtask veryStrangeTask = new EncryptionSubtask();

将局部变量passcode作为类字段,如下所示:

public class EncryptionSubtask {
String passcode;
...
}

现在您可以在&#34;其他&#34;中使用此字段上课

veryStrangeTask.passcode;

答案 1 :(得分:1)

也许是观察者?

您可以创建一个具有传递密码的方法的界面

public interface PasscodeNotify{
    public void notifyPasscode(String passcode);
}

然后修改EncryptionSubtask构造函数以接受此类的实例,这样:

public EncryptionSubtask(PasscodeNotify observer) {
    ...
    if(observer != null)
        observer.notifyPasscode(c1+c2+c3+c4+c5+c6+c7+c8);
}

最后你让你的Main类实现了另一个接口,我刚创建的PasscodeNotify,并实现了检索密码的方法。

它是否符合您的目的?

希望它有所帮助!