2016-12-02 71 views
0

我有一個擴展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 
      } 
     } 

    } 
+0

最好嘗試MVC-ize你的代碼,如果可能的話,從視圖中分離邏輯。此外,你的領域都不應該是「公共」的,實際上所有領域都應該是「私人」的。對象之間的任何通信都應該通過公共方法進行控制。 –

+1

Arggghhh花費太多時間來創建MVC示例。 tdel, - 查看@ trashgod的優秀示例:[here](http://stackoverflow.com/questions/10523343/how-to-wire-one-pane-to-another) –

+1

注意:'Gui'不應該延伸'JFrame','TopPanel'和'CenterPanel'都不應該擴展'JPanel'。在所有三種情況下,代碼應該只使用組件的簡單實例,然後向其中添加其他組件。 –

回答

0

好吧,我想你就遇到了這個問題,因爲你的設置是有缺陷的。我看到的唯一方法是在頂層面板類中創建一個GetSelectedNameBox()方法,並在中心面板類中創建一個設置方法。

所以在你的GUI類,你會做這樣的事情:

字符串溫度= topPanel.getSelectedNamebox();

然後你會做centerPanel.setXXXX(temp);

您可以看到,爲了通過分解來創建更易讀的代碼,可能會使可讀性變差。此外,這不是吸氣和吸氣器的正確使用。

我會重新配置,並將所有不同的JPanel放在同一個類中。我還會專注於爲actionListeners使用匿名類而不是內部類。這會非常縮短你的代碼。並擺脫所有的空白行:P

相關問題