JavaSE18—Swing高级控件(上)
JavaSE18—Swing高级控件(上)
1.为窗体添加菜单
1)创建菜单栏
在窗体中添加菜单,首先要实现在窗体中添加菜单栏。在Java中使用JMenuBar类创建菜单栏,该类在javax.swing包中。菜单创建完成后,可以使用JFrame类的setJMenuBar()方法将菜单栏添加到窗体上。
JMenuBar类有个不惨的构造方法
JMenuBar mb = new JMenuBar();
创建完成菜单栏对象后,需要将该对象添加到窗体中。
setJMenuBar(mb);
创建完成菜单栏对象后,并不包含任何菜单,要实现在菜单栏中添加惨淡对象,需要通过JMenuBar实例的add()方法实现。
2)创建菜单
在Java中使用JMenu类来创建菜单,该类在javax.swing包中,菜单创建完后通过JMenuBar类的add方法将菜单添加到菜单栏上
例如
JmenuBar mb = new JMenuBar();
setJMenuBar(mb);
JMenu file = new JMenu(“文件”);
JMenu edit = new JMenu(“编辑”);
mb.add(file);
mb.add(edit);
3)创建菜单项
在Java中使用JMenuItem类创建菜单项,菜单项创建完以后,可以使用JMenu类的add()方法将菜单项添加到菜单上。
为菜单添加动作时间监听器可以使用addActionListener(ActionListener actionListener)方法来实现
实例:创建窗体对象,在窗体中添加“文件”菜单,并在菜单中添加“打开”与“退出”两个菜单项,在“打开”菜单项中添加图片文件,在“退出”菜单项中添加单击事件。
import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.net.URL;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;public class MenuDemo extends JFrame{public MenuDemo(){super();//设置窗体基本信息setTitle("创建菜单");setBounds(100,100,240,160);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//创建菜单栏JMenuBar menuBar = new JMenuBar();//创建菜单JMenu mn_file = new JMenu("文件");//获取图片URL url = getClass().getResource("file.png");ImageIcon icon = new ImageIcon(url);//为菜单添加菜单项JMenuItem mi_open = new JMenuItem("文件",icon);JMenuItem mi_exit = new JMenuItem("退出");//添加菜单事件mi_exit.addActionListener(new ActionListener(){public void actionPerformed(final ActionEvent arg0){System.exit(0);}});//将菜单项添加到菜单上mn_file.add(mi_open);mn_file.addSeparator();mn_file.add(mi_exit);//将菜单添加到菜单栏上menuBar.add(mn_file);//将菜单栏添加到窗体上setJMenuBar(menuBar);setVisible(true);}public static void main(String[] args){new MenuDemo();}}4)创建弹出式菜单
import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swing.JFrame;import javax.swing.JMenuItem;import javax.swing.JPopupMenu;public class MenuDemo extends JFrame{public MenuDemo(){super();//设置窗体基本信息setTitle("创建菜单");setBounds(100,100,240,160);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//添加容器getContentPane().setLayout(null);final JPopupMenu menu = new JPopupMenu("编辑");//为菜单添加菜单项JMenuItem mi_open = new JMenuItem("文件");JMenuItem mi_exit = new JMenuItem("退出");//将菜单栏添加到菜单中menu.add(mi_open);menu.add(mi_exit);this.add(menu);//添加菜单事件mi_exit.addActionListener(new ActionListener(){public void actionPerformed(final ActionEvent arg0){System.exit(0);}});this.addMouseListener(new MouseAdapter(){@Overridepublic void mouseReleased(MouseEvent e) {// TODO Auto-generated method stubif(e.isPopupTrigger()){menu.show(e.getComponent(), e.getX(), e.getY());}}});setVisible(true);}public static void main(String[] args){new MenuDemo();}}2.添加工具栏和对话框
import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JToolBar;public class ToolBarDemo extends JFrame{public ToolBarDemo(){//创建窗体super();setTitle("ToolBarDemo");setBounds(100,100,269,222);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//新建工具栏JToolBar toolBar = new JToolBar();//工具设置为可以移动toolBar.setFloatable(true);//将工具栏添加到窗体中getContentPane().add(toolBar);//新建并添加按钮JButton newButton = new JButton("新建");toolBar.add(newButton);toolBar.addSeparator();JButton saveButton = new JButton("保存");toolBar.add(saveButton);toolBar.addSeparator();JButton closeButton = new JButton("退出");toolBar.add(closeButton);this.setVisible(true);//设置按钮事件closeButton.addMouseListener(new MouseListener(){@Overridepublic void mouseClicked(MouseEvent arg0) {// TODO Auto-generated method stubSystem.exit(0);}@Overridepublic void mouseEntered(MouseEvent arg0) {// TODO Auto-generated method stub}@Overridepublic void mouseExited(MouseEvent arg0) {// TODO Auto-generated method stub}@Overridepublic void mousePressed(MouseEvent arg0) {// TODO Auto-generated method stub}@Overridepublic void mouseReleased(MouseEvent arg0) {// TODO Auto-generated method stub}});}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubnew ToolBarDemo();}}2)打开本地文件对话框
import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.FileFilter;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.filechooser.FileNameExtensionFilter;public class FileChooserDemo extends JFrame{public FileChooserDemo(){//创建窗体super();setTitle("FileChosserDemo");setBounds(100,100,294,167);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//新建菜单栏JMenuBar menuBar = new JMenuBar();JMenu menu = new JMenu("文件");//将菜单栏加入窗体setJMenuBar(menuBar);//将菜单加入菜单栏menuBar.add(menu);//新建菜单项JMenuItem open = new JMenuItem("打开");//添加事件open.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubJFileChooser fileChooser = new JFileChooser();FileNameExtensionFilter filter = new FileNameExtensionFilter("图像文件(*.bmp;*.gif;*.jpg;*.jpeg)","bmp","gif","jpg","jpeg");fileChooser.setFileFilter(filter);int returnValue = fileChooser.showOpenDialog(getContentPane());}});menu.add(open);setVisible(true);}public static void main(String[] args){new FileChooserDemo();}}