图片编辑器:基本框架的显示
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class PhotoFrame { public static void main(String[]args){ //创建框架 final JFrame pf = new JFrame(); pf.setTitle("图片编辑器"); pf.setBounds(50,0,1300,900); pf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pf.setVisible(true); //创建菜单栏并添加到窗口顶部 JMenuBar mbar = new JMenuBar(); pf.setJMenuBar(mbar); pf.setLayout(new BorderLayout()); //创建三个下拉式菜单并添加到菜单栏里 JMenu m1 = new JMenu("文件"); JMenu m2 = new JMenu("编辑"); JMenu m3 = new JMenu("图像"); mbar.add(m1); mbar.add(m2); mbar.add(m3); //创建菜单项并添加到“文件”菜单下 JMenuItem mi11 = new JMenuItem("新建"); JMenuItem mi12 = new JMenuItem("打开"); JMenuItem mi13 = new JMenuItem("储存"); m1.add(mi11); m1.add(mi12); m1.add(mi13); m1.add("退出");//直接添加菜单项 //获取到直接添加的菜单项并注册鼠标动作,输出菜单项标题 final JMenuItem mi14 = m1.getItem(3); mi14.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); final JMenuItem mi15 = m1.getItem(0); mi15.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { FileDialog fd=new FileDialog(pf,"photo open", FileDialog.LOAD); fd.show(); } }); //创建菜单项并添加到“编辑”菜单下 JMenuItem mi21 = new JMenuItem("还原"); JMenuItem mi22 = new JMenuItem("删除"); m2.add(mi21); m2.add(mi22); //创建菜单项并添加到“图像”菜单下 JMenuItem mi31 = new JMenuItem("放大"); JMenuItem mi32 = new JMenuItem("缩小"); JMenuItem mi33 = new JMenuItem("旋转"); m3.add(mi31); m3.add(mi32); m3.add(mi33); //创建水平分割面板 JSplitPane splitPane = new JSplitPane(); splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT); splitPane.setDividerLocation(140); pf.add(splitPane); //创建左边面板的按钮 JButton ColorBtn = new JButton("颜色"); splitPane.setLeftComponent(ColorBtn); } }
时间: 2024-10-18 11:53:25