JAVA的GUI图形问题小弟编了个程序可是不知道怎么了运行后只产生了一个空白的窗口,只有把窗口拉伸一下才组建才能正常显示
代码如下
package dialog;
import java.awt.event.*;
import java.awt.Dialog;
import java.awt.*;
public class MyDialog extends Dialog {
TextField textField1 = new TextField();
String info = null;
public MyDialog(Frame owner,String title,boolean model) {
super(owner,title,model);
this.setBounds(0,0,200,150);
Button button1 = new Button("应用");
Button button2 = new Button("确定");
this.add(textField1);
this.add(button1,"North");
this.add(button2,"South");
if(this.isModal()){
button1.setEnabled(false);
}
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
/*TestDialog td = new TestDialog();
td.setInfo(textField1.getText());*/
((TestDialog)getOwner()).setInfo(textField1.getText()); }
});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(isModal() == true){
info = new String(textField1.getText());
}else{
((TestDialog)getOwner()).setInfo(textField1.getText());
}
dispose();
}
});
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public String getinfo(){
return info;
}
public void setinfo(String str){
textField1.setText(str);
}
private void jbInit() throws Exception { }}
package dialog;import java.awt.*;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.*;public class TestDialog extends Frame { public TestDialog() {
this.setSize(400,600);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
} private void jbInit() throws Exception {
button1.setLabel("打开模胎对话框");
button1.addActionListener(new TestDialog_button1_actionAdapter(this));
button2.setLabel("打开非模胎对话框");
button2.addActionListener(new TestDialog_button2_actionAdapter(this));
this.add(textField1, java.awt.BorderLayout.NORTH);
this.add(button1, java.awt.BorderLayout.WEST);
this.add(button2, java.awt.BorderLayout.CENTER);
}
public void setInfo(String str){
textField1.setText(str);
} public static void main(String[] args) {
//Dialog dialog = new Dialog();
TestDialog dialog = new TestDialog();
try{
dialog.jbInit();
}catch(Exception e){e.printStackTrace();}
} TextField textField1 = new TextField();
Button button1 = new Button();
Button button2 = new Button();
public void button1_actionPerformed(ActionEvent e) {
MyDialog ml = new MyDialog(TestDialog.this,"hi",true);
ml.setinfo(textField1.getText());
ml.setVisible(true);
textField1.setText(ml.getinfo());
} public void button2_actionPerformed(ActionEvent e) {
MyDialog ml = new MyDialog(TestDialog.this,"hw",false);
ml.setinfo(textField1.getText());
ml.setVisible(true);
}}
class TestDialog_button2_actionAdapter implements ActionListener {
private TestDialog adaptee;
TestDialog_button2_actionAdapter(TestDialog adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.button2_actionPerformed(e);
}
}
class TestDialog_button1_actionAdapter implements ActionListener {
private TestDialog adaptee;
TestDialog_button1_actionAdapter(TestDialog adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.button1_actionPerformed(e);
}
}
代码如下
package dialog;
import java.awt.event.*;
import java.awt.Dialog;
import java.awt.*;
public class MyDialog extends Dialog {
TextField textField1 = new TextField();
String info = null;
public MyDialog(Frame owner,String title,boolean model) {
super(owner,title,model);
this.setBounds(0,0,200,150);
Button button1 = new Button("应用");
Button button2 = new Button("确定");
this.add(textField1);
this.add(button1,"North");
this.add(button2,"South");
if(this.isModal()){
button1.setEnabled(false);
}
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
/*TestDialog td = new TestDialog();
td.setInfo(textField1.getText());*/
((TestDialog)getOwner()).setInfo(textField1.getText()); }
});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(isModal() == true){
info = new String(textField1.getText());
}else{
((TestDialog)getOwner()).setInfo(textField1.getText());
}
dispose();
}
});
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public String getinfo(){
return info;
}
public void setinfo(String str){
textField1.setText(str);
}
private void jbInit() throws Exception { }}
package dialog;import java.awt.*;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.*;public class TestDialog extends Frame { public TestDialog() {
this.setSize(400,600);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
} private void jbInit() throws Exception {
button1.setLabel("打开模胎对话框");
button1.addActionListener(new TestDialog_button1_actionAdapter(this));
button2.setLabel("打开非模胎对话框");
button2.addActionListener(new TestDialog_button2_actionAdapter(this));
this.add(textField1, java.awt.BorderLayout.NORTH);
this.add(button1, java.awt.BorderLayout.WEST);
this.add(button2, java.awt.BorderLayout.CENTER);
}
public void setInfo(String str){
textField1.setText(str);
} public static void main(String[] args) {
//Dialog dialog = new Dialog();
TestDialog dialog = new TestDialog();
try{
dialog.jbInit();
}catch(Exception e){e.printStackTrace();}
} TextField textField1 = new TextField();
Button button1 = new Button();
Button button2 = new Button();
public void button1_actionPerformed(ActionEvent e) {
MyDialog ml = new MyDialog(TestDialog.this,"hi",true);
ml.setinfo(textField1.getText());
ml.setVisible(true);
textField1.setText(ml.getinfo());
} public void button2_actionPerformed(ActionEvent e) {
MyDialog ml = new MyDialog(TestDialog.this,"hw",false);
ml.setinfo(textField1.getText());
ml.setVisible(true);
}}
class TestDialog_button2_actionAdapter implements ActionListener {
private TestDialog adaptee;
TestDialog_button2_actionAdapter(TestDialog adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.button2_actionPerformed(e);
}
}
class TestDialog_button1_actionAdapter implements ActionListener {
private TestDialog adaptee;
TestDialog_button1_actionAdapter(TestDialog adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.button1_actionPerformed(e);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货