不一定呀,可以传递主窗口的reference(作为参数)给被弹开的窗口,不就可以实现对其的引用了么?
解决方案 »
- 关于 FileInputStream 的问题
- 有关html的层次的问题:如何让一个含文字(或其它控件)的div在一个Flash上面显示啊
- 提一个笨笨的问题,并预祝大家圣诞快乐
- 高手帮忙解决问题,有几个textfield,当鼠标放在某个的时候,点一个按钮就在那个textfield添加文字!!
- Socket如何传递对象?
- 请教各位!有关用javascript写函数来check输入框的问题!
- 如何为JTable加上背景图片?
- 菜鸟求教。散分了。
- 关于JTextPane自动滚屏的问题。(up有分了)
- 幫幫忙吧,先謝過了!!
- 关于JAVA的执行(JAVA)。能执行JSP,但....请帮忙。
- 如何在一个jtable的cell中用一个JcomboBox显示一个Vector!
import java.awt.event.*;
import javax.swing.*;public class Frame1 extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JButton jButton1 = new JButton();
JPanel jPanel2 = new JPanel();
GridLayout gridLayout1 = new GridLayout();
JPanel jPanel3 = new JPanel();
JPanel jPanel4 = new JPanel();
JTextField jTextField2 = new JTextField();
JTextField jTextField1 = new JTextField();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
FlowLayout flowLayout1 = new FlowLayout();
FlowLayout flowLayout2 = new FlowLayout(); /**Construct the frame*/
public Frame1() {
//enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
this.setSize(new Dimension(405, 163));
this.setTitle("Frame Title");
this.getContentPane().setBackground(new Color(245, 246, 247));
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jPanel2.setBackground(new Color(245, 246, 247));
jPanel2.setLayout(gridLayout1);
gridLayout1.setRows(2);
jLabel1.setFont(new java.awt.Font("Dialog", 1, 16));
jLabel1.setText("用户名:");
jLabel2.setFont(new java.awt.Font("Dialog", 1, 16));
jLabel2.setText("密 码:");
jPanel4.setLayout(flowLayout1);
flowLayout1.setHgap(20);
jPanel3.setLayout(flowLayout2);
flowLayout2.setHgap(20);
jPanel3.setBackground(new Color(245, 246, 247));
jPanel4.setBackground(new Color(245, 246, 247));
jTextField1.setMinimumSize(new Dimension(65, 24));
jTextField1.setPreferredSize(new Dimension(65, 24));
jTextField2.setPreferredSize(new Dimension(65, 24));
jPanel1.setBackground(new Color(245, 246, 247));
contentPane.add(jPanel1, BorderLayout.SOUTH);
jPanel1.add(jButton1, null);
contentPane.add(jPanel2, BorderLayout.CENTER);
jPanel2.add(jPanel3, null);
jPanel3.add(jLabel1, null);
jPanel3.add(jTextField2, null);
jPanel2.add(jPanel4, null);
jPanel4.add(jLabel2, null);
jPanel4.add(jTextField1, null);
}
/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}class Frame2 extends JFrame {
GridLayout gridLayout1 = new GridLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel(); public Frame2() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setSize(new Dimension(100,80));
this.getContentPane().setLayout(gridLayout1);
gridLayout1.setRows(2);
jLabel1.setText(jTextField1.getText());
jLabel2.setText(jTextField2.getText());
this.getContentPane().add(jPanel1, null);
jPanel1.add(jLabel1, null);
this.getContentPane().add(jPanel2, null);
jPanel2.add(jLabel2, null);
}
} void jButton1_actionPerformed(ActionEvent e) {
new Frame2().show();
}
}
假如主窗口protected JTextFiled为InputName和InputPass
不能直接在TestDialog里获得InputName的值吗????
类似javascript里的,弹开窗口可以对父窗口用opener.document.xxxx.value……获得某个input框的值类为MainFrame.java
调用
TestBut.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
TestBut_actionPerformed(e);
}
}); void TestBut_actionPerformed(ActionEvent e) {
TestDialog td=new TestDialog(this,"测试……",true);
Dimension tdSize=td.getPreferredSize();
Dimension frmSize=getSize();
ctd.setSize(300,150);
Point loc=getLocation();
ctd.setLocation((frmSize.width-300)/2+loc.x,(frmSize.height-200)/2+loc.y);
ctd.show();
}TestDialog.java 构造
public TestDialog(Frame frame, String title, boolean modal) {
super(frame, title, modal);
try {
jbInit();
pack();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
如果frame2不是frame1的内部类, jLabel1.setText(jTextField1.getText());
jLabel2.setText(jTextField2.getText());
这里就会有错
====================A==================================
// main
public class MainFrame extends JFrame {
....
static String inputName;
static String inputPass; public void actionPerformed(...) {
inputName = txtName.getText();
inputPass = txtPswd.getText();
...
// show ChildFrame
...
}// in Child
class ChildFrame extends ...
MainFrame.inputName
MainFrame.inputPass====================B==================================
// main
public class MainFrame extends JFrame {
MainFrame handle;
String inputName = txtName.getText();
String inputPass = txtPswd.getText(); public MainFrame() {
handle = this;
}
public void actionPerformed(...) {
inputName = txtName.getText();
inputPass = txtPswd.getText();
ChildFrame child = new ChildFrame(handle);
...// in Child
class ChildFrame extends ...
MainFrame parent;
public ChildFrame(MainFrame p) {
parent = p;
}
...
parent.inputName
parent.inputPass
当然了如果要达到你所说的不用给弹出窗口传参数,得到主窗口的输入信息。除了使用内部类或接口,我还没有别的好方法。 另外最好不要引用主窗口,两个窗口是相互独立的在弹出窗口中引用主窗口,会增加两个类之间的偶合度。弹出窗口将降低复用性。最好是主窗口获得输入信息,用弹出窗口的方法将信息传给弹出窗口,再显示。