问题描述
我正在制作一个程序,我在这个框架中有 1 个带有 JDesktopPane 的 JFrame,我打开了两个 JInternalFrame,我想在这两个 JInternalFrame 之间传递数据,但使用 JTextField.我只是要传递数据,但它不会更新我想显示的 JInternalFrame.但是如果我选择再次打开它会显示数据.请帮我!谢谢
i'm making a program, and I have 1 JFrame with JDesktopPane in this frame i open two JInternalFrame and I want to pass data between this two JInternalFrame but with JTextField.I'm just made to pass the data but it doesn't update the JInternalFrame that i want to show. But if i choose to open again it show me the data. Please Help me!THANKS
在这个 JInternalFrame 2 中,我将数据发送到另一个 JInternalFrame 1
In this JInternalFrame 2 i sent the data to another 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,我有
this is JInternalFrame 1 and i have
public void getData(String word){
initComponents();
this.word = word;
jTxtIDACA.setText(word);
}
推荐答案
基本思想是你想要某种模型,它保存数据并使用 观察者模式,当模型以某种方式发生变化时向相关方提供通知.
The basic idea is you want some kind of model, which holds the data and use Observer Pattern to provide notification to interested parties when the model changes in some way.
然后两个 JInternalFrames
将能够共享该模型,一个可以更新它,另一个可以监视它的更改(严格来说,这种关系可以双向工作,但我们将离开暂时就这样)...
The two JInternalFrames
would then be able to share that model, one could update it and the other could monitor for changes to it (strictly speaking the relationship can work both ways, but we'll leave that alone for the moment)...
现在,因为我永远不知道人们可能喜欢什么或如何使用该模型,我总是从一个 interface
开始,并提供一个 abstract
实现和某种类型的如果我觉得需要,请使用默认实现....
Now, because I never know what or how people might like to use the model, I always start with an interface
and the provide an abstract
implementation and some kind of default implementation if I feel it's required....
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);
}
}
接下来我们需要通过监听器的使用来定义观察者模式契约...
Next we need to define the observer pattern contract through the use of listeners...
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...
Next, we define the 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);
}
}
}
这篇关于如何在两个 JInternalFrame 之间传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!