我声明的类并设置变量。 每次用户激活按钮时,都会设置变量userQuestion。目的是使它可以在下一个类中用来重新解决这个问题。但是,似乎没有设置userQuestion变量。对于凌乱的代码感到抱歉,它是在Eclipse中使用Window Builder制作的。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
public class createprojecte extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JButton btnWhatIsYour;
private JButton btnWhereDidYour;
private JButton btnWhatWasThe;
private JButton btnWhoIsYour;
private JButton btnWhatIsYour_1;
private JButton btnWhatIsYour_2;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
createprojecte frame = new createprojecte();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
})
;
}
String userQuestion;
public createprojecte() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{424, 0};
gbl_contentPane.rowHeights = new int[]{20, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.columnWeights = new double[]{0.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
textField = new JTextField();
textField.setText("Choose what security option you want:");
textField.setColumns(10);
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.insets = new Insets(0, 0, 5, 0);
gbc_textField.anchor = GridBagConstraints.NORTH;
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 0;
gbc_textField.gridy = 0;
contentPane.add(textField, gbc_textField);
btnWhatIsYour = new JButton("What is your mother's maiden name?");
btnWhatIsYour.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
input frame = new input();
frame.setVisible(true);
String userQuestion = "What is your mother's maiden name?";
frame.SetUserQuestion(userQuestion);
}
});
GridBagConstraints gbc_btnWhatIsYour = new GridBagConstraints();
gbc_btnWhatIsYour.insets = new Insets(0, 0, 5, 0);
gbc_btnWhatIsYour.gridx = 0;
gbc_btnWhatIsYour.gridy = 2;
contentPane.add(btnWhatIsYour, gbc_btnWhatIsYour);
btnWhereDidYour = new JButton("Where did your parents meet?");
btnWhereDidYour.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
input frame = new input();
frame.setVisible(true);
String userQuestion = "Where did your parents meet?";
}
});
GridBagConstraints gbc_btnWhereDidYour = new GridBagConstraints();
gbc_btnWhereDidYour.insets = new Insets(0, 0, 5, 0);
gbc_btnWhereDidYour.gridx = 0;
gbc_btnWhereDidYour.gridy = 3;
contentPane.add(btnWhereDidYour, gbc_btnWhereDidYour);
btnWhatWasThe = new JButton("What was the first street you lived on?");
btnWhatWasThe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
input frame = new input();
frame.setVisible(true);
String userQuestion = "What was the first street you lived on?";
}
});
GridBagConstraints gbc_btnWhatWasThe = new GridBagConstraints();
gbc_btnWhatWasThe.insets = new Insets(0, 0, 5, 0);
gbc_btnWhatWasThe.gridx = 0;
gbc_btnWhatWasThe.gridy = 4;
contentPane.add(btnWhatWasThe, gbc_btnWhatWasThe);
btnWhoIsYour = new JButton("Who is your favorite music group?");
btnWhoIsYour.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
input frame = new input();
frame.setVisible(true);
String userQuestion = "Who is your favorite music group?";
}
});
GridBagConstraints gbc_btnWhoIsYour = new GridBagConstraints();
gbc_btnWhoIsYour.insets = new Insets(0, 0, 5, 0);
gbc_btnWhoIsYour.gridx = 0;
gbc_btnWhoIsYour.gridy = 5;
contentPane.add(btnWhoIsYour, gbc_btnWhoIsYour);
btnWhatIsYour_1 = new JButton("What is your favorite book?");
btnWhatIsYour_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
input frame = new input();
frame.setVisible(true);
String userQuestion = "What is your favorite book?";
}
});
GridBagConstraints gbc_btnWhatIsYour_1 = new GridBagConstraints();
gbc_btnWhatIsYour_1.insets = new Insets(0, 0, 5, 0);
gbc_btnWhatIsYour_1.gridx = 0;
gbc_btnWhatIsYour_1.gridy = 6;
contentPane.add(btnWhatIsYour_1, gbc_btnWhatIsYour_1);
btnWhatIsYour_2 = new JButton("What is your favorite sports team?");
btnWhatIsYour_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
input frame = new input();
frame.setVisible(true);
String userQuestion = "What is your favorite sports team?";
}
});
GridBagConstraints gbc_btnWhatIsYour_2 = new GridBagConstraints();
gbc_btnWhatIsYour_2.insets = new Insets(0, 0, 5, 0);
gbc_btnWhatIsYour_2.gridx = 0;
gbc_btnWhatIsYour_2.gridy = 7;
contentPane.add(btnWhatIsYour_2, gbc_btnWhatIsYour_2);
}
}
我尝试访问变量的类。 https://pastebin.com/vb6MWeba
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import javax.swing.JPasswordField;
import java.awt.Insets;
public class input extends JFrame {
private JTextField txtWhatIsThe;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
input frame = new input();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
String userQuestion;
public void SetUserQuestion(String question)
{
this.userQuestion = question;
}
public input() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
getContentPane().setLayout(null);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0};
gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
getContentPane().setLayout(gridBagLayout);
txtWhatIsThe = new JTextField();
txtWhatIsThe.setText(userQuestion );
GridBagConstraints gbc_txtWhatIsThe = new GridBagConstraints();
gbc_txtWhatIsThe.insets = new Insets(0, 0, 5, 0);
gbc_txtWhatIsThe.fill = GridBagConstraints.HORIZONTAL;
gbc_txtWhatIsThe.gridx = 0;
gbc_txtWhatIsThe.gridy = 0;
getContentPane().add(txtWhatIsThe, gbc_txtWhatIsThe);
txtWhatIsThe.setColumns(10);
}
}
答案 0 :(得分:0)
在某些听众中,你并没有真正将问题置于框架中:
btnWhatIsYour_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
input frame = new input();
frame.setVisible(true);
String userQuestion = "What is your favorite book?";
// Missing setter
}
});