[英]How to access components of one JPanel to another JPanel
我有一個擴展JFrame的gui類。 我分離了JPanels以提高代碼的可讀性。 我從頂部面板定義了組合框,我想在中央面板中訪問它的選定項。 我的中心面板是一個可單擊的網格面板。 如何從BoxListener事件中的組合框訪問選定的項目?
我的代碼在這里:
//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()方法,並在“中心”面板類中創建一個set方法。
因此,在您的Gui課堂中,您將執行以下操作:
字符串temp = topPanel.getSelectedNamebox();
然后您將要做centerPanel.setXXXX(temp);
您會看到,為了通過拆分內容來創建更具可讀性的代碼,可能會使可讀性變差。 此外,這不是對getter和setter的正確使用。
我將重新配置,並將所有不同的JPanels放在同一類中。 我還將重點介紹為您的actionListeners使用匿名類,而不是內部類。 這將大大縮短您的代碼。 並擺脫所有空白行:P
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.