2016-12-20 10 views
0

現在、私はJava Swingを利用した私の小さなプロジェクトに取り組んでいます。私がしようとしているのは、戦艦を作り直すことです。問題は、ボタンを押すたびに、別のクラスのJTextareaに文字列を追加する方法がわかりませんが、私はグリッドクラスにActionListenerを記述しています。サブクラスからの文字列の追加

パー例:

public class MainPanel extends JPanel { 

private GridPanel gridPanel; 
private TextPanel textPanel; 

public MainPanel(String name,Color color) { 
    gridPanel = new GridPanel("Grid", 400, 400, 10,color, name); 
    textPanel = new TextPanel(); 
    } 
} 

これは、両方のクラスをインスタンス化し、私のメインパネルのクラスです。今私は私のコンソールに出力を出力している私のgridPanelの中​​にActionListenerを持っています。私はtextPanelで印刷したいと思います。

クラス(gridPanel)

public class GridPanel extends JPanel { 
    private JButton[][] grid; 

public GridPanel(String panelName, int xWidth, int yHeigth, int buttonSquared, Color color, String name) { 
      grid = new JButton[buttonSquared][buttonSquared]; 
      grid[0][0].addActionListener; 
      grid[i][j].setText("~~~"); 
      grid[i][j].putClientProperty("column", i); 
      grid[i][j].putClientProperty("row", j); 
      grid[i][j].putClientProperty("name", name); 
      grid[i][j].addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        JButton btn = (JButton) e.getSource(); 
        System.out.println("clicked column " + btn.getClientProperty("column") 
          + ", row " + btn.getClientProperty("row") + " Name of the panel you've clicked on: " + btn.getClientProperty("name")); 
       } 
      }); 
      this.add(grid[i][j]); 
     } 
    } 

} 

(TextPanel)

public class TextPanel extends JPanel{ 

JTextArea textArea; 

public TextPanel() { 
    textArea = new JTextArea(); 
    textArea.setSize(200,400); 
    textArea.setEditable(false); 
    textArea.setFont(new Font("Verdana", Font.BOLD, 11)); 
    textArea.append("Test!"); 
    this.add(textArea); 

    } 

} 

あなたが私のActionListenerの内部を見ることができるように、それは与えられたキーの値を出力する簡単なSYSOUTを持っています。どのようにImが現在私のコンソールに印刷しているかを示すために、TextPanelをどのように追加するのですか?

ps。私は質問のために私のコードを簡素化した。

Example screenshot

+0

MVCやモデル・ビューア・コントローラーを検索し、それを利用しています。 –

答えて

0

あなたはMVCフレームワークを構築するつもりはない場合は、ここでは、迅速かつ汚いソリューションです:

メインフレーム

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 

public class MainFrame extends JFrame { 

    public MainFrame() { 
     setLayout(new BorderLayout()); 
     getContentPane().setPreferredSize(new Dimension(400, 250)); 

     TextPanel tp = new TextPanel(); 
     GridPanel gp = new GridPanel(tp); 

     add(tp,BorderLayout.CENTER); 
     add(gp,BorderLayout.SOUTH); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       MainFrame frame = new MainFrame(); 
       frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
       frame.pack(); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 

TextPanel

import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 

public class TextPanel extends JPanel { 
    JTextArea textArea; 

    public TextPanel() { 
     textArea = new JTextArea(); 
     textArea.setSize(200,400); 
     textArea.setEditable(false); 
     textArea.setFont(new Font("Verdana", Font.BOLD, 11)); 
     textArea.append("Test!"); 
     this.add(textArea); 
    } 

    public JTextArea getTextArea() { 
     return textArea; 
    } 

} 

GridPanel

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 

public class GridPanel extends JPanel { 

    TextPanel tp = null; 

    public GridPanel(TextPanel panel) { 

     tp = panel; 

     JButton btn = new JButton("Click Me"); 
     btn.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       JTextArea ta = tp.getTextArea(); 
       ta.setText(ta.getText() + "\n" + "Added!!!"); 
      } 
     }); 
     add(btn); 

    } 

} 
関連する問題