java中 使用消息对话框,要用到javax.swing包中的JOptionPane类的静态方法:
public static void showMessageDialog(Component parentComponent,String message,String title,int messageType).
messageType可以以下5个取值:
JOptionPane.INFORMATION_MESSAGE;
JOptionPane.WARNING_MESSAGE;
JOptionPane.ERROR_MESSAGE;
JOptionPane.QUESTION_MESSAGE;
JOptionPane.PLAIN_MESSAGE;
取不同的值,对于着不同的对话框风格.
下面的例子程序实现了以下功能:当你向文本框中输入文字后,点击"show"按钮,会谈出消息对话框,显示你输入的内容.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Message extends JFrame implements ActionListener {
JTextField text=new JTextField("输入文本",30);
public Message(int height,int width){
setTitle("消息对话框测试");
JButton mesButton=new JButton("show");
JPanel panel=new JPanel();
text.setEditable(true);
panel.add(text);
panel.add(mesButton);
setBounds(100,100,width,height);
setContentPane(panel);//装载所有组件
mesButton.addActionListener(this);
}
public static void main(String args[]){
Message newMessage=new Message(500,500);
newMessage.setVisible(true);
}
public void actionPerformed(ActionEvent e){
String comStr=e.getActionCommand();
if (comStr.equals("show"))
{
System.out.print("ok");
JOptionPane.showMessageDialog(this,"你输入的内容是: "+text.getText(),"显示对话框",JOptionPane.INFORMATION_MESSAGE);
}
else System.out.println("error");
}
}