带半透明菜单的记事本的图形化组件,半透明图形化,带半透明菜单的记事本的图
分享于 点击 45991 次 点评:242
带半透明菜单的记事本的图形化组件,半透明图形化,带半透明菜单的记事本的图
带半透明菜单的记事本的图形化组件
CustomMenuItemUI.java
import java.awt.*;import java.awt.image.*;import javax.swing.*;import javax.swing.plaf.*;import javax.swing.plaf.basic.*;public class CustomMenuItemUI extends BasicMenuItemUI { public static ComponentUI createUI(JComponent c) { return new CustomMenuItemUI(); } public void paint(Graphics g, JComponent comp) { // paint to the buffered image BufferedImage bufimg = new BufferedImage( comp.getWidth(), comp.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = bufimg.createGraphics(); // restore the foreground color in case the superclass needs it g2.setColor(g.getColor()); super.paint(g2,comp); // do an alpha composite Graphics2D gx = (Graphics2D) g; gx.setComposite(AlphaComposite.getInstance( AlphaComposite.SRC_OVER,0.8f)); gx.drawImage(bufimg,0,0,null); }}
CustomPopupMenuUI.java
import javax.swing.*;import javax.swing.plaf.*;import javax.swing.plaf.basic.*;public class CustomPopupMenuUI extends BasicPopupMenuUI { public static ComponentUI createUI(JComponent c) { return new CustomPopupMenuUI(); } public void installUI(JComponent c) { super.installUI(c); popupMenu.setOpaque(false); } public Popup getPopup(JPopupMenu popup, int x, int y) { Popup pp = super.getPopup(popup,x,y); JPanel panel = (JPanel)popup.getParent(); panel.setOpaque(false); return pp; }}
Notebook.java
import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;public class Notebook extends JFrame { static { UIManager.put("PopupMenuUI", "CustomPopupMenuUI"); UIManager.put("MenuItemUI", "CustomMenuItemUI"); } private static final long serialVersionUID = 1L; private JTextArea jta = new JTextArea(); private JScrollPane jsp = new JScrollPane(jta); private JPopupMenu jm = EditPopupMenu(); public static void main(String[] args) { new Notebook(); } public Notebook() { JMenu menu = EditMenu(); JMenuItem jmi2 = new JMenuItem("打开"); jmi2.setActionCommand("打开"); jmi2.addActionListener(new Mylistener()); JMenuItem jmi3 = new JMenuItem("保存"); jmi3.setActionCommand("保存"); JMenuItem jmiexit = new JMenuItem("退出"); jmiexit.setActionCommand("退出"); jmi3.addActionListener(new Mylistener()); JMenuItem jmi4 = new JMenuItem("查看帮助"); jmi4.setActionCommand("查看帮助"); jmiexit.addActionListener(new Mylistener()); jmi4.addActionListener(new Mylistener()); JMenu jm1 = new JMenu("文件"); jm1.add(jmi2); jm1.add(jmi3); jm1.addSeparator(); jm1.add(jmiexit); JMenu jm2 = new JMenu("帮助"); jm2.add(jmi4); JMenuBar jbr = new JMenuBar(); jbr.add(jm1); jbr.add(menu); jbr.add(jm2); this.add(jsp); this.add(jbr, BorderLayout.NORTH); jta.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON3) { jm.show(jta, e.getX(), e.getX()); } } }); this.setTitle("记事本"); this.setSize(600, 500); this.setLocation(200, 200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } private JMenu EditMenu() { JMenuItem jmicut, jmicopy, jmipaste, jmiSelectAll; jmicut = new JMenuItem("剪切"); jmicut.setActionCommand("剪切"); jmicut.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { jta.cut(); } }); jmicopy = new JMenuItem("复制"); jmicopy.setActionCommand("复制"); jmicopy.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { jta.copy(); } }); jmipaste = new JMenuItem("粘贴"); jmipaste.setActionCommand("粘贴"); jmipaste.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { jta.paste(); } }); jmiSelectAll = new JMenuItem("全选"); jmiSelectAll.setActionCommand("全选"); jmiSelectAll.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { jta.selectAll(); } }); JMenu menu = new JMenu("编辑"); menu.add(jmicut); menu.add(jmicopy); menu.add(jmipaste); menu.addSeparator(); menu.add(jmiSelectAll); return menu; } private JPopupMenu EditPopupMenu() { JMenuItem jmicut, jmicopy, jmipaste, jmiSelectAll; jmicut = new JMenuItem("剪切"); jmicut.setActionCommand("剪切"); jmicut.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { jta.cut(); } }); jmicopy = new JMenuItem("复制"); jmicopy.setActionCommand("复制"); jmicopy.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { jta.copy(); } }); jmipaste = new JMenuItem("粘贴"); jmipaste.setActionCommand("粘贴"); jmipaste.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { jta.paste(); } }); jmiSelectAll = new JMenuItem("全选"); jmiSelectAll.setActionCommand("全选"); jmiSelectAll.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { jta.selectAll(); } }); JPopupMenu menu = new JPopupMenu("编辑"); menu.add(jmicut); menu.add(jmicopy); menu.add(jmipaste); menu.addSeparator(); menu.add(jmiSelectAll); return menu; } class Mylistener implements ActionListener { private BufferedReader bfd = null; private BufferedWriter bfw = null; private JFileChooser jfc = null; private FileReader fd = null; private FileWriter fw = null; public void actionPerformed(ActionEvent arg0) { if (arg0.getActionCommand().equals("打开")) { jfc = new JFileChooser(); jfc.setDialogTitle("请选择要打开的文件"); jfc.showOpenDialog(null); jfc.setVisible(true); File f = jfc.getSelectedFile(); if (f != null) { String filename = f.getAbsolutePath(); try { fd = new FileReader(filename); bfd = new BufferedReader(fd); String s; StringBuilder allstring = new StringBuilder(); while ((s = bfd.readLine()) != null) { allstring.append(s); allstring.append('\n'); } jta.setText(allstring.toString()); jta.setToolTipText(filename); } catch (Exception e) { e.printStackTrace(); } finally { try { fd.close(); bfd.close(); } catch (IOException e) { e.printStackTrace(); } } } } else if (arg0.getActionCommand().equals("保存")) { // 很重要的一个组件JFileChooser jfc = new JFileChooser(); jfc.setDialogTitle("请选择要保存的路径"); jfc.showSaveDialog(null); jfc.setVisible(true); File f = jfc.getSelectedFile(); if (f != null) { String savepath = f.getAbsolutePath(); try { fw = new FileWriter(savepath); bfw = new BufferedWriter(fw); bfw.write(jta.getText()); bfw.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { bfw.close(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } } } else if (arg0.getActionCommand().equals("查看帮助")) JOptionPane.showMessageDialog(null, "请自行探索各项功能"); else if (arg0.getActionCommand().equals("退出")) System.exit(0); } }}
用户点评