我有一个主框架类和两个面板类。
其中一个面板具有输入字段 第二个面板有按钮
我试图弄清楚如何设置事件处理,以便单击第二个面板中的按钮时,我能够执行需要从第一个面板访问组件(文本字段)的操作。
问题是我无法访问其他类中的组件
代码:
public class MainFrame extends JFrame {
private InputPanel input_panel;
private ButtonPanel button_panel;
public MainFrame() {
setTitle("Shop");
setSize(650,350);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
input_panel = new InputPanel();
button_panel = new ButtonPanel();
add(input_panel, BorderLayout.CENTER);
add(button_panel, BorderLayout.SOUTH);
setVisible(true);
}
}
public class InputPanel extends JPanel {
private static JLabel item_num;
private static JLabel book_id;
private static JLabel quantity;
private static JLabel item_info;
private static JLabel subtotal;
private static JTextField t_item_num;
private static JTextField t_book_id;
private static JTextField t_quantity;
private static JTextField t_item_info;
private static JTextField t_subtotal;
public InputPanel() {
//Layout Manager
FlowLayout flow_input_layout = new FlowLayout(FlowLayout.TRAILING, 75, 10);
//Panel
setLayout(flow_input_layout);
setBackground(Color.yellow);
//Text Fields and Labels
item_num = new JLabel("Enter number of items in this order:");
add(item_num);
t_item_num = new JTextField(20);
add(t_item_num);
book_id = new JLabel("Enter Book ID for Item # 1:");
add(book_id);
t_book_id = new JTextField(20);
add(t_book_id);
quantity = new JLabel("Enter quantity for Item # 1:");
add(quantity);
t_quantity = new JTextField(20);
add(t_quantity);
item_info = new JLabel("Item # 1 Info:");
add(item_info);
t_item_info = new JTextField(20);
add(t_item_info);
subtotal = new JLabel("Order subtotal for X Items:");
add(subtotal);
t_subtotal = new JTextField(20);
add(t_subtotal);
}
}
public class ButtonPanel extends JPanel implements ActionListener {
private static JButton process_btn;
private static JButton confirm_btn;
private static JButton view_btn;
private static JButton finish_btn;
private static JButton new_btn;
private static JButton exit_btn;
public ButtonPanel() {
//Layout Manager
FlowLayout flow_input_layout = new FlowLayout();
//Button Panel
setLayout(flow_input_layout);
setBackground(Color.blue);
//Buttons
process_btn = new JButton("Process Item #1");
process_btn.addActionListener(this);
add(process_btn);
confirm_btn = new JButton("Confirm Item #1");
confirm_btn.addActionListener(this);
add(confirm_btn);
view_btn = new JButton("View Order");
view_btn.addActionListener(this);
add(view_btn);
finish_btn = new JButton("Finish Order");
finish_btn.addActionListener(this);
add(finish_btn);
new_btn = new JButton("New Order");
new_btn.addActionListener(this);
add(new_btn);
exit_btn = new JButton("Exit");
exit_btn.addActionListener(this);
add(exit_btn);
}
@Override
public void actionPerformed(ActionEvent event) {
;
}
}
问题是我无法从其他面板类访问组件。不知道该怎么办
答案 0 :(得分:0)
最简单的方法是将InputPanel添加到ButtonPanel的构造函数中,然后将其存储在变量中。
input_panel = new InputPanel();
button_panel = new ButtonPanel(input_panel);
在ButtonPanel中:
public class ButtonPanel extends JPanel implements ActionListener {
...
private final InputPanel inputPanel;
public ButtonPanel(final InputPanel inputPanel) {
this.inputPanel = inputPanel;
...
}
@Override
public void actionPerformed(ActionEvent event) {
this.inputPanel.getItemNum().doSomething() // you will need to also create accessor methods in the inputPanel
}
顺便说一句,您应该在Java中使用camelCase。