我正在尝试将餐馆类中的表号传递给Menu页面类中的tableLabel。当代码运行时,tableLabel返回null。任何帮助都可以在运行时启用代码以返回tableLabel中的数字。
从餐厅课程中提取
public class Restaurant extends JFrame {
private JPanel contentPane;
private JTextField restaurant_Txt;
private JTextField num_Diners;
private JTextField num_Diners_Txt;
private JTextField table_Num_Txt;
private JTextField num_Table;
private JButton num_TableSub_Btn;
private JButton proceed_Menu_Btn;
private JButton MyDocumentListener;
MenuPage parent;
public static String tableNumber;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Restaurant frame = new Restaurant();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Restaurant() {
super("Restaurant");
parent = new MenuPage();
initGUI();
num_Table = new JTextField("NewUser", 10);
}
public void initGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 640, 310);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
table_Num_Txt = new JTextField();
table_Num_Txt.setEditable(false);
table_Num_Txt.setText("Table number ?");
table_Num_Txt.setBounds(145, 164, 112, 26);
contentPane.add(table_Num_Txt);
table_Num_Txt.setColumns(10);
num_Table = new JTextField("");
num_Table.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
if (e.getKeyCode() == 10) {
tableNumber = num_Table.getText();
}
}
});
num_Table.setBounds(334, 161, 83, 26);
contentPane.add(num_Table);
num_Table.setColumns(10);
}
}
从菜单页面类提取
tableLabel = new JLabel(" : " + Restaurant.tableNumber);
tableLabel.setBounds(16, 6, 61, 16);
contentPane.add(tableLabel);
答案 0 :(得分:0)
这可能是一个愚蠢的问题,但您确定在GUI内部的num_Table字段内按Enter键,因为在发生这种情况时设置变量。你也应该通过构造函数将它传递给MainPage,而不是作为静态变量。