⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.java

📁 利用JAVA实现了类似XP中前进后退键样式的主从按钮。
💻 JAVA
字号:
/**
 *  [Test.java] 主从按钮测试类
 *
 *
 * 创建日期:(2003-9-4)
 * @author:ONE_Fox
 */



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import subbutton.*;



public class Test extends JFrame {
    
    //界面组件-----------------------//
    private JToolBar edit = new JToolBar();
    
    private JButton jBtnNew = new JButton("  NEW  ",new ImageIcon("ICON/edit.gif"));
    private JButton jBtnMore = new JButton(">",new ImageIcon("ICON/more.gif"));
    private JButton[] jBtnOther = new JButton[5];
    
    private SubordinateButton sButton =  //主从按钮
             new SubordinateButton(jBtnNew, jBtnMore, SubordinateButton.RIGHT);
    



/**
 * 构造方法 Test()
 */
    public Test() {
        makeFace(); //界面构造
        addListener(); //事件监听
        showFace(); //面板显示
    }




/**
 * 面板制作
 */
    private void makeFace() {
        
        //演示按钮配置-------------------------------//
        jBtnNew.setFocusable(false);
        jBtnNew.setVerticalTextPosition(AbstractButton.BOTTOM);
        jBtnNew.setHorizontalTextPosition(AbstractButton.CENTER);
        
        jBtnMore.setFocusable(false);
        jBtnMore.setVerticalTextPosition(AbstractButton.BOTTOM);
        jBtnMore.setHorizontalTextPosition(AbstractButton.CENTER);
        
        
        //重新设置演示按钮的事件---------------------//
        sButton.setListener(new DefaultMouseEvent(jBtnNew, jBtnMore) {
            
            //重写 mousePressed 方法
            public void mousePressed(MouseEvent e) {
                super.mousePressed(e);
                
                //自己的代码-------------------//
                if(((JButton)e.getComponent()).equals(jBtnNew))
                    JOptionPane.showMessageDialog(null,"你点了【主】按钮");
                else
                    JOptionPane.showMessageDialog(null,"你点了【从】按钮");
            }
        });
        
        
        //其它按钮构建-------------------------------//
        for(int i = 0; i < jBtnOther.length; i++) {
            jBtnOther[i] = new JButton(" Other"+i+" ",
                                         new ImageIcon("ICON/insert.gif"));
            
            jBtnOther[i].setVerticalTextPosition(AbstractButton.BOTTOM);
            jBtnOther[i].setHorizontalTextPosition(AbstractButton.CENTER);
            
            //添加其它按钮事件为默认 DefaultOtherBTEvent -----------//
            jBtnOther[i].addMouseListener(new DefaultOtherBTEvent(jBtnOther[i]) {
                //重写 mousePressed 方法
                public void mousePressed(MouseEvent e) {
                    super.mousePressed(e);
                
                    //自己的代码-------------------//
                    JOptionPane.showMessageDialog(null,"你点了 "
                              +((JButton)e.getComponent()).getText() +" !");
                }
            });
        }
        
        
        //添加按钮到 JToolBar------------------------//
        edit.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
        edit.add(jBtnOther[0]);
        edit.add(sButton);
        for(int i = 1; i < jBtnOther.length; i++)
            edit.add(jBtnOther[i]);
        
        
        //设置其它按钮边框为 SubordinateButton.usual
        for(int i = 0; i < jBtnOther.length; i++)
            jBtnOther[i].setBorder(SubordinateButton.usual);
        
        //添加 JToolBar
        getContentPane().add(edit,BorderLayout.NORTH);
    }




/**
 * 面板显示
 */
    private void showFace() {
        setTitle("主从按钮 Demo");
        setSize(500, 400);
        
        Toolkit tmpTK = Toolkit.getDefaultToolkit();
        Dimension dime = tmpTK.getScreenSize();  //取得屏幕大小
        setLocation(dime.width/2 - 250, dime.height/2 - 200);
        show();
    }




/**
 * 方法:事件监听
 */
    private void addListener() {
        this.addWindowListener(new WindowAdapter() {  //添加窗口关闭事件
            public void windowClosing(WindowEvent e) {
                setVisible(false);
                dispose();
                System.exit(0);
            }
        });
    }




/**
 * 程序入口
 */
    public static void main(String[] args) {
        //设置 windows 风格
        try {
            UIManager.setLookAndFeel(
                     "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) {}
        
        //全局字体设置-----------------------//
        Font font1 = new Font("宋体",Font.PLAIN,12);
        Font font2 = new Font("宋体",Font.PLAIN,15);
        UIManager.put("Button.font",font1);
        UIManager.put("OptionPane.messageFont",font2);
        UIManager.put("OptionPane.buttonFont",font1);
        
        //测试启动
        new Test();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -