import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DialogTest
{
public static void main(String[] args)
{
DialogFrame frame = new DialogFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}/**
A frame with a menu whose File->About action shows a dialog.
*/
class DialogFrame extends JFrame
{
public DialogFrame()
{
setTitle("DialogTest");
setSize(WIDTH, HEIGHT); // construct a File menu JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu); // add About and Exit menu items // The About item shows the About dialog JMenuItem aboutItem = new JMenuItem("About");
aboutItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if (dialog == null) // first time
dialog = new AboutDialog(DialogFrame.this);
dialog.show(); // pop up dialog
}
});
fileMenu.add(aboutItem); // The Exit item exits the program JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
fileMenu.add(exitItem);
} public static final int WIDTH = 300;
public static final int HEIGHT = 200; private AboutDialog dialog;
}/**
A sample modal dialog that displays a message and
waits for the user to click the Ok button.
*/
class AboutDialog extends JDialog
{
public AboutDialog(JFrame owner)
{
super(owner, "About DialogTest", true);
Container contentPane = getContentPane(); // add HTML label to center contentPane.add(new JLabel("这只是一个对话框的例子!"),
BorderLayout.CENTER); // Ok button closes the dialog
JButton ok = new JButton("Ok");
ok.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
setVisible(false);
}
}); // add Ok button to southern border
JPanel panel = new JPanel();
panel.add(ok);
contentPane.add(panel, BorderLayout.SOUTH); setSize(250, 150);
}
}
import java.awt.event.*;
import javax.swing.*;
public class DialogTest
{
public static void main(String[] args)
{
DialogFrame frame = new DialogFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}/**
A frame with a menu whose File->About action shows a dialog.
*/
class DialogFrame extends JFrame
{
public DialogFrame()
{
setTitle("DialogTest");
setSize(WIDTH, HEIGHT); // construct a File menu JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu); // add About and Exit menu items // The About item shows the About dialog JMenuItem aboutItem = new JMenuItem("About");
aboutItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if (dialog == null) // first time
dialog = new AboutDialog(DialogFrame.this);
dialog.show(); // pop up dialog
}
});
fileMenu.add(aboutItem); // The Exit item exits the program JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
fileMenu.add(exitItem);
} public static final int WIDTH = 300;
public static final int HEIGHT = 200; private AboutDialog dialog;
}/**
A sample modal dialog that displays a message and
waits for the user to click the Ok button.
*/
class AboutDialog extends JDialog
{
public AboutDialog(JFrame owner)
{
super(owner, "About DialogTest", true);
Container contentPane = getContentPane(); // add HTML label to center contentPane.add(new JLabel("这只是一个对话框的例子!"),
BorderLayout.CENTER); // Ok button closes the dialog
JButton ok = new JButton("Ok");
ok.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
setVisible(false);
}
}); // add Ok button to southern border
JPanel panel = new JPanel();
panel.add(ok);
contentPane.add(panel, BorderLayout.SOUTH); setSize(250, 150);
}
}
解决方案 »
- oracle directory manager
- 求解:写一个Java函数,交换两个字符串的值
- 内存问题
- 捆饶新手N长时间的问题
- 五十分求解,怎么检测一个字符串是否是合法的日期格式yyyy/MM/dd HH:mm:ss
- 怎么用javax.swing.timer
- jvm移植
- 如何在JInternalFrame中创建一个模态的JDialog???
- 我的JBuilder 6.0怎么用不了呢?
- 我在广州,是JAVA爱好者,想投身JAVA开发,苦于没有机会。
- 请教大家,我在自定义输入类的几个不明白问题(我自己写了个输入类,但有几个毛病解决不了,特向大家请教)
- Jtree中如何在一个节点上存放两个值.一个是文本,一个是值.文本显示出来,而键不显示.
import java.awt.*;
import java.awt.event.*;public class Menu extends JFrame
{
public Menu()
{
super("菜单栏");
setSize(800,600);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{System.exit(0);}});
JMenuBar bar=new JMenuBar();
setJMenuBar(bar);
JMenu filemenu=new JMenu("文件");
JMenuItem fileopen=new JMenuItem("打开");
JMenuItem filenew=new JMenuItem("新建");
JMenuItem filesave=new JMenuItem("保存");
JMenuItem fileexit=new JMenuItem("退出");
JMenu formatmenu=new JMenu("格式");
//
JMenu colormenu=new JMenu("颜色"); //!!!!此处也用 JMenu!!!!!
JMenuItem blue=new JMenuItem("蓝色"); //!!!!此处为 JMenuItem
fileexit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);}});
filemenu.add(fileopen);
filemenu.add(filenew);
filemenu.add(filesave);
filemenu.addSeparator(); //添加分格线
filemenu.add(fileexit);
formatmenu.add(colormenu); //添加子菜单
colormenu.add(blue); //在子菜单中添加二级菜单
bar.add(filemenu);
bar.add(formatmenu);
setVisible(true);
}
public static void main(String args[])
{
new Menu();
}
}