我目前正在开发一个利用Java Swing的小项目。我想要做的就是重建战列舰。问题是每当我按下一个按钮时,我都不知道如何在我的网格类中编写ActionListener时,如何在另一个类的JTextarea中追加一个字符串。
Par例子:
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,可以打印给定键的值。如何附加TextPanel以显示我当前正在打印到控制台的内容?
PS。我简化了我的问题代码。
答案 0 :(得分: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);
}
}