在java中改變菜單的外觀,想必只有傻瓜才會這么干吧,哈哈哈~~~你不許笑,只許我笑.因為隨便笑人家是傻瓜不是一個禮貌的孩子:) 好了,說實現思路吧 .javax.swing包中的組件都是繼承JComponent組件 JComponent組件中有一個方法setBorder(Border border) 用來設置組件的邊框 Boder是一個接口,在avax.swing.border包中
那么改變菜單的外觀就可以首先從這個來著手了.因此我們需要一個實現這個接口的類.好了,來看看這個接口的方法吧 :
void paintBorder(Component c,
???????????????? Graphics g,
???????????????? int x,
???????????????? int y,
???????????????? int width,
???????????????? int height)按指定的位置和尺寸繪制指定組件的邊框。
參數:
c - 要為其繪制邊框的組件
g - 繪制的圖形
x - 所繪制邊框的 x 坐標位置
y - 所繪制邊框的 y 坐標位置
width - 所繪制邊框的寬度
height - 所繪制邊框的高度
//附注:邊框就是在這個方法中給畫出來的
--------------------------------------------------------------------------------
Insets getBorderInsets(Component c)返回該邊框的 insets。
//附注一下:該類是容器邊界的表示形式。它指定容器必須在其各個邊緣留出的空間。這個空間可以是邊
//界、空白空間或標題。
參數:
c - 要應用此邊框 insets 值的組件
--------------------------------------------------------------------------------
isBorderOpaque
boolean isBorderOpaque()返回此邊框是否透明。如果邊框為不透明,則在繪制它時將用自身的背景來填充。
首先來看看效果圖: 請注意右上角是橢圓的噢,這個就暗示了我們可以把菜單弄成任意形狀,只要你愿意!
按照這種思路,同樣的,你也可以改變右鍵彈出菜單的外觀

接口的實現類? 至于畫圖用到的方法,詳見JAVA API.
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.border.*;
public class MyBorder implements Border
{
?//設置邊框四周留出的空間
? protected int m_w=6;??
? protected int m_h=6;
? protected int m_left=16;
//設置邊框的前景色跟背景色
? protected Color m_topColor = Color.white;
? protected Color m_bottomColor = Color.gray;
? public MyBorder() {
??? m_w=6;
??? m_h=6;
??? m_left=16;
? }
? public MyBorder(int w, int h) {
??? m_w=w;
??? m_h=h;
? }
? public MyBorder(int w, int h, Color topColor,
?? Color bottomColor) {
??? m_w=w;
??? m_h=h;
??? m_topColor = topColor;
??? m_bottomColor = bottomColor;
? }
? public Insets getBorderInsets(Component c) {
??? return new Insets(m_h, m_left, m_h, m_w);
? }
? public boolean isBorderOpaque() { return true; }
? public void paintBorder(Component c, Graphics gd,
?? int x, int y, int w, int h) {
??? Graphics2D g = (Graphics2D) gd;
??? w--;
??? h--;
??? g.setColor(m_topColor);
??? g.drawLine(x, y+h, x, y+m_h);
??? g.drawArc(x, y, 2*m_w, 2*m_h, 180, -90);
??? g.drawLine(x, y, x+w-m_w, y);
??? g.drawArc(x+w-2*m_w, y, 2*m_w, 2*m_h, 90, -90);
??? int stringHeitht = y+h-5;
??? Point2D.Float? p1 = new Point2D.Float(x,y+h);
??? Point2D.Float p2 = new Point2D.Float(x,y);
??? GradientPaint gradient = new GradientPaint(p1,Color.blue,p2,Color.gray,false);
??? //g.setColor(Color.YELLOW);
??? //g.drawRect();
??? g.setPaint(gradient);
??? g.fillRect(x,y,x+m_left,y+h);
??? g.setColor(Color.GRAY);
??? g.drawString("瓜",((x+m_left)-g.getFontMetrics().charWidth('傻'))/2,stringHeitht);
??? int simpleFontHeitht = g.getFontMetrics().getHeight();
??? stringHeitht-=simpleFontHeitht;
??? g.drawString("傻",((x+m_left)-g.getFontMetrics().charWidth('傻'))/2,stringHeitht);
??? g.setColor(m_bottomColor);
??? g.drawLine(x+w, y+m_h, x+w, y+h);
?? // g.drawArc(x+w-2*m_w, y+h-2*m_h, 2*m_w, 2*m_h, 0, -90);
?? g.drawLine(x, y+h, x+w, y+h);
??? //g.drawArc(x, y+h-2*m_h, 2*m_w, 2*m_h, -90, -90);
? }
}
剩下的就是把這個邊框給它裝上去了.在JMenu中有一個方法getPopupMenu()得到彈出菜單對象JPopupMenu;
然后調用這個JPopupMenu對象的setBorder方法就行了.下面是完整的測試程序
import java.awt.Toolkit;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Dimension;
public class Application1 {
? boolean packFrame = false;
? /**
?? * Construct and show the application.
?? */
? public Application1() {
??? MenuFaces frame = new MenuFaces();
??? // Validate frames that have preset sizes
??? // Pack frames that have useful preferred size info, e.g. from their layout
??? if (packFrame) {
????? frame.pack();
??? }
??? else {
????? frame.validate();
??? }
??? // Center the window
??? Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
??? Dimension frameSize = frame.getSize();
??? if (frameSize.height > screenSize.height) {
????? frameSize.height = screenSize.height;
??? }
??? if (frameSize.width > screenSize.width) {
????? frameSize.width = screenSize.width;
??? }
??? frame.setLocation( (screenSize.width - frameSize.width) / 2,
????????????????????? (screenSize.height - frameSize.height) / 2);
??? frame.setVisible(true);
? }
? /**
?? * Application entry point.
?? *
?? * @param args String[]
?? */
? public static void main(String[] args) {
??? SwingUtilities.invokeLater(new Runnable() {
????? public void run() {
??????? try {
????????? UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
??????? }
??????? catch (Exception exception) {
????????? exception.printStackTrace();
??????? }
??????? new Application1();
????? }
??? });
? }
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MenuFaces
??? extends JFrame {
? JPanel contentPane;
? BorderLayout borderLayout1 = new BorderLayout();
? JMenuBar jMenuBar1 = new JMenuBar();
? JMenu jMenuFile = new JMenu();
? JMenuItem jMenuFileExit = new JMenuItem();
? JMenuItem jMenuItem1 = new JMenuItem();
? JMenuItem jMenuItem2 = new JMenuItem();
? JMenuItem jMenuItem3 = new JMenuItem();
? JMenu jMenu1 = new JMenu();
? JMenuItem jMenuItem4 = new JMenuItem();
? public MenuFaces() {
??? try {
????? setDefaultCloseOperation(EXIT_ON_CLOSE);
????? jbInit();
??? }
??? catch (Exception exception) {
????? exception.printStackTrace();
??? }
? }
? /**
?? * Component initialization.
?? *
?? * @throws java.lang.Exception
?? */
? private void jbInit() throws Exception {
??? contentPane = (JPanel) getContentPane();
??? contentPane.setLayout(borderLayout1);
??? setSize(new Dimension(400, 300));
??? setTitle("Frame Title");
??? jMenuFile.setText("File");
??? jMenuFileExit.setText("Exit");
??? jMenuFileExit.addActionListener(new MenuFaces_jMenuFileExit_ActionAdapter(this));
??? jMenuItem1.setText("open");
??? jMenuItem2.setText("save");
??? jMenuItem3.setText("save as");
??? jMenu1.setText("other");
??? jMenuItem4.setText("tt");
??? jMenuBar1.add(jMenuFile);
??? jMenuBar1.add(jMenu1);
??? jMenuFile.add(jMenuItem3);
??? jMenuFile.add(jMenuItem2);
??? jMenuFile.add(jMenuItem1);
??? jMenuFile.add(jMenuFileExit);
??? jMenu1.add(jMenuItem4);
??? JPopupMenu tt = jMenuFile.getPopupMenu();
??? MyBorder myBorder = new MyBorder();
??? tt.setBorder(myBorder);
??? setJMenuBar(jMenuBar1);
? }
? /**
?? * File | Exit action performed.
?? *
?? * @param actionEvent ActionEvent
?? */
? void jMenuFileExit_actionPerformed(ActionEvent actionEvent) {
??? System.exit(0);
? }
}
class MenuFaces_jMenuFileExit_ActionAdapter
??? implements ActionListener {
? MenuFaces adaptee;
? MenuFaces_jMenuFileExit_ActionAdapter(MenuFaces adaptee) {
??? this.adaptee = adaptee;
? }
? public void actionPerformed(ActionEvent actionEvent) {
??? adaptee.jMenuFileExit_actionPerformed(actionEvent);
? }
}
歡迎加入QQ群:30406099?
?