2016-12-02 6 views
0

私はJFrameを拡張するguiクラスを持っています。私はコードの読みやすさのためにJPanelsを分離しました。私はトップパネルからコンボボックスを定義し、それを私のセンターパネルに選択されたアイテムにアクセスしたいと思います。私のセンターパネルは、クリック可能なグリッドパネルです。 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化しようとすると、可能であればロジックをビューから分離するのが最適です。また、あなたのフィールドは 'public'でなくてはなりません。実際には全て' private'でなければなりません。オブジェクト間の通信は、公開された方法で制御された方法で行われるべきです。 –

+1

ArggghhhがMVCの例を作成するのに時間がかかりすぎる。 tdel、@ trashgodの優れた例をチェックしてください:[ここ](http://stackoverflow.com/questions/10523343/how-to-wire-one-pane-to-another) –

+1

注: 'Gui'は拡張しないでください'JFrame'であり、' TopPanel'と 'CenterPanel'のどちらも' JPanel'を拡張してはいけません。 3つのケースすべてで、コードはコンポーネントのプレーンなインスタンスを使用し、それに他のコンポーネントを追加するだけです。 –

答えて

0

まあ、私はあなたのセットアップは欠陥があるので、あなたがこの問題に遭遇したと思います。私がこれを見る唯一の方法は、TopパネルクラスでGetSelectedNameBox()メソッドを作成し、Centerパネルクラスでsetメソッドを作成することです。

だからあなたグイクラスであなたのようなものだろう:

文字列の一時を= topPanel.getSelectedNamebox();

次に、centerPanel.setXXXX(temp)を実行します。

分かりやすいコードを作成するために、読みやすさが悪化する可能性があることがわかります。さらに、これはgetterとsetterの適切な使用ではありません。

私は再構成し、すべての異なるJPanelを同じクラスに入れます。私はあなたのactionListenersのために、内部クラスではなく、匿名クラスの使用にも焦点を当てます。これはあなたのコードを非常に短くするでしょう。そして、すべての空白行を取り除く:P

関連する問題