gpt4 book ai didi

java - 有没有办法让一个类中的 JTextArea 响应另一个类中的 JButton 单击?

转载 作者:行者123 更新时间:2023-12-01 11:21:08 24 4
gpt4 key购买 nike

我需要使 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 "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);
}

}

此外,任何有关我可以改进的地方/我做错的地方的反馈都会受到赞赏。

最佳答案

你的问题实际上有点大,但是通过一些巧妙的想法,很容易克服......

本质上,您需要某种方式从工具栏ChatPanel进行通信。有多种方法可以实现这一目标,但如果您想要灵 active ,那么您应该分解需求并专注于每个部分的功能...

  1. 工具栏包含“操作”
  2. 其中一些“操作”会向某些内容添加文本

从这里开始,我们需要一些东西来桥接操作和想要更新的内容。

让我们从一个简单的契约(Contract)开始

public interface Outlet {

public void say(String text);

}

这说明的是,该接口(interface)的实现可以接受一些文本。

接下来,我们需要某种方法来在操作发生时设置文本,假设您需要几个不同的操作,例如...

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);
}
}

现在,您需要某种方式将这些操作应用到工具栏...

public class Toolbar extends JPanel {

public void addAction(Action action) {
add(new JButton(action));
}

}

只需添加一个 addAction 方法来负责创建 JButton 并将操作应用于它,这似乎是一个简单的解决方案。

最后,我们需要更新ChatPanel来实现Outlet界面

public class ChatPanel extends JPanel implements Outlet {
//...

@Override
public void say(String text) {
chatArea.append(text + NEWLINE);
}

}

现在,您只需将它们全部绑定(bind)在一起......

ChatPanel chatPane = new ChatPanel();

HelloAction action = new HelloAction(chatPane);
Toolbar toolBar = new Toolbar();
toolBar.addAction(action);
//... Add what ever other actions you might like...

这演示了通常称为“代码到接口(interface)(而不是实现)”的原则,它以一种允许您更改底层实现而不影响其余代码的方式解耦代码。它还可以保护您的代码免受不必要的交互,因为操作只能调用 Outlet 接口(interface)的 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);
}

}
}

关于java - 有没有办法让一个类中的 JTextArea 响应另一个类中的 JButton 单击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31218355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com