JOptionPane对话框的使用
import java.awt.event.*;
import javax.swing.*;
class MyFrame extends JFrame
{
JButton b1;
public static void main(String args[])
{
MyFrame f = new MyFrame();
}
public MyFrame()
{
/* super() method is used to invoke the base class constructor */
super("Window Title");
b1=new JButton("Click Here");
getContentPane().add("Center",b1);
//Register the listener for the button
ButtonListener blisten= new ButtonListener();
b1.addActionListener(blisten);
setSize(200,200);
setVisible(true);
}
//The Listener class
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
JButton source = (JButton)evt.getSource();
source.setText("Button clicked");
JOptionPane.showMessageDialog(MyFrame.this,"Button
clicked","information",JOptionPane.INFORMATION_MESSAGE);
}
}
}
本文介绍了一个使用Java Swing中的JOptionPane类创建简单对话框的示例程序。该程序通过按钮触发显示信息对话框,展示了如何在Java应用程序中集成基本的用户交互组件。
7万+

被折叠的 条评论
为什么被折叠?



