有没有办法让一个 class 中的 JTextArea 响应另一个中的 JButton 单击?
Is there any way to make a JTextArea in one class respond to JButton clicks in another?
我需要让聊天面板中的 JTextField class 响应工具栏中的 JButton 单击 class。如果我的程序变大,有什么方法可以很好地扩展吗?我试着用一个 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 "hello\n" 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
进行通信。您可以通过多种方式实现这一目标,但如果您想要灵活性,那么您应该分解需求并专注于每个部分的功能...
- 工具栏保持"actions"
- 其中一些 "actions" 向某些内容添加文本
从这里开始,我们需要一些可以桥接操作和任何想要更新的东西。
让我们从一个简单的合同开始
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
和要发送的文本
现在,为了让生活更轻松,您可以设置一系列常见的 "word" 操作,例如...
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));
}
}
只需添加一个 addAction
方法来创建 JButton
并将操作应用于它似乎是一个简单的解决方案。
最后,我们需要更新 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...
这展示了一个通常称为 "code to interface (not implementation)" 的原则,它以一种允许您更改底层实现而不影响其余代码的方式分离代码。它还可以保护您的代码免受不必要的交互,因为操作只能调用 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);
}
}
}
我需要让聊天面板中的 JTextField class 响应工具栏中的 JButton 单击 class。如果我的程序变大,有什么方法可以很好地扩展吗?我试着用一个 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 "hello\n" 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
进行通信。您可以通过多种方式实现这一目标,但如果您想要灵活性,那么您应该分解需求并专注于每个部分的功能...
- 工具栏保持"actions"
- 其中一些 "actions" 向某些内容添加文本
从这里开始,我们需要一些可以桥接操作和任何想要更新的东西。
让我们从一个简单的合同开始
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
和要发送的文本
现在,为了让生活更轻松,您可以设置一系列常见的 "word" 操作,例如...
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));
}
}
只需添加一个 addAction
方法来创建 JButton
并将操作应用于它似乎是一个简单的解决方案。
最后,我们需要更新 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...
这展示了一个通常称为 "code to interface (not implementation)" 的原则,它以一种允许您更改底层实现而不影响其余代码的方式分离代码。它还可以保护您的代码免受不必要的交互,因为操作只能调用 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);
}
}
}