下面是弹出消息框的程序,但怎么运行啊,我的java技术有限啊,谢谢高人指导啊。:
//Listing 14.3 TJOptionPane2.java
/*
* <Applet code=TJOptionPane2 width=400 height=75>
* </Applet>
*/
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 sa 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
/*
* <Applet code=TJOptionPane2 width=400 height=75>
* </Applet>
*/
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 sa 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()));
}
}
}
解决方案 »
- 请问一个小问题,关于MySQL乱码的!
- insert 的时候提示标识符太长,请问怎么解决
- JSP&java.lang.ClassNotFoundException: com.microsoft.JDBC.sqlserver.SQLServerDriver 的问题
- 急急急急..............谁帮忙把ASP.NET改成jsp呀 在线等待.......谢谢
- jsp连接sql数据库的问题。
- 用JSP,在一个函数中如何返回多个值?
- servlet显示数据库图片
- 日期格式的问题
- 请问怎么能在发送的HTML邮件中弹出一个新的页面出来?
- Linix下的Tomcat,为什么我的Applet找不到?在Windows环境下运行正常的,而且jsp中用的servelet没有问题
- 如何屏蔽用户输入jsp?
- request getAttribute为null
public static void main(String [] args)
{
TJOptionPane2 test = new TJOptionPane2();
test.setVisible(true);
} 程序运行结果是显示系统现在的时间
里面是这样写的:
import TJOptionPane2;
class Message
{
public static void main(String [] args)
{
TJOptionPane2 test = new TJOptionPane2();
test.setVisible(true);
}
}
TJOptionPane2 已经编译好了,但编译Message.java时候出错。
Message.java:1:'.' expected import TJOptionPane2 ;
在什么地方加'.'啊,对java不是很熟悉啊。谢谢指教啊。