写了一个关于JAVA 中关于AWT组件的小程序 但是运行时出了点小问题 请大家帮我看看怎么解决
packageEventPackage;importjava.awt.*;importjava.awt.event.*;publicclassDialogWindowex...
package EventPackage;
import java.awt.*;
import java.awt.event.*;
public class DialogWindow extends Frame implements ActionListener{
private SimpleDialog dialog;
private TextArea textArea;//文本区
String newline;
public DialogWindow(){
textArea=new TextArea(5,40);
textArea.setEditable(false);
add("Center",textArea);//??
Button button=new Button ("打开对话框");
button.addActionListener(this);
add("South",button);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
newline = System.getProperty("line.separator");//获得分隔符
}
public void actionPerformed(ActionEvent event){//按钮监听器
if (dialog==null){
dialog=new SimpleDialog(this,"A Simple Dialog");
}
dialog.setVisible(true);
}
//设置文本区textArea的内容
public void setText(String text){
textArea.append(text+newline);
}
public static void main(String[] args) {
DialogWindow window=new DialogWindow();
window.setTitle("DialogWindow Application");
window.pack();
window.setVisible(true);
}
}
//实现一个简单对话框的类
class SimpleDialog extends Dialog implements ActionListener{
TextField field;//文本域
DialogWindow parent;
Button setButton;
public SimpleDialog(Frame dw,String title){
super(dw,title,false);
parent=(DialogWindow)dw;//强制转换
Panel p1=new Panel();
Label label=new Label("请随意输入文字:");
p1.add(label);
field = new TextField(20);
field.addActionListener(this);//又设置了一个监听器
p1.add(field);
add("Center",p1);
//创建两个按钮
Panel p2=new Panel();
p2.setLayout(new FlowLayout(FlowLayout.RIGHT));//???
Button b=new Button("Cancel");
b.addActionListener(this);//又设置了一个监听器
setButton=new Button("Set");
p2.add(b);
p2.add(setButton);
add("South",p2);
pack();//将对话框设置为最佳的大小
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent event){
Object source=event.getSource();//??
if((source==setButton)||(source==field)){
parent.setText(field.getText());
}
field.selectAll();//文本域
setVisible(true);
}
}
1.惦记文本框时 像文本域中输入字符串 输入完后 按SET键或者回车没反映 请大家帮我看看
2.请大家帮我解释一下文本域和文本区的区别
谢谢各位了 给加分!!
监听器全加了。 展开
import java.awt.*;
import java.awt.event.*;
public class DialogWindow extends Frame implements ActionListener{
private SimpleDialog dialog;
private TextArea textArea;//文本区
String newline;
public DialogWindow(){
textArea=new TextArea(5,40);
textArea.setEditable(false);
add("Center",textArea);//??
Button button=new Button ("打开对话框");
button.addActionListener(this);
add("South",button);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
newline = System.getProperty("line.separator");//获得分隔符
}
public void actionPerformed(ActionEvent event){//按钮监听器
if (dialog==null){
dialog=new SimpleDialog(this,"A Simple Dialog");
}
dialog.setVisible(true);
}
//设置文本区textArea的内容
public void setText(String text){
textArea.append(text+newline);
}
public static void main(String[] args) {
DialogWindow window=new DialogWindow();
window.setTitle("DialogWindow Application");
window.pack();
window.setVisible(true);
}
}
//实现一个简单对话框的类
class SimpleDialog extends Dialog implements ActionListener{
TextField field;//文本域
DialogWindow parent;
Button setButton;
public SimpleDialog(Frame dw,String title){
super(dw,title,false);
parent=(DialogWindow)dw;//强制转换
Panel p1=new Panel();
Label label=new Label("请随意输入文字:");
p1.add(label);
field = new TextField(20);
field.addActionListener(this);//又设置了一个监听器
p1.add(field);
add("Center",p1);
//创建两个按钮
Panel p2=new Panel();
p2.setLayout(new FlowLayout(FlowLayout.RIGHT));//???
Button b=new Button("Cancel");
b.addActionListener(this);//又设置了一个监听器
setButton=new Button("Set");
p2.add(b);
p2.add(setButton);
add("South",p2);
pack();//将对话框设置为最佳的大小
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent event){
Object source=event.getSource();//??
if((source==setButton)||(source==field)){
parent.setText(field.getText());
}
field.selectAll();//文本域
setVisible(true);
}
}
1.惦记文本框时 像文本域中输入字符串 输入完后 按SET键或者回车没反映 请大家帮我看看
2.请大家帮我解释一下文本域和文本区的区别
谢谢各位了 给加分!!
监听器全加了。 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询