gpt4 book ai didi

java - 如何在两个JInternalFrame之间传递数据?

转载 作者:行者123 更新时间:2023-12-01 18:26:49 25 4
gpt4 key购买 nike

我正在制作一个程序,我在这个框架中有 1 个带有 JDesktopPane 的 JFrame,我打开两个 JInternalFrame,我想在这两个 JInternalFrame 之间传递数据,但使用 JTextField。我只是要传递数据,但它不会更新我想显示的 JInternalFrame。但如果我选择再次打开,它会显示数据。请帮我!谢谢

在这个 JInternalFrame 2 中,我将数据发送到另一个 JInternalFrame 1

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
String word = jTxtIDACA.getText();
DatosPersonales frame = new DatosPersonales();
frame.getData(word);
frame.setVisible(true);
this.getDesktopPane().add(frame);
this.dispose();

}

这是 JInternalFrame 1,我有

public void getData(String word){
initComponents();
this.word = word;
jTxtIDACA.setText(word);

}

最佳答案

基本思想是你想要某种模型,它保存数据并使用 Observer Pattern当模型以某种方式发生变化时向感兴趣的各方提供通知。

两个 JInternalFrames 将能够共享该模型,一个可以更新它,另一个可以监视它的更改(严格来说,这种关系可以双向工作,但我们将离开目前仅此而已)...

Model

现在,因为我永远不知道人们可能喜欢什么或如何使用该模型,所以我总是从一个接口(interface)开始,并提供一个抽象实现和某种形式如果我觉得需要的话,默认实现......

public interface FruitBowl {

public void addFruit(String fruit);
public void removeFruit(String fruit);
public List<String> getFruit();

public void addFruitBowlListener(FruitBowlListener listener);
public void removeFruitBowlListener(FruitBowlListener listener);

}

public abstract class AbstractFruitBowl implements FruitBowl {

private EventListenerList listenerList;

public AbstractFruitBowl() {
}

protected EventListenerList getEventListenerList() {
if (listenerList == null) {
listenerList = new EventListenerList();
}
return listenerList;
}

@Override
public void addFruitBowlListener(FruitBowlListener listener) {
getEventListenerList().add(FruitBowlListener.class, listener);
}

@Override
public void removeFruitBowlListener(FruitBowlListener listener) {
getEventListenerList().remove(FruitBowlListener.class, listener);
}

protected void fireFruitAdded(String fruit) {
FruitBowlListener[] listeners = getEventListenerList().getListeners(FruitBowlListener.class);
if (listeners.length > 0) {
FruitBowlEvent evt = new FruitBowlEvent(this, fruit);
for (FruitBowlListener listener : listeners) {
listener.fruitAdded(evt);
}
}
}

protected void fireFruitRemoved(String fruit) {
FruitBowlListener[] listeners = getEventListenerList().getListeners(FruitBowlListener.class);
if (listeners.length > 0) {
FruitBowlEvent evt = new FruitBowlEvent(this, fruit);
for (FruitBowlListener listener : listeners) {
listener.fruitRemoved(evt);
}
}
}
}

public class DefaultFruitBowl extends AbstractFruitBowl {

private List<String> fruits;

public DefaultFruitBowl() {
fruits = new ArrayList<>(25);
}

@Override
public void addFruit(String fruit) {
fruits.add(fruit);
fireFruitAdded(fruit);
}

@Override
public void removeFruit(String fruit) {
fruits.remove(fruit);
fireFruitRemoved(fruit);
}

@Override
public List<String> getFruit() {
return Collections.unmodifiableList(fruits);
}

}

接下来我们需要通过使用监听器来定义观察者模式契约...

public class FruitBowlEvent extends EventObject {
private String fruit;

public FruitBowlEvent(FruitBowl fruitBowl, String fruit) {
super(fruitBowl);
this.fruit = fruit;
}

public FruitBowl getFruitBowl() {
return (FruitBowl)getSource();
}

public String getFruit() {
return fruit;
}

}

public interface FruitBowlListener extends EventListener {

public void fruitAdded(FruitBowlEvent evt);
public void fruitRemoved(FruitBowlEvent evt);

}

接下来,我们定义 UI...

public class TestModel {

public static void main(String[] args) {
new TestModel();
}

public TestModel() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

FruitBowl fb = new DefaultFruitBowl();

JDesktopPane dp = new JDesktopPane() {
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 200);
}
};
JInternalFrame manager = new JInternalFrame("Fruit Bowl Manager", true, true, true, true);
manager.add(new FruitBowelManagerPane(fb));
manager.setVisible(true);
manager.setBounds(0, 0, 200, 200);

JInternalFrame monitor = new JInternalFrame("Fruit Bowl Monitor", true, true, true, true);
monitor.add(new FruitBowelMonitorPane(fb));
monitor.setVisible(true);
monitor.setBounds(200, 0, 200, 200);

dp.add(manager);
dp.add(monitor);

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(dp);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public abstract class AbstractFruitPane extends JPanel {

private FruitBowl fruitBowl;

public AbstractFruitPane(FruitBowl fruitBowl) {
this.fruitBowl = fruitBowl;
}

public FruitBowl getFruitBowl() {
return fruitBowl;
}

}

public class FruitBowelManagerPane extends AbstractFruitPane {

private String[] fruits = {"Banana", "Strewberry", "Pear", "Peach", "Orange"};

private JButton giver;
private JButton taker;

public FruitBowelManagerPane(FruitBowl fruitBowl) {
super(fruitBowl);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = 1;

giver = new JButton("Add fruit");
taker = new JButton("Remove fruit");
taker.setEnabled(false);

giver.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String fruit = fruits[(int)(fruits.length * Math.random())];
getFruitBowl().addFruit(fruit);
taker.setEnabled(true);
}
});
taker.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
List<String> fruits = getFruitBowl().getFruit();
String eat = fruits.get((int)(fruits.size() * Math.random()));
getFruitBowl().removeFruit(eat);
if (getFruitBowl().getFruit().isEmpty()) {
taker.setEnabled(false);
}
}
});

add(giver, gbc);
add(taker, gbc);
}

}

public class FruitBowelMonitorPane extends AbstractFruitPane {

private JLabel label;

public FruitBowelMonitorPane(FruitBowl fruitBowl) {
super(fruitBowl);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();

label = new JLabel("I'm watching you...");
add(label);

fruitBowl.addFruitBowlListener(new FruitBowlListener() {
@Override
public void fruitAdded(FruitBowlEvent evt) {
label.setText("You added " + evt.getFruit());
}

@Override
public void fruitRemoved(FruitBowlEvent evt) {
if (getFruitBowl().getFruit().isEmpty()) {
label.setText("You ate all the fruit!");
} else {
label.setText("You ate my " + evt.getFruit());
}
}
});

}

}

}

更新了组合示例

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EventListener;
import java.util.EventObject;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.EventListenerList;

public class TestModel {

public static void main(String[] args) {
new TestModel();
}

public TestModel() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

FruitBowl fb = new DefaultFruitBowl();

JDesktopPane dp = new JDesktopPane() {
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 200);
}
};
JInternalFrame manager = new JInternalFrame("Fruit Bowl Manager", true, true, true, true);
manager.add(new FruitBowelManagerPane(fb));
manager.setVisible(true);
manager.setBounds(0, 0, 200, 200);

JInternalFrame monitor = new JInternalFrame("Fruit Bowl Monitor", true, true, true, true);
monitor.add(new FruitBowelMonitorPane(fb));
monitor.setVisible(true);
monitor.setBounds(200, 0, 200, 200);

dp.add(manager);
dp.add(monitor);

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(dp);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public abstract class AbstractFruitPane extends JPanel {

private FruitBowl fruitBowl;

public AbstractFruitPane(FruitBowl fruitBowl) {
this.fruitBowl = fruitBowl;
}

public FruitBowl getFruitBowl() {
return fruitBowl;
}

}

public class FruitBowelManagerPane extends AbstractFruitPane {

private String[] fruits = {"Banana", "Strewberry", "Pear", "Peach", "Orange"};

private JButton giver;
private JButton taker;

public FruitBowelManagerPane(FruitBowl fruitBowl) {
super(fruitBowl);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = 1;

giver = new JButton("Add fruit");
taker = new JButton("Remove fruit");
taker.setEnabled(false);

giver.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String fruit = fruits[(int) (fruits.length * Math.random())];
getFruitBowl().addFruit(fruit);
taker.setEnabled(true);
}
});
taker.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
List<String> fruits = getFruitBowl().getFruit();
String eat = fruits.get((int) (fruits.size() * Math.random()));
getFruitBowl().removeFruit(eat);
if (getFruitBowl().getFruit().isEmpty()) {
taker.setEnabled(false);
}
}
});

add(giver, gbc);
add(taker, gbc);
}

}

public class FruitBowelMonitorPane extends AbstractFruitPane {

private JLabel label;

public FruitBowelMonitorPane(FruitBowl fruitBowl) {
super(fruitBowl);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();

label = new JLabel("I'm watching you...");
add(label);

fruitBowl.addFruitBowlListener(new FruitBowlListener() {
@Override
public void fruitAdded(FruitBowlEvent evt) {
label.setText("You added " + evt.getFruit());
}

@Override
public void fruitRemoved(FruitBowlEvent evt) {
if (getFruitBowl().getFruit().isEmpty()) {
label.setText("You ate all the fruit!");
} else {
label.setText("You ate my " + evt.getFruit());
}
}
});

}

}

public class FruitBowlEvent extends EventObject {

private String fruit;

public FruitBowlEvent(FruitBowl fruitBowl, String fruit) {
super(fruitBowl);
this.fruit = fruit;
}

public FruitBowl getFruitBowl() {
return (FruitBowl) getSource();
}

public String getFruit() {
return fruit;
}

}

public interface FruitBowlListener extends EventListener {
public void fruitAdded(FruitBowlEvent evt);
public void fruitRemoved(FruitBowlEvent evt);
}

public interface FruitBowl {
public void addFruit(String fruit);
public void removeFruit(String fruit);
public List<String> getFruit();
public void addFruitBowlListener(FruitBowlListener listener);
public void removeFruitBowlListener(FruitBowlListener listener);
}

public abstract class AbstractFruitBowl implements FruitBowl {

private EventListenerList listenerList;

public AbstractFruitBowl() {
}

protected EventListenerList getEventListenerList() {
if (listenerList == null) {
listenerList = new EventListenerList();
}
return listenerList;
}

@Override
public void addFruitBowlListener(FruitBowlListener listener) {
getEventListenerList().add(FruitBowlListener.class, listener);
}

@Override
public void removeFruitBowlListener(FruitBowlListener listener) {
getEventListenerList().remove(FruitBowlListener.class, listener);
}

protected void fireFruitAdded(String fruit) {
FruitBowlListener[] listeners = getEventListenerList().getListeners(FruitBowlListener.class);
if (listeners.length > 0) {
FruitBowlEvent evt = new FruitBowlEvent(this, fruit);
for (FruitBowlListener listener : listeners) {
listener.fruitAdded(evt);
}
}
}

protected void fireFruitRemoved(String fruit) {
FruitBowlListener[] listeners = getEventListenerList().getListeners(FruitBowlListener.class);
if (listeners.length > 0) {
FruitBowlEvent evt = new FruitBowlEvent(this, fruit);
for (FruitBowlListener listener : listeners) {
listener.fruitRemoved(evt);
}
}
}
}

public class DefaultFruitBowl extends AbstractFruitBowl {

private List<String> fruits;

public DefaultFruitBowl() {
fruits = new ArrayList<>(25);
}

@Override
public void addFruit(String fruit) {
fruits.add(fruit);
fireFruitAdded(fruit);
}

@Override
public void removeFruit(String fruit) {
fruits.remove(fruit);
fireFruitRemoved(fruit);
}

@Override
public List<String> getFruit() {
return Collections.unmodifiableList(fruits);
}

}
}

关于java - 如何在两个JInternalFrame之间传递数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25841715/

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