将数据从JFrame传递到JInternalFrame,反之亦然? [英] pass data from JFrame to JInternalFrame and vice-versa?
问题描述
我有一个JFrame,当我点击JFrame上的一个按钮时,它会创建一个带有'Finalize'按钮的新JInternalFrame。我想要的是,当我单击Finalize按钮时,JInternalFrame会处理并向创建JInternalFrame的JFrame发送一些值(数据)。
I have a JFrame, when I click a button on JFrame, it creates a new JInternalFrame having a 'Finalize' button. What I want is that, when I click the Finalize button the JInternalFrame disposes and send some values (data) to that JFrame which created the JInternalFrame.
我怎样才能做到这一点?任何相关的例子对我都有很大的帮助。
How can I achieve this? any relevant example will be a great help to me.
推荐答案
抱歉,我没有太多时间解释。基本上我在下面的代码中做的是从MainFrame传递给InternalFrame的构造函数。这些值是从IntrernalFrame设置的,然后用值做一些事情。我刚刚做了一个凌乱的GUI Builder演示,但您也可以考虑在MainFrame中使用setter和setter,并将整个MainFrame传递给InternalFrame。只是一个想法。您可以运行该程序。我会尽快回来,为了更好的解释。
Sorry I don't have much time to explain. basically what I do in the code below is pass to the constructor of the InternalFrame from the MainFrame. Those values are set from the IntrernalFrame, then does something with the value. I just did a messy GUI Builder demo, but you may also consider using setters and setters in your MainFrame and pass the entire MainFrame to the InternalFrame. Just an idea. You can run the program. I'll try and come back when I have time, for a better explanation.
EDIT
所以我懒得制作真正的示例而不使用超级凌乱的Gui Builder代码,所以我会解释重要的部分。你要做的是分享对象。你可以做的是通过构造函数传递我的引用。我在下面做的是,从 MainFrame
传递一个 JLabel
和一个 String
将由 InternalFrame
更改。当按下 Finalize
按钮时,更改将自动在 MainFrame
中更新。以下是重要代码的含义
So I'm too lazy to make a real example without using the super messy looking Gui Builder code, so I'll explain the important parts. What you're trying to do is share objects. What you can do is pass then my reference through the constructor. What I did below was, from the MainFrame
pass a JLabel
and a String
which will be altered by the InternalFrame
. When the Finalize
button is pressed, the changes will automatically be updated in the MainFrame
. Here's what the important code looks like
MainFrame
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String word = jTextField1.getText();
MyInternalFrame frame = new MyInternalFrame(jLabel1, word);
frame.setVisible(true);
jDesktopPane1.add(frame);
}
InternalFrame
JLabel label;
String word;
public MyInternalFrame(JLabel label, String word) {
this.label = label;
this.word = word;
initComponents();
jTextField1.setText(word);
}
.....
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
label.setText(jTextField1.getText());
this.dispose();
}
这是传递信息的一种简单方法。完整的运行代码。请注意,上面的代码是所有我在自动生成的代码中更改了。自动生成的代码主要用于样式和定位,因此不要被它淹没。你只关心上面的代码。
This is one simple way of passing infomation. Complete running code. Note the above code was all I changed, in the auto-generated code. The auto-generated code is mainly just for styling and positioning, so don't be overwhelmed by it. You're just concerned with the code above.
MainFrame.java
MainFrame.java
public class MainFrame extends javax.swing.JFrame {
public MainFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jDesktopPane1 = new javax.swing.JDesktopPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.setText("jTextField1");
jLabel1.setText("The finalized word is :");
jButton1.setText("Show Internal");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jDesktopPane1Layout = new javax.swing.GroupLayout(jDesktopPane1);
jDesktopPane1.setLayout(jDesktopPane1Layout);
jDesktopPane1Layout.setHorizontalGroup(
jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 356, Short.MAX_VALUE)
);
jDesktopPane1Layout.setVerticalGroup(
jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jDesktopPane1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField1, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jDesktopPane1)
.addGroup(layout.createSequentialGroup()
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1)
.addGap(0, 149, Short.MAX_VALUE)))
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String word = jTextField1.getText();
MyInternalFrame frame = new MyInternalFrame(jLabel1, word);
frame.setVisible(true);
jDesktopPane1.add(frame);
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JDesktopPane jDesktopPane1;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
MyInternalFrame.java
MyInternalFrame.java
import javax.swing.JLabel;
public class MyInternalFrame extends javax.swing.JInternalFrame {
JLabel label;
String word;
public MyInternalFrame(JLabel label, String word) {
this.label = label;
this.word = word;
initComponents();
jTextField1.setText(word);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jTextField1.setText("jTextField1");
jLabel1.setText("Confirm/Change then FInalize");
jButton1.setText("FInalize");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(51, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
.addComponent(jTextField1)))
.addGap(49, 49, 49))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(39, 39, 39)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addContainerGap(43, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
label.setText(jTextField1.getText());
this.dispose();
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
你想要的只是查看MyInternalFrame构造函数上的代码和两个类中的actionPerformed
You'll want to just look at the code on the MyInternalFrame constructor and the actionPerformed in both class
另一个简单修复只是让 InternalFrame
成为 MainFrame
的内部类,这样他们就可以使用属于 JFrame
Another Simple Fix would be just to make the InternalFrame
an inner class of the MainFrame
so they can use the same objects that are members of the JFrame
这篇关于将数据从JFrame传递到JInternalFrame,反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!