我需要让ChatPanel类中的JTextField响应Toolbar类中的JButton点击。如果我的程序变得很大,有什么方法可以做到这一点吗?我试着用Action对象做这件事,但我不确定我需要用这个对象做什么。这是我的代码:
public class Toolbar extends JPanel {
private JButton helloButton;
public Toolbar() {
setLayout(new FlowLayout());
helloButton = new JButton(new HelloAction("Hello", null));
add(helloButton);
}
class HelloAction extends AbstractAction {
public HelloAction(String text, ImageIcon icon) {
super(text, icon);
}
@Override
public void actionPerformed(ActionEvent e) {
//want to print "hellon" in the JTextArea in ChatPanel
}
}
}
public class ChatPanel extends JPanel {
private JLabel nameLabel;
private JTextField chatField;
private JTextArea chatArea;
private GridBagConstraints gc;
private static final String NEWLINE = "n";
public ChatPanel() {
super(new GridBagLayout());
//chatArea
chatArea = new JTextArea(8, 30);
chatArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(chatArea);
gc = new GridBagConstraints();
gc.gridx = 0;
gc.gridy = 0;
add(scrollPane, gc);
//nameLabel
nameLabel = new JLabel("Name: ");
gc = new GridBagConstraints();
gc.gridx = 0;
gc.gridy = 2;
gc.anchor = GridBagConstraints.LINE_START;
gc.weightx = 0.0;
add(nameLabel, gc);
//chatField
chatField = new JTextField(25);
chatField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String message = chatField.getText() + NEWLINE;
chatArea.append(message);
chatField.setText("");
}
});
gc = new GridBagConstraints();
gc.gridx = 0;
gc.gridy = 2;
gc.anchor = GridBagConstraints.LINE_END;
gc.weightx = 0.0;
add(chatField, gc);
}
}
public class MainFrame {
private static int width = 800;
private static int height = (int) (width * (9.0 / 16));
private JFrame mainFrame = new JFrame("Main Frame");
public MainFrame() {
mainFrame.setPreferredSize(new Dimension(width, height));
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLayout(new GridBagLayout());
mainFrame.pack();
mainFrame.setResizable(false);
//Toolbar
Toolbar toolbar = new Toolbar();
GridBagConstraints gcToolbar = new GridBagConstraints();
gcToolbar.gridx = 0;
gcToolbar.gridy = 0;
mainFrame.add(toolbar);
//ChatPanel
ChatPanel chatPanel = new ChatPanel();
GridBagConstraints gcChatPanel = new GridBagConstraints();
gcChatPanel.gridx = 0;
gcChatPanel.gridy = 2;
gcChatPanel.gridwidth = 2;
gcChatPanel.weighty = 1.0;
gcChatPanel.weightx = 1.0;
gcChatPanel.anchor = GridBagConstraints.LAST_LINE_START;
mainFrame.add(chatPanel, gcChatPanel);
mainFrame.setVisible(true);
}
}
此外,任何关于我可以改进的地方/我做错了什么的反馈都将受到赞赏。
你的问题实际上有点大,但有了一些聪明的想法,很容易克服。。。
从本质上讲,您需要通过某种方式从Toolbar
与ChatPanel
进行通信。有很多方法可以实现这一点,但如果你想要灵活性,那么你应该分解需求并专注于每个部分的功能。。。
- 工具栏包含"操作">
- 其中一些"操作"会为某些内容添加文本
从这里开始,我们需要一些能够将行动和想要更新的内容联系起来的东西。
让我们从一个简单的合同开始
public interface Outlet {
public void say(String text);
}
所有这些都表明,这个接口的实现可以接受一些文本。
接下来,我们需要一些方法来设置操作发生时的文本,假设您想要几个不同的操作,比如。。。
public class OutletAction extends AbstractAction {
private Outlet outlet;
private String text;
public OutletAction(Outlet outlet, String text) {
this.outlet = outlet;
this.text = text;
}
public Outlet getOutlet() {
return outlet;
}
public String getText() {
return text;
}
@Override
public void actionPerformed(ActionEvent e) {
getOutlet().say(getText());
}
}
会让生活更轻松。基本上,这允许您指定文本应该发送到的Outlet
和要发送的文本
现在,为了让生活更轻松,你可以设置一系列常见的"单词"动作,例如。。。
public class HelloAction extends OutletAction {
public HelloAction(Outlet outlet) {
super(outlet, "Hello");
putValue(Action.NAME, "Hello");
//putValue(Action.SMALL_ICON, icon);
}
}
现在,您需要某种方法将这些操作应用于Toolbar
。。。
public class Toolbar extends JPanel {
public void addAction(Action action) {
add(new JButton(action));
}
}
简单地添加一个负责创建JButton
并对其应用操作的addAction
方法似乎是一个简单的解决方案。
最后,我们需要更新ChatPanel
以实现Outlet
interface
public class ChatPanel extends JPanel implements Outlet {
//...
@Override
public void say(String text) {
chatArea.append(text + NEWLINE);
}
}
现在,你只需要把它们绑在一起。。。
ChatPanel chatPane = new ChatPanel();
HelloAction action = new HelloAction(chatPane);
Toolbar toolBar = new Toolbar();
toolBar.addAction(action);
//... Add what ever other actions you might like...
这展示了一个通常被称为"代码到接口(而不是实现("的原则,它将代码解耦,从而允许您在不影响代码其余部分的情况下更改底层实现。它还保护您的代码免受不必要的交互,因为操作只能调用Outlet
接口的say
方法;(
最后,一个可运行的例子。。。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
ChatPanel chatPane = new ChatPanel();
HelloAction action = new HelloAction(chatPane);
Toolbar toolBar = new Toolbar();
toolBar.addAction(action);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(toolBar, BorderLayout.NORTH);
frame.add(chatPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class Toolbar extends JPanel {
public void addAction(Action action) {
add(new JButton(action));
}
}
public class OutletAction extends AbstractAction {
private Outlet outlet;
private String text;
public OutletAction(Outlet outlet, String text) {
this.outlet = outlet;
this.text = text;
}
public Outlet getOutlet() {
return outlet;
}
public String getText() {
return text;
}
@Override
public void actionPerformed(ActionEvent e) {
getOutlet().say(getText());
}
}
public class HelloAction extends OutletAction {
public HelloAction(Outlet outlet) {
super(outlet, "Hello");
putValue(Action.NAME, "Hello");
//putValue(Action.SMALL_ICON, icon);
}
}
public interface Outlet {
public void say(String text);
}
public class ChatPanel extends JPanel implements Outlet {
private JLabel nameLabel;
private JTextField chatField;
private JTextArea chatArea;
private GridBagConstraints gc;
private static final String NEWLINE = "n";
public ChatPanel() {
super(new GridBagLayout());
//chatArea
chatArea = new JTextArea(8, 30);
chatArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(chatArea);
gc = new GridBagConstraints();
gc.gridx = 0;
gc.gridy = 0;
add(scrollPane, gc);
//nameLabel
nameLabel = new JLabel("Name: ");
gc = new GridBagConstraints();
gc.gridx = 0;
gc.gridy = 2;
gc.anchor = GridBagConstraints.LINE_START;
gc.weightx = 0.0;
add(nameLabel, gc);
//chatField
chatField = new JTextField(25);
chatField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String message = chatField.getText() + NEWLINE;
chatArea.append(message);
chatField.setText("");
}
});
gc = new GridBagConstraints();
gc.gridx = 0;
gc.gridy = 2;
gc.anchor = GridBagConstraints.LINE_END;
gc.weightx = 0.0;
add(chatField, gc);
}
@Override
public void say(String text) {
chatArea.append(text + NEWLINE);
}
}
}