1.TestFrame.java,用於繪製主面板的組件,如下是源碼: java
import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Insets; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenuBar; import javax.swing.JTextField; import javax.swing.UIManager; import javax.swing.plaf.basic.BasicButtonUI; public class TestFrame extends JFrame { private JTextField searchText = null; private JButton searchBtn = null; private JButton editBtn = null; private JButton saveBtn = null; public TestFrame() { setLookAndFeel(); init(); } /** * 爲主面板設置皮膚 */ private void setLookAndFeel() { try { UIManager .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Exception e) { e.printStackTrace(); } } /** * 增長工具欄的組件,並設置樣式 */ private JMenuBar addMenuBar() { JMenuBar menuBar = new JMenuBar(); menuBar.setLayout(new FlowLayout(40, 4, 0)); // 增長文件選擇按鈕 menuBar.add(createBtn("File", "./src/folder_up.png")); // 增長編輯按鈕 editBtn = createBtn("Edit", "./src/page_edit.png"); //editBtn.setEnabled(false); menuBar.add(editBtn); // 增長保存文件按鈕 saveBtn = createBtn("Save", "./src/folder_down.png"); //saveBtn.setEnabled(false); menuBar.add(saveBtn); // 增長查詢輸入文本框 searchText = new JTextField(); searchText.setPreferredSize(new Dimension(140, 27)); menuBar.add(searchText); // 增長查詢按鈕 searchBtn = createBtn("Search", "./src/search.png"); //searchBtn.setEnabled(false); menuBar.add(searchBtn); return menuBar; } /** * 建立工具欄按鈕 * * @author lqj * @param text * 按鈕名稱 * @param icon * 按鈕圖標所在路徑 * @return 返回添加樣式和監聽器後的按鈕 */ private JButton createBtn(String text, String icon) { JButton btn = new JButton(text, new ImageIcon(icon)); btn.setUI(new BasicButtonUI());// 恢復基本視覺效果 btn.setPreferredSize(new Dimension(80, 27));// 設置按鈕大小 btn.setContentAreaFilled(false);// 設置按鈕透明 btn.setFont(new Font("粗體", Font.PLAIN, 15));// 按鈕文本樣式 btn.setMargin(new Insets(0, 0, 0, 0));// 按鈕內容與邊框距離 btn.addMouseListener(new MyMouseListener(this)); return btn; } /** * @author lqj * 初始化主面板和工做欄內容 */ private void init() { JMenuBar menuBar = addMenuBar(); // menuBar.setBackground(Color.WHITE); menuBar.setPreferredSize(new Dimension(1400, 30)); this.setJMenuBar(menuBar); this.setSize(1320, 750); this.setLocationRelativeTo(null); this.setVisible(true); this.setTitle("TestButton"); this.setDefaultCloseOperation(EXIT_ON_CLOSE); // this.setBackground(Color.gray); } // main public static void main(String[] args) { new TestFrame(); } }
2.MyMouseListener.java,用於增長鼠標事件,源碼以下: ide
import java.awt.Color; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JButton; import javax.swing.border.Border; import javax.swing.border.EtchedBorder; public class MyMouseListener implements MouseListener { private TestFrame frame; public MyMouseListener() { // TODO Auto-generated constructor stub } public MyMouseListener(TestFrame f) { this.frame = f; } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub String btnName =((JButton)e.getSource()).getText(); if ("File".equals(btnName.trim())) { //鼠標點擊事件 } } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub JButton btn = (JButton) e.getComponent(); Border etchedBorder = new EtchedBorder(EtchedBorder.RAISED);// 設置邊框凸顯 btn.setBorder(etchedBorder); } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub JButton btn = (JButton) e.getComponent(); Border etchedBorder = new EtchedBorder(EtchedBorder.LOWERED);// 設置邊框凹顯 btn.setBorder(etchedBorder); } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub JButton btn = (JButton) e.getComponent(); btn.setForeground(new Color(0x33, 0x66, 0xcc));// 設置字體顏色 btn.setBorderPainted(true);// 顯示邊框 Border etchedBorder = new EtchedBorder(EtchedBorder.LOWERED);// 設置邊框凹顯 btn.setBorder(etchedBorder); btn.setRolloverEnabled(true); } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub JButton btn = (JButton) e.getComponent(); btn.setForeground(Color.black);// 設置字體顏色 btn.setBorderPainted(false);// 隱藏邊框 } }
最後上傳一張運行的效果圖片給你們看!但願對你們有用! 工具