当前位置: 技术问答>java相关
消息提示框!!大虾帮忙
来源: 互联网 发布时间:2015-06-10
本文导语: 弹出一个消息提示框,提示框有确定和取消两个按钮,我如何控制这两个按钮的事件?(jb的design里面好像看不到这两个按钮) 比如当点击确定,退出这个消息框并做相应处理 点击取消,直接退出回到上级页面! 请给...
弹出一个消息提示框,提示框有确定和取消两个按钮,我如何控制这两个按钮的事件?(jb的design里面好像看不到这两个按钮)
比如当点击确定,退出这个消息框并做相应处理
点击取消,直接退出回到上级页面!
请给出从头至尾的代码处理(包括弹出消息提示框)。
在线等待,一定给分
比如当点击确定,退出这个消息框并做相应处理
点击取消,直接退出回到上级页面!
请给出从头至尾的代码处理(包括弹出消息提示框)。
在线等待,一定给分
|
这是一个完整的消息提示框程序,你可以根据自己的需要更改。别忘了给分哦!!
//Listing 14.3 TJOptionPane2.java
/*
*
*
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
public class TJOptionPane2 extends JApplet {
Container container = null;
public void init() {
// 1. Get a handle on the applet's content pane.
container = this.getContentPane();
// 2. Add a box to the content pane.
Box box = new Box(BoxLayout.X_AXIS);
container.add(box);
// 3. Add a button to the box.
JButton button = new JButton("Show Time");
button.setPreferredSize(new Dimension(150,25));
button.addActionListener(new ButtonListener());
box.add(Box.createGlue());
box.add(button);
box.add(Box.createGlue());
}
// 4. The listener class.
class ButtonListener implements ActionListener {
// 5. Argument values for the confirmation dialog box.
Object confirmText = "Do You Wish To See Date Also?";
String confirmTitle = "Date Confirmation Dialog";
int optionType = JOptionPane.YES_NO_OPTION;
int messageType1 = JOptionPane.QUESTION_MESSAGE;
// 6. Argument values for the message dialog box.
Object information = null;
String title = "Message Display Dialog";
int messageType2 = JOptionPane.INFORMATION_MESSAGE;
// 7. Option selected.
// If the selection is 'yes', selectedValue = 0;
// If the selection is 'No', selectedValue = 1;
int selectedValue;
public void actionPerformed(ActionEvent e) {
// 8. Display the confirmation dialog box.
selectedValue = JOptionPane.showConfirmDialog(container,
confirmText, confirmTitle,
optionType, messageType1);
// 9. Fetch the time or date and time.
information = fetchInformation();
// 10. Display the message.
JOptionPane.showMessageDialog(container,
information, title,
messageType2);
}
// 11. Returns the time or date and time depending
// on the Yes or No choice made.
public String fetchInformation() {
DateFormat formatter = null;
if (selectedValue == 0) { //If it is Yes.
formatter = DateFormat.getDateTimeInstance(
DateFormat.SHORT,
DateFormat.LONG);
}
else if(selectedValue == 1) { //If it is No.
formatter = DateFormat.getTimeInstance(
DateFormat.LONG);
}
// Format the time or date and time and return.
return(formatter.format(new Date()));
}
}
}
//Listing 14.3 TJOptionPane2.java
/*
*
*
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
public class TJOptionPane2 extends JApplet {
Container container = null;
public void init() {
// 1. Get a handle on the applet's content pane.
container = this.getContentPane();
// 2. Add a box to the content pane.
Box box = new Box(BoxLayout.X_AXIS);
container.add(box);
// 3. Add a button to the box.
JButton button = new JButton("Show Time");
button.setPreferredSize(new Dimension(150,25));
button.addActionListener(new ButtonListener());
box.add(Box.createGlue());
box.add(button);
box.add(Box.createGlue());
}
// 4. The listener class.
class ButtonListener implements ActionListener {
// 5. Argument values for the confirmation dialog box.
Object confirmText = "Do You Wish To See Date Also?";
String confirmTitle = "Date Confirmation Dialog";
int optionType = JOptionPane.YES_NO_OPTION;
int messageType1 = JOptionPane.QUESTION_MESSAGE;
// 6. Argument values for the message dialog box.
Object information = null;
String title = "Message Display Dialog";
int messageType2 = JOptionPane.INFORMATION_MESSAGE;
// 7. Option selected.
// If the selection is 'yes', selectedValue = 0;
// If the selection is 'No', selectedValue = 1;
int selectedValue;
public void actionPerformed(ActionEvent e) {
// 8. Display the confirmation dialog box.
selectedValue = JOptionPane.showConfirmDialog(container,
confirmText, confirmTitle,
optionType, messageType1);
// 9. Fetch the time or date and time.
information = fetchInformation();
// 10. Display the message.
JOptionPane.showMessageDialog(container,
information, title,
messageType2);
}
// 11. Returns the time or date and time depending
// on the Yes or No choice made.
public String fetchInformation() {
DateFormat formatter = null;
if (selectedValue == 0) { //If it is Yes.
formatter = DateFormat.getDateTimeInstance(
DateFormat.SHORT,
DateFormat.LONG);
}
else if(selectedValue == 1) { //If it is No.
formatter = DateFormat.getTimeInstance(
DateFormat.LONG);
}
// Format the time or date and time and return.
return(formatter.format(new Date()));
}
}
}
|
int reply = JOptionPane.showConfirmDialog(null,
"question",
"confirm",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (reply != JOptionPane.YES_OPTION) {
return;
} //canceled.
continue and do your bussiness...
"question",
"confirm",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (reply != JOptionPane.YES_OPTION) {
return;
} //canceled.
continue and do your bussiness...