如何解决有人知道按下X时如何关闭对话框窗口吗?
因此,当您按下左上方的红色X时,很难关闭对话框窗口。当您按下X时关闭对话框窗口本身。我尝试做system.exit(0);在MessageDialog类中,但是没有用。我不确定从哪里开始。
但是,我相信问题的主要根源在这里。
public MessageDialog(Frame parent,String title,String messageString) {
super(parent,title,true);
setSize(400,100);
setLocation(150,150);
setLayout(new BorderLayout());
message = new Label(messageString,Label.CENTER);
add(message,BorderLayout.CENTER);
Panel panel = new Panel(new FlowLayout(FlowLayout.CENTER));
add(panel,BorderLayout.soUTH);
okButton = new Button(" OK ");
okButton.addActionListener(this);
panel.add(okButton);
}
但是,如果我错过了一些重要的事情,那么这里是完整的代码。我仍然是Java编码的初学者。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class JavaEdit extends Frame implements ActionListener {
String clipBoard;
String fileName;
TextArea text;
MenuItem newMI,openMI,saveMI,saveAsMI,exitMI;
MenuItem cutMI,copyMI,deleteMI,pastemI,selectAllMI,aboutJavaMI;
/**
* Constructor
*/
public JavaEdit() {
super("JavaEdit"); // set frame title
setLayout(new BorderLayout()); // set layout
// create menu bar
MenuBar menubar = new MenuBar();
setMenuBar(menubar);
// create file menu
Menu fileMenu = new Menu("File");
menubar.add(fileMenu);
newMI = fileMenu.add(new MenuItem("New"));
newMI.addActionListener(this);
openMI = fileMenu.add(new MenuItem("Open"));
openMI.addActionListener(this);
fileMenu.addSeparator();
saveMI = fileMenu.add(new MenuItem("Save"));
saveMI.addActionListener(this);
saveAsMI = fileMenu.add(new MenuItem("Save As ..."));
saveAsMI.addActionListener(this);
fileMenu.addSeparator();
exitMI = fileMenu.add(new MenuItem("Exit"));
exitMI.addActionListener(this);
// create edit menu
Menu editMenu = new Menu("Edit");
menubar.add(editMenu);
cutMI = editMenu.add(new MenuItem("Cut"));
cutMI.addActionListener(this);
copyMI = editMenu.add(new MenuItem("copy"));
copyMI.addActionListener(this);
pastemI = editMenu.add(new MenuItem("Paste"));
pastemI.addActionListener(this);
deleteMI = editMenu.add(new MenuItem("Delete"));
deleteMI.addActionListener(this);
selectAllMI = editMenu.add(new MenuItem("Select All"));
selectAllMI.addActionListener(this);
Menu helpMenu = new Menu("Help");
menubar.add(helpMenu);
aboutJavaMI = helpMenu.add(new MenuItem("About Java"));
aboutJavaMI.addActionListener(this);
// create text editing area
text = new TextArea();
add(text,BorderLayout.CENTER);
}
// implementing ActionListener
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if(source == newMI) {
clearText();
fileName = null;
setTitle("JavaEdit"); // reset frame title
}
else if(source == openMI) {
doopen();
}
else if(source == saveMI) {
doSave();
}
else if(source == saveAsMI) {
doSaveAs();
}
else if(source == exitMI) {
MessageDialog dialog = new MessageDialog(this,"Warning","Are you sure you want to quit?");
dialog.setVisible(true);
System.exit(0);
}
else if(source == cutMI) {
docopy();
doDelete();
}
else if(source == copyMI) {
docopy();
}
else if(source == pastemI) {
doPaste();
}
else if(source == deleteMI) {
doDelete();
}
else if(source == selectAllMI) {
doSelectAll();
}
else if(source == aboutJavaMI) {
doAboutJava();
}
}
/**
* method to specify and open a file
*/
private void doopen() {
// display file selection dialog
FileDialog fDialog = new FileDialog(this,"Open ...",FileDialog.LOAD);
fDialog.setVisible(true);
// Get the file name chosen by the user
String name = fDialog.getFile();
// If user canceled file selection,return without doing anything.
if(name == null)
return;
fileName = fDialog.getDirectory() + name;
// Try to create a file reader from the chosen file.
FileReader reader=null;
try {
reader = new FileReader(fileName);
} catch (FileNotFoundException ex) {
MessageDialog dialog = new MessageDialog(this,"Error Message","File Not Found: "+fileName);
dialog.setVisible(true);
return;
}
BufferedReader bReader = new BufferedReader(reader);
// Try to read from the file one line at a time.
StringBuffer textBuffer = new StringBuffer();
try {
String textLine = bReader.readLine();
while (textLine != null) {
textBuffer.append(textLine + '\n');
textLine = bReader.readLine();
}
bReader.close();
reader.close();
} catch (IOException ioe) {
MessageDialog dialog = new MessageDialog(this,"Error reading file: "+ioe.toString());
dialog.setVisible(true);
return;
}
setTitle("JavaEdit: " +name); // reset frame title
text.setText(textBuffer.toString());
}
/**
* method to clear text editing area
*/
private void clearText() {
text.setText("");
}
private void doSave() {
}
private void doSaveAs() {
}
/**
* method to copy selected text to the clipBoard
*/
private void docopy() {
clipBoard = new String(text.getSelectedText());
}
/**
* method to delete selected text
*/
private void doDelete() {
text.replaceRange("",text.getSelectionStart(),text.getSelectionEnd());
}
/**
* method to replace current selection with the contents of the clipBoard
*/
private void doPaste() {
if(clipBoard != null) {
text.replaceRange(clipBoard,text.getSelectionEnd());
}
}
private void doSelectAll() {
text.selectAll();
}
private void doAboutJava() {
MessageDialog dialog = new MessageDialog(this,"About JavaEdit","JavaEdit in Java,Version 1.1,2020");
dialog.setVisible(true);
return;
}
/**
* class for message dialog
*/
class MessageDialog extends Dialog implements ActionListener {
private Label message;
private Button okButton;
// Constructor
public MessageDialog(Frame parent,BorderLayout.soUTH);
okButton = new Button(" OK ");
okButton.addActionListener(this);
panel.add(okButton);
}
// implementing ActionListener
public void actionPerformed(ActionEvent event) {
setVisible(false);
dispose();
}
}
/**
* the main method
*/
public static void main(String[] argv) {
// create frame
JavaEdit frame = new JavaEdit();
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(size.width-80,size.height-80);
frame.setLocation(20,20);
// add window closing listener
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// show the frame
frame.setVisible(true);
}
}
解决方法
如果使用的是AWT,只需在创建的WindowListener
类中手动添加一个MessageDialog
。
// Constructor
public MessageDialog(Frame parent,String title,String messageString) {
super(parent,title,true);
setSize(400,100);
setLocation(150,150);
setLayout(new BorderLayout());
message = new Label(messageString,Label.CENTER);
add(message,BorderLayout.CENTER);
Panel panel = new Panel(new FlowLayout(FlowLayout.CENTER));
add(panel,BorderLayout.SOUTH);
okButton = new Button(" OK ");
okButton.addActionListener(this);
panel.add(okButton);
// manually add a WindowListener
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("simulate closing dialog");
MessageDialog.super.dispose();
super.windowClosing(e);
}
});
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。