gpt4 book ai didi

java - 从 JFrame 修改独立的 JPanel

转载 作者:行者123 更新时间:2023-11-29 07:03:43 24 4
gpt4 key购买 nike

我有一个带有两个独立 JPanel 的 JFrame。其中一个 JPanel 充满了 JButton,而另一个则有几个文本字段。我通过 JFrame 向按钮添加了鼠标监听器,我想这样做,以便当从第一个 JPanel 触发事件时,第二个 JPanel 中的文本字段会更改。这两个面板有自己的类。我该怎么做呢?

最佳答案

  1. 使用 MVC、模型- View -控制、关注点分离。
  2. 让控制您的听众的控件更改模型的状态。
  3. View ——您的 GUI,由控件添加监听器,以便将用户输入传输到控件,从而传输到模型。
  4. View 还可以直接向模型添加监听器,以便它们可以在模型发生变化时更改其显示,或者这通常是通过控件间接完成的。
  5. 不要将 MouseListeners 添加到 JButton。使用 ActionListeners,因为这就是它们的用途。例如,如果禁用 JButton,则附加到它的任何 ActionListener 都将不起作用——这是正确的行为。对于 MouseListeners 则不同。

要获得更具体的帮助,请考虑创建并发布 minimal example program .


编辑
例如:

import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.SwingPropertyChangeSupport;

public class MvcMain {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MvcView view = new MvcView();
MvcModel model = new MvcModel();
MvcControl control = new MvcControl(view, model);
view.createAndDisplay();
}
});
}
}

class MvcView {
private MvcModel model;
private ButtonPanel buttonPanel = new ButtonPanel();
private TextFieldPanel textFieldPanel = new TextFieldPanel();
private JPanel mainPanel = new JPanel();

public MvcModel getModel() {
return model;
}

public ButtonPanel getButtonPanel() {
return buttonPanel;
}

public TextFieldPanel getTextFieldPanel() {
return textFieldPanel;
}

public MvcView() {
mainPanel.setLayout(new GridLayout(0, 1));
mainPanel.add(textFieldPanel);
mainPanel.add(buttonPanel);
}

public void setModel(MvcModel model) {
this.model = model;
model.addPropertyChangeListener(new ModelListener());
}

public void createAndDisplay() {
JFrame frame = new JFrame("MVC Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

private class ModelListener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (ButtonTitle.class.getCanonicalName().equals(evt.getPropertyName())) {
ButtonTitle newValue = model.getButtonTitle();
textFieldPanel.textFieldSetText(newValue.getTitle());
}
}
}
}

enum ButtonTitle {
START("Start"), STOP("Stop"), PAUSE("Pause");
private String title;

private ButtonTitle(String title) {
this.title = title;
}

public String getTitle() {
return title;
}
}

@SuppressWarnings("serial")
class ButtonPanel extends JPanel {
public ButtonPanel() {
setBorder(BorderFactory.createTitledBorder("Button Panel"));
setLayout(new GridLayout(1, 0, 5, 0));
for (ButtonTitle btnTitle : ButtonTitle.values()) {
add(new JButton(new ButtonAction(btnTitle)));
}
}

private class ButtonAction extends AbstractAction {
private ButtonTitle btnTitle;

public ButtonAction(ButtonTitle btnTitle) {
super(btnTitle.getTitle());
this.btnTitle = btnTitle;
}

public void actionPerformed(java.awt.event.ActionEvent e) {
Object oldValue = null;
ButtonTitle newValue = btnTitle;
ButtonPanel.this.firePropertyChange(
ButtonTitle.class.getCanonicalName(), oldValue, newValue);
};
}
}

@SuppressWarnings("serial")
class TextFieldPanel extends JPanel {
private JTextField textField = new JTextField(15);

public TextFieldPanel() {
setBorder(BorderFactory.createTitledBorder("TextField Panel"));
add(textField);
}

public void textFieldSetText(String text) {
textField.setText(text);
}
}

class MvcControl {
private MvcView view;
private MvcModel model;

public MvcControl(MvcView view, MvcModel model) {
this.view = view;
this.model = model;
view.setModel(model);
view.getButtonPanel().addPropertyChangeListener(new ButtonPanelListener());
}

private class ButtonPanelListener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (ButtonTitle.class.getCanonicalName().equals(evt.getPropertyName())) {
ButtonTitle newValue = (ButtonTitle) evt.getNewValue();
model.setButtonTitle(newValue);
}
}
}
}

class MvcModel {
private ButtonTitle buttonTitle;
private SwingPropertyChangeSupport pcSupport = new SwingPropertyChangeSupport(
this);

public void addPropertyChangeListener(PropertyChangeListener listener) {
pcSupport.addPropertyChangeListener(listener);
}

public void removePropertyChangeListener(PropertyChangeListener listener) {
pcSupport.removePropertyChangeListener(listener);
}

public ButtonTitle getButtonTitle() {
return buttonTitle;
}

public void setButtonTitle(ButtonTitle buttonTitle) {
ButtonTitle oldValue = this.buttonTitle;
ButtonTitle newValue = buttonTitle;
this.buttonTitle = buttonTitle;
pcSupport.firePropertyChange(ButtonTitle.class.getCanonicalName(),
oldValue, newValue);
}
}

该示例没有使用接口(interface),这将允许进一​​步分离关注点,从而导致更松散的耦合(一件好事)。

关于java - 从 JFrame 修改独立的 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22288265/

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