如何从静态类中提取变量

  • 本文关键字:提取 变量 静态类 java
  • 更新时间 :
  • 英文 :


正如标题所说,我想从不同的类中提取一些变量。在课堂上,我的任务是制作一个不是特别难编码的魔方块,所以我想我会制作一个交互式 GUI,因为在我必须学习如何使用 JLabel 和 JButton 等之前,我从未使用 GUI 编码过。

我相信我自己成功地学习了这个,能够想出一个带有不同部分的对话框,打开一个提示菜单,但是我添加了一个确认按钮,我想显示用户输入的内容。我已经使用JOptionPane.showMessageDialog来获取用户输入并将其分配给一个变量,但是我不知道如何将变量从它的类中提取到主变量中,我可以将其用于其他目的。我尝试过使用它:classname.membername,但我无法让它工作。

代码如下:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.*;
public class Magic_SquareR {
    static boolean confirm = false;
    public Magic_SquareR() {
        gui();
    }
        public void gui() {

        JFrame f = new JFrame();
        f.setVisible(true);
        f.setSize(350, 150);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new FlowLayout());
        JPanel p = new JPanel(new GridBagLayout());
        JButton b1 = new JButton("ML");
        JButton b2 = new JButton("TL");
        JButton b3 = new JButton ("BL");
        JButton b4 = new JButton ("TM");
        JButton b5 = new JButton ("MM");
        JButton b6 = new JButton ("BM");
        JButton b7 = new JButton ("TR");
        JButton b8 = new JButton ("MR");
        JButton b9 = new JButton ("BR");
        JButton b10 = new JButton ("Confirm");


        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(5,5,5,5);
        c.gridx = 0;
        c.gridy = 0;
        p.add(b2,c);
        b2.addActionListener(new Action());
        c.gridx = 0;
        c.gridy = 100;
        p.add(b1,c);
        b1.addActionListener(new Action2());

        c.gridx = 0;
        c.gridy = -100;
        p.add(b3,c);
        b3.addActionListener(new Action3());
        c.gridx = 100;
        c.gridy = 000;
        p.add(b4,c);
        b4.addActionListener(new Action4());
        c.gridx = 100;
        c.gridy = -100;
        p.add(b5,c);
        b5.addActionListener(new Action5());
        c.gridx = 100;
        c.gridy = -100;
        p.add(b6,c);
        b6.addActionListener(new Action6());

        c.gridx = -100;
        c.gridy = -100;
        p.add(b7,c);
        b7.addActionListener(new Action7());
        c.gridx = 200;
        c.gridy = -100;
        p.add(b8,c);
        b8.addActionListener(new Action8());
        c.gridx = 200;
        c.gridy = -100;
        p.add(b9,c);
        b9.addActionListener(new Action9());

        c.gridx = 100;
        c.gridy = 300;
        p.add(b10,c);
        b10.addActionListener(new Action10());
        f.add(p);
    }

static class Action implements ActionListener {
        public void actionPerformed (ActionEvent e) {
            String TL = JOptionPane.showInputDialog("Please enter a number from 1-10 for TL");
            int tl = Integer.parseInt(TL);

        }
    }
static class Action2 implements ActionListener {
    public void actionPerformed (ActionEvent e) {
        String ML = JOptionPane.showInputDialog("Please enter a number from 1-10 for ML");
        int ml = Integer.parseInt(ML);
    }
}
static class Action3 implements ActionListener {
    public void actionPerformed (ActionEvent e) {
        String BL = JOptionPane.showInputDialog("Please enter a number from 1-10 for BL");
        int bl = Integer.parseInt(BL);
    }
}
static class Action4 implements ActionListener {
    public void actionPerformed (ActionEvent e) {
        String TM = JOptionPane.showInputDialog("Please enter a number from 1-10 for TM");
        int tm = Integer.parseInt(TM);

    }
}
static class Action5 implements ActionListener {
    public void actionPerformed (ActionEvent e) {
        String MM = JOptionPane.showInputDialog("Please enter a number from 1-10 for MM");
        int mm = Integer.parseInt(MM);

    }
}
static class Action6 implements ActionListener {
    public void actionPerformed (ActionEvent e) {
        String BM = JOptionPane.showInputDialog("Please enter a number from 1-10 for BM");
        int bm = Integer.parseInt(BM);

    }
}
static class Action7 implements ActionListener {
    public void actionPerformed (ActionEvent e) {
        String TR = JOptionPane.showInputDialog("Please enter a number from 1-10 for TR");
        int tr = Integer.parseInt(TR);

    }
}
static class Action8 implements ActionListener {
    public void actionPerformed (ActionEvent e) {
        String MR = JOptionPane.showInputDialog("Please enter a number from 1-10 for MR");
        int mr = Integer.parseInt(MR);

    }
}
public class Action9 implements ActionListener {
    public void actionPerformed (ActionEvent e) {
        String BR = JOptionPane.showInputDialog("Please enter a number from 1-10 for BR");
        int br = Integer.parseInt(BR);

    }

}
public class Action10 implements ActionListener {
    public void actionPerformed (ActionEvent e) {
        JOptionPane.showMessageDialog(null, "Welcome to the end - This is where we check your answers");
        JOptionPane.showMessageDialog(null, "For BR your wrote " + Action9.BR);

    }

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

        new Magic_SquareR();

        {


}

您对当前值所做的只是将它们分配给局部变量(在方法返回后消失(。实现对它们的访问的最简单方法是将它们声明为类中的非静态变量Magic_SquareR。

public class Magic_SquareR {
    static boolean confirm = false;
    static int actionResult1;
    //other results
    public Magic_SquareR() {
        gui();
    }
    public void gui() {
        // ...
    }
    static class Action implements ActionListener {
        public void actionPerformed (ActionEvent e) {
            String TL = JOptionPane.showInputDialog("Please enter a number from 1-10 for TL");
            Magic_SquareR.actionResult1 = Integer.parseInt(TL);
        }
    }
    public static void main(String[] args) {
        new Magic_SquareR();            
        System.out.println(actionResult1);
    }
}

请注意,这确实是实现这一目标的"丑陋"方式(您通常应避免使用静态变量(,但它可以。

如果您希望在单独的文件中声明 Action 类更专业,请为它们提供带有输入值的局部变量,并从 gui(( 方法返回这些值的列表:

public class Action implements ActionListener {
    int actionResult;
    public void actionPerformed (ActionEvent e) {
            String TL = JOptionPane.showInputDialog("Please enter a number from 1-10 for TL");
            actionResult = Integer.parseInt(TL);
    }
{

在 gui(( 中,它看起来像这样:

//return list of input values instead of void
List<Integer> gui() {
    List<Integer> list = new ArrayList();
    //...
    c.gridx = 0;
    c.gridy = 100;
    p.add(b1,c);
    //Assign action to variable so you can access it later
    Action action = new Action();
    b1.addActionListener(action);
    list.add(action.actionResult);
    //...
    return list;
}

最新更新