現在、私は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。私は質問のために私のコードを簡素化した。
MVCやモデル・ビューア・コントローラーを検索し、それを利用しています。 –