如何访问一个JPanel组件到另一个JPanel
问题描述:
我有一个扩展JFrame的GUI类。为了代码可读性,我分离了JPanel。我从顶部面板定义了组合框,并且我想将它的选定项目访问到我的中心面板。我的中心面板是可点击的网格面板。我如何从我的BoxListener事件中的组合框访问选定的项目?如何访问一个JPanel组件到另一个JPanel
我的代码放在这里:
//Gui ==================================================
public class Gui extends JFrame {
final int WINDOW_WIDTH = 1000; // Window width in pixels
final int WINDOW_HEIGHT = 800; // Window height in pixels
private TopPanel topPanel;
private CenterPanel centerPanel;
public SchedulerGui() {
// Display the title
setTitle("Class Scheduler");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
// specify action for the close button
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create border layout
setLayout(new BorderLayout());
// create the custom panels;
topPanel = new TopPanel();
centerPanel = new CenterPanel(15,7);
// Add it to the content pane
add(topPanel, BorderLayout.NORTH);
add(centerPanel, BorderLayout.CENTER);
setVisible(true);
}
public static void main(String args[]) {
new Gui();
}
}
//top panel =====================================================
public class TopPanel extends JPanel {
JLabel labelCurrentStatus;
// create combo boxes
public JComboBox nameBox;
String[] listNameBox = { "Select Box”, “Box1”, “Box2”, “Box3”};
String selectedNameBox = "";
public TopPanel() {
nameBox = new JComboBox(listNameBox);
// Register an action listener
nameBox.addActionListener(new ComboBoxListener());
// add the combo boxes into the content pane
add(nameBox);
}
private class ComboBoxListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
selectedNameBox = (String) nameBox.getSelectedItem();
labelCurrentStatus.setText(selectedNameBox);
}
}
}
//center panel ================================================
// creates panel grids that is clickable
public class CenterPanel extends JPanel {
public CenterPanel(int row, int col) {
setLayout(new GridLayout(row, col));
setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
JPanel pan = new JPanel();
pan.setEnabled(true);
pan.setBackground(Color.WHITE);
pan.setPreferredSize(new Dimension(3, 3));
pan.setBorder(BorderFactory.createLineBorder(Color.BLACK));
// an exception to not click the top row and most left column headers
if (i != 0 && j != 0) {
pan.addMouseListener(new BoxListener()); // add a mouse listener to make the panels clickable
}
// set names for each panel for later use
pan.setName("PANEL_" + i + "_" + j);
add(pan);
}
}
}
//Class that defines what happens when a panel is clicked
public static class BoxListener extends MouseAdapter
{
public void mouseClicked(MouseEvent me)
{
JPanel clickedBox =(JPanel)me.getSource();
clickedBox.setBackground(Color.RED);
// insert here the code defining what happens when a grid is clicked.
// Need to access the value of the selected item from the combo box
}
}
}
答
好吧,我想你就遇到了这个问题,因为你的设置是有缺陷的。我看到的唯一方法是在顶层面板类中创建一个GetSelectedNameBox()方法,并在中心面板类中创建一个设置方法。
所以在你的GUI类,你会做这样的事情:
字符串温度= topPanel.getSelectedNamebox();
然后你会做centerPanel.setXXXX(temp);
您可以看到,为了通过分解来创建更易读的代码,可能会使可读性变差。此外,这不是吸气和吸气器的正确使用。
我会重新配置,并将所有不同的JPanel放在同一个类中。我还会专注于为actionListeners使用匿名类而不是内部类。这会非常缩短你的代码。并摆脱所有的空白行:P
最好尝试MVC-ize你的代码,如果可能的话,从视图中分离逻辑。此外,你的领域都不应该是“公共”的,实际上所有领域都应该是“私人”的。对象之间的任何通信都应该通过公共方法进行控制。 –
Arggghhh花费太多时间来创建MVC示例。 tdel, - 查看@ trashgod的优秀示例:[here](http://*.com/questions/10523343/how-to-wire-one-pane-to-another) –
注意:'Gui'不应该延伸'JFrame','TopPanel'和'CenterPanel'都不应该扩展'JPanel'。在所有三种情况下,代码应该只使用组件的简单实例,然后向其中添加其他组件。 –