我这里有一个主窗体 和一个 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);
//
}}
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);
//
}}
解决方案 »
- 菜鸟求解i++,++i问题。
- 用java实现从文件中读取数据并写入表中
- java事件监听的问题
- 藐 视 奇 怪 的 打 印 问 题....
- linux下开机自动运行java程序
- 谁有JCreator Pro v3的注册码?
- 如何使java类中一方法在Deprecated,使得在jbuilder等开发工具中知道此方法已过时了?
- jb6好烦呀,请高手指点!(因是初来驾道分子不多,望高手不吝赐教!!!)
- 高分求助:请看下面代码出了什么问题,编译没问题,运行出错?
- 急!!!请教在设connection.setAutoCommit(false)时出的问题
- 怎样调用不窗口内的变量
- 编写一个类,包括一个字符串变量,在定义处将期初始化.另一个字符串变量由构建器初始化.说明这两种方法的区别?
这两个函数如下:
paramString()
protected String paramString()
返回此 JDialog 的字符串表示形式。此方法仅在进行调试的时候使用,对于各个实现,所返回字符串的内容和格式可能有所不同。返回的字符串可能为空,但不可能为 null。 覆盖:
类 Dialog 中的 paramString
返回:此 JDialog 的字符串表示形式。
setText()
public void setText(String text)
定义此组件将要显示的单行文本。如果 text 值为 null 或空字符串,则什么也不显示。
此属性的默认值为 null。 这是一个 JavaBeans bound 属性。
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);
}
});
楼主,程序我没看,我看了你的描述,只是我有个疑问哎~~
弱弱地问一句:你为什么不设立一个StringBuffer来接收在Dialog中输入的内容,
然后再将它直接在主窗口的JLabel输出呢?
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显示了。请指正!
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();后都会新创建一个窗体
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();
}
});
}
对于一个模态对话框来说,个人认为其操作方式还是不错的。
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());
}