gpt4 book ai didi

Java Swing——编写一个将根据对自定义数据对象的更改重新绘制自身的 Ui

转载 作者:行者123 更新时间:2023-11-30 05:54:02 25 4
gpt4 key购买 nike

首先,我已经发布了一个问题 earlier其中我曾就为什么我的代码不起作用寻求帮助,这个问题是根据我在那个问题中得到的建议行事的。

我的用例是这样的。

我正在写一个 MethodEditor模块其中每个 Method由具有 2 个数据成员的自定义数据对象表示 --

  1. 输入变量名称列表
  2. String(此方法设置的结果变量的名称)。

这些 Method对象是由于在 JDialog 上填写一些数据而生成的.
这些Method对象存储在具有 1 个数据成员的容器中,该数据成员是 List<Method>容器驻留在 Controller 中 JPanel来自上述JDialog被调用。

从用户界面的角度来看,我想显示每个 Method以 Jbutton 形式的对象,单击它会出现 JDialog将打开并允许用户对其进行编辑。

MethodEditor作用于 List 并生成垂直排列的 JButton,每个 Method 1 个在 List<Method> .这List<Method>传递给 MethodEditor来自上述 Controller JPane .

我之前有 question实现了 MethodEditor作为 JPanel这将添加一个 PropertyChangeListenerList<Method>并且会repaint本身每次都有一个PropertyChange事件,但我的方法不起作用,重绘不会发生。

是否有另一种方法来实现我的用例,或者我是否可以对我在之前 question 中发布的代码进行修复?

最佳答案

您的问题和解决方案之前已被提及。实际上,您必须从 PropertyChangeListener 中向观察者面板添加或移除组件。调用 revalidate()repaint() 不会神奇地添加或删除组件,除非您在调用这些方法之前明确执行此操作

例如:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.IndexedPropertyChangeEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.*;
import javax.swing.event.SwingPropertyChangeSupport;

public class ListenToTest {
public static final String[] ITEMS = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
private JPanel mainPanel = new JPanel();
private ObserverPanel observerPanel = new ObserverPanel();
private ListenToModel model = new ListenToModel();

public ListenToTest() {
observerPanel.setModel(model);

for (String item : ITEMS) {
model.addItem(item);
}

JPanel btnPanel = new JPanel();
btnPanel.add(new JButton(new AddAction("Add")));
btnPanel.add(new JButton(new RemoveAction("Remove")));

mainPanel.setLayout(new BorderLayout());
mainPanel.add(new JScrollPane(observerPanel.getMainComponent()));
mainPanel.add(btnPanel, BorderLayout.PAGE_END);
}

public JComponent getMainComponent() {
return mainPanel;
}

private class AddAction extends AbstractAction {
public AddAction(String title) {
super(title);
}

@Override
public void actionPerformed(ActionEvent arg0) {
String text = JOptionPane.showInputDialog(mainPanel, "Enter a String");
if (text != null) {
model.addItem(text);
}
}
}

private class RemoveAction extends AbstractAction {
public RemoveAction(String title) {
super(title);
}

@Override
public void actionPerformed(ActionEvent arg0) {
int index = observerPanel.getSelectedIndex();
if (index >= 0) {
model.removeItem(index);
}
}
}

private static void createAndShowGui() {
ListenToTest mainPanel = new ListenToTest();

JFrame frame = new JFrame("ListenToModelTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel.getMainComponent());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

class ObserverPanel {
public static final Font LABEL_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 18);
protected static final Color SELECTED_COLOR = new Color(150, 150, 255);
private JPanel mainPanel = new JPanel();
private ListenToModel model;
private GridLayout gridLayout = new GridLayout(0, 1);
private int selectedIndex = -1;

public ObserverPanel() {
mainPanel.setLayout(gridLayout);
mainPanel.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
Component[] components = mainPanel.getComponents();
for (int i = 0; i < components.length; i++) {
if (mainPanel.getComponentAt(p).equals(components[i])) {
selectedIndex = i;
components[i].setBackground(SELECTED_COLOR);
} else {
components[i].setBackground(null);
}
}
}
});
}

public int getSelectedIndex() {
return selectedIndex;
}

public void setModel(ListenToModel model) {
this.model = model;
model.addPropertyChangeListener(new ObserverPanelListener());
}

public JComponent getMainComponent() {
return mainPanel;
}

private class ObserverPanelListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(ListenToModel.ADD)) {
JLabel label = createLabel(evt);
for (Component comp : mainPanel.getComponents()) {
comp.setBackground(null);
}
int index = ((IndexedPropertyChangeEvent)evt).getIndex();
mainPanel.add(label, index);
label.setBackground(SELECTED_COLOR);
selectedIndex = index;
} else if (evt.getPropertyName().equals(ListenToModel.REMOVE)) {
int index = ((IndexedPropertyChangeEvent)evt).getIndex();
mainPanel.remove(index);
for (Component comp : mainPanel.getComponents()) {
comp.setBackground(null);
}
selectedIndex = -1;
} else if (evt.getPropertyName().equals(ListenToModel.REMOVE_ALL)) {
mainPanel.removeAll();
selectedIndex = -1;
}
mainPanel.revalidate();
mainPanel.repaint();
}

private JLabel createLabel(PropertyChangeEvent evt) {
String newValue = evt.getNewValue().toString();
JLabel label = new JLabel(newValue);
label.setFont(LABEL_FONT);
int eb = 20;
label.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.blue),
BorderFactory.createEmptyBorder(eb , eb, eb, eb)));
label.setFocusable(true);
label.setOpaque(true);
return label;
}
}
}

class ListenToModel implements Iterable<String> {
public static final String ADD = "add";
public static final String REMOVE = "remove";
public static final String REMOVE_ALL = "remove all";
private SwingPropertyChangeSupport spcSupport = new SwingPropertyChangeSupport(
this);
private List<String> modelNucleus = new ArrayList<String>();

public void addItem(String item) {
modelNucleus.add(item);
spcSupport.fireIndexedPropertyChange(ADD, modelNucleus.size() - 1, null,
item);
}

public void addItem(int index, String item) {
if (index < 0 || index > modelNucleus.size()) {
// TODO: throw an exception
} else {
modelNucleus.add(index, item);
spcSupport.fireIndexedPropertyChange(REMOVE, index, null, item);
}

}

public void removeItem(int index) {
if (index < 0 || index >= modelNucleus.size()) {
// TODO: throw an exception
} else {
String oldValue = modelNucleus.remove(index);
spcSupport.fireIndexedPropertyChange(REMOVE, index, oldValue, null);
}
}

public void removeAll() {
modelNucleus.clear();
spcSupport.firePropertyChange(REMOVE_ALL, null, null);
}

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

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

@Override
public Iterator<String> iterator() {
return modelNucleus.iterator();
}
}

关于Java Swing——编写一个将根据对自定义数据对象的更改重新绘制自身的 Ui,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9762747/

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