我这里有一个主窗体 和一个 DIALOG。 在DIALOG中设置好内容,然后关闭DIALOG,主窗体上LABEL的内容也更改为DIALOG中设置的内容,请问怎么实现?我这样写实现不了(这只是一个例子,真正的程序比这个复杂。如果要重新NEW主窗体的话 会很麻烦。因为窗体上还有很多其他的内容要更改。但不是同时更改的)。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class T_Main { private JFrame frame;
private JLabel label;
private JLabel label_1; /**
 * Launch the application
 * @param args
 */
public static void main(String args[]) {
try {
T_Main window = new T_Main();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
} /**
 * Create the application
 */
public T_Main() {
createContents();
} /**
 * Initialize the contents of the frame
 */
private void createContents() {
frame = new JFrame();
frame.getContentPane().setLayout(null);
frame.setBounds(100, 100, 500, 375);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLabel(new JLabel());
getLabel().setText("New JLabel");
getLabel().setBounds(93, 53, 66, 18);
frame.getContentPane().add(getLabel()); setLabel_1(new JLabel());
getLabel_1().setText("New JLabel");
getLabel_1().setBounds(93, 97, 66, 18);
frame.getContentPane().add(getLabel_1()); final JButton modifyButton = new JButton();
modifyButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
O o=new O();
o.setVisible(true);
}
});
modifyButton.setText("Modify");
modifyButton.setBounds(93, 185, 106, 28);
frame.getContentPane().add(modifyButton);
} public void setLabel(JLabel label) {
this.label = label;
} public JLabel getLabel() {
return label;
} public void setLabel_1(JLabel label_1) {
this.label_1 = label_1;
} public JLabel getLabel_1() {
return label_1;
}}import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;import javax.swing.JDialog;
import javax.swing.JTextField;
public class O extends JDialog { private JTextField textField_1;
private JTextField textField; /**
 * Create the dialog
 */
public O() {
super();
setResizable(false);
setModal(true);
getContentPane().setLayout(null);
setBounds(100, 100, 346, 306); textField = new JTextField();
textField.setBounds(87, 87, 87, 22);
getContentPane().add(textField); textField_1 = new JTextField();
textField_1.setBounds(87, 142, 87, 22);
getContentPane().add(textField_1); final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
T_Main t=new T_Main();
t.getLabel().setText(textField.getText());
t.getLabel_1().setText(textField_1.getText());
dispose();
}
});
button.setText("New JButton");
button.setBounds(89, 203, 106, 28);
getContentPane().add(button);
//
}}

解决方案 »

  1.   

    你看能不能用JDialog类的paramString()的函数得到Dialog的文本,然后通过JLable的setText(String text)方法把Dialog的内容赋给Lable。
    这两个函数如下:
    paramString()
    protected String paramString()
    返回此 JDialog 的字符串表示形式。此方法仅在进行调试的时候使用,对于各个实现,所返回字符串的内容和格式可能有所不同。返回的字符串可能为空,但不可能为 null。 覆盖:
    类 Dialog 中的 paramString
    返回:此 JDialog 的字符串表示形式。
    setText()
    public void setText(String text)
    定义此组件将要显示的单行文本。如果 text 值为 null 或空字符串,则什么也不显示。 
    此属性的默认值为 null。 这是一个 JavaBeans bound 属性。 
      

  2.   

    谢谢楼上的朋友,现在从DIALOG里传过去的 不是NULL的值,只要我在这里这样写  把JFRAME多显示一个就好了  但我不想用这样的方法  有没有其他办法?     
            button.addActionListener(new ActionListener() {
                public void actionPerformed(final ActionEvent arg0) {
                    T_Main t=new T_Main();
                    t.getLabel().setText(textField.getText());
                    t.getLabel_1().setText(textField_1.getText());
                    dispose();
                    t.setVisible(true);
                }
            });
      

  3.   

    嗯~~~
    楼主,程序我没看,我看了你的描述,只是我有个疑问哎~~
    弱弱地问一句:你为什么不设立一个StringBuffer来接收在Dialog中输入的内容,
    然后再将它直接在主窗口的JLabel输出呢?
      

  4.   

    回楼上的朋友  关键是怎么在主窗口上的JLABEL上显示出来呢,我按DIALOG上的OK按钮后  主窗体上的内容还是没有改变的!大家可以帮我试试吗?可能是我这里有别的问题?
      

  5.   

    刚刚回答了另外一个人的问题,和你的问题有相关性,拿出来抛砖引玉~~~我设计一个类,含有static的静态成员变量,比如 
    public class Hinge{ 
        static Hashtable hash; 
        public Hinge(){} 
        public void putCom(String asKey,Object aobjValue){ 
            hash.put(asKey,aobjValue); 
        } 
        public Object getCom(String asKey){ 
            return hash.get(asKey); 
        } 

    通过这个类,我可以取到整个系统中的任何一个Frame或者Dialog,只要在合适的时候把实例化的对象putCom()就可以了,这样子你在关闭Dialog之前使用getCom()得到Frame,就可以随意控制它的JLabel显示了。请指正!
      

  6.   

     button.addActionListener(new ActionListener() {
                public void actionPerformed(final ActionEvent arg0) {
                    T_Main t=new T_Main();
                    t.getLabel().setText(textField.getText());
                    t.getLabel_1().setText(textField_1.getText());
                    dispose();
                }
            });
    执行T_Main t=new T_Main();后,会新创建一个窗体,用新的窗体的对象去调用getLabel().setText无法改变原窗体label的值可以改变Label的访问修饰符,使之能在Dialog中访问到它,
    createContents()不要放在构造方法中,因为每T_Main();后都会新创建一个窗体
      

  7.   

    把T_Main对象传到Dialog中即可。class O extends JDialog {
      protected T_Main t;
      public O (T_Main main) {
          this.t = main;
      }
     // .....
     button.addActionListener(new ActionListener() {
                public void actionPerformed(final ActionEvent arg0) {
                    t.getLabel().setText(textField.getText());
                    t.getLabel_1().setText(textField_1.getText());
                    dispose();
                }
            }); 
    }
      

  8.   

    lz可以参考一下JOptionPane里各种预定义对话框中参数获取的方式。
    对于一个模态对话框来说,个人认为其操作方式还是不错的。
      

  9.   

    另外,你程序中dialog里的button事件触发时新初始化了一个T_Main,这个T_Main和你Dialog触发界面所在的T_Main是两个对象,这就导致了你的Label内容修改无法实现。lz可以考虑以下两种解决办法:
    1.将T_Main的对象饮用作为参数传递给dialog,在dialog的ok按钮事件代码中对T_Main中Label属性进行修改。
    2.在dialog中添加paramString获取方法,dialog以模态对话框形式弹出,在T_Main中的按键事件进行如下修改
                public void actionPerformed(final ActionEvent arg0) {
                    O o=new O();
                    o.setVisible(true);
                    getLabel().setText(0.paramString());
                }