2016-12-20 63 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裏面看到,它有一個簡單的系統輸出,其打印指定鍵的值。我如何附加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); 

    } 

}