正如标题所说,我想从不同的类中提取一些变量。在课堂上我的任务是制作一个不太难编码的魔方,所以我想我会制作一个交互式GUI,因为在我不得不学习如何使用JLabel和JButton等之前我从未用GUI编码。 / p>
我相信我已经成功了解这个我自己能够拿出一个带有不同部分的对话框打开一个提示菜单但是我已经添加了一个确认按钮我希望显示用户有什么进入。我已经使用了JOptionPane.showMessageDialog来获取用户输入并将其分配给变量但是我不知道如何将变量从它的类中拉出到我可以将其用于其他目的的main中。我试过使用这个: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();
{
}
答案 0 :(得分:0)
您对当前值所做的只是将它们分配给局部变量(在方法返回后消失)。实现对它们的访问的最简单方法是将它们声明为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;
}