- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个程序,我在这个框架中有 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
将能够共享该模型,一个可以更新它,另一个可以监视它的更改(严格来说,这种关系可以双向工作,但我们将离开目前仅此而已)...
现在,因为我永远不知道人们可能喜欢什么或如何使用该模型,所以我总是从一个接口(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/
任何人都可以帮助如何将值从一个 jInternalFrame1 传递到另一个 jInternalFrame2?我无法在 jInternalFrame2 中创建 jInternalFrame1 的对
我有 2 个 JInternalFrames,每个 JInternalFrames 包含一个 JPanel。一个 JPanel(源)更新其 GUI 和一些数据以响应鼠标事件。另一个 JPanel(目标
有可能吗?从另一个 JInternalFrame 调用 JInternalFrame ??如果是这样怎么办? 我花了好几个小时寻找答案..之前发现一些问题.. 在这里How to manage a J
我有一个包含这段代码的 JDesktopPane。 public class Menu extends JFrame implements ActionListener{ /** * Creates
这是我的源代码。我无法将我的 JInternalframe 放到前面。我已经尝试了很多代码,但没有任何效果。 private void jMenuItem3ActionPerformed(ja
在下面的 SSCCE 中,您可以看到,如果最大化 JInternalFrame 之一,则会同时最大化它们。这只(AFAIK)发生在“Windows”LookAndFeel 中(如果省略 LookAnd
我在桌面 Pane 内有一个 JInternalframe (tab_items)。每当用户单击 tab_items 中的弹出菜单时,我想在同一桌面 Pane 中打开另一个 JInternalfram
例如: 当JButton1点击JInternalFrame1时在JDesktopPane上显示当JButton2点击JInternalFrame1关闭时,JInternalFrame2在JDeskto
我正在创建一个非常简单的程序。我创建了这个类:MainJframeClass、JDesktopPaneClass、JinternalFrameClass1 和 JinternalFrameClass2
在我的应用程序中,我尝试在实现 MigLayout 的单个 JDesktopPane 中打开一个 JInternalFrame 覆盖另一个 JInternalFrame > 但它在第一个内部框架旁边显
我已经阅读了很多关于 Java 中的构造函数的文章,并在 stackoverflow 中搜索了相关问题,但我仍然对我的程序如何从 jinternalframe1 到 jinternalframe2 获
我使用 netbeans 在 java 中创建了 MDI(多文档界面),其中我有两个 jbuttons 和一个 jdesktoppane 所以当点击两个按钮然后两个 jinternalframes 在
我从 https://stackoverflow.com/a/6868039/2240900 得到的这段代码 how to add the internal2 to desktoppane1 usin
您好,我需要一个示例程序,其中当我最大化 JInternalFrame 时,JFrame 的 JMenuBar 应该设置在 JInternalFrame 上当我再次最小化 JInternalFrame
我创建了一个应用程序,其中有两个标题为 的选项卡选项卡 Pane 1 和 选项卡 Pane 2 .在一个标签正文中 选项卡 Pane 1 包含 JInternalFrame其中有一个搜索按钮。单击按钮
我想在 jFrame 中的桌面 Pane 上显示 jInternalFrame1。jInternalFrame1 包含一个按钮,用于通过删除 jInternalFrame1 在桌面 Pane 上显示
我在网上看到一个JInternalFrame的例子,它是这样写的: public class AddEntry extends JInternalFrame 当我尝试在 Netbeans 中创建 JI
下面的“代码部分 1”用于在 MDI 应用程序中从 menuItem 调用 UcakListesi(JinternalFrame) 没有问题。 我想使用相同的代码从另一个 JinternalFrame
我打算在全屏模式下使用 JInternalFrame 作为模态 JDialog,但是,当它被调用时当前没有显示。我需要将它添加到某个容器中吗?我尝试将它添加到 JOptionPane.showInte
我正在处理与此错误相关的问题,我认为:DefaultDesktopManager does not handle InternalFrame state changes as expected. 我有
我是一名优秀的程序员,十分优秀!