2014-10-10 10:48:55

JavaSwing 简单模拟Mac Finder

为了方便切换,我把Panel和JFrame分开了。之前画了个用的Netbeans,但是感觉毕竟没有手写的方便扩展。

NetBeans画的:

2

代码写的:

1

MainFrame.java:

package org.javaweb.burp.test;
import java.awt.Dimension;
import javax.swing.JFrame;
/**
 *
 * @author yz
 */
public class MainFrame extends JFrame {
    private static final long serialVersionUID = 1L;
    public MainFrame() {
        initComponents();
    }
    private void initComponents() {
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        this.setTitle("测试");
        this.add(new ApplicationPanel());
        int screenWidth = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
        int screenHeight = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;
        Dimension d = this.getPreferredSize();
        this.setBounds(screenWidth / 2 - (d.width / 2), screenHeight / 2 - (d.height / 2), d.width, d.height);
        pack();
    }
    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
}

ApplicationPanel.java:

package org.javaweb.burp.test;
import java.awt.Color;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
/**
 *
 * @author yz
 */
public class ApplicationPanel extends javax.swing.JPanel {
    /**
     * Creates new form ApplicationPanel
     */
    public ApplicationPanel() {
        initComponents();
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel3 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel1.setMinimumSize(new java.awt.Dimension(200, 100));
        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 196, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 592, Short.MAX_VALUE)
        );
        jScrollPane1.setViewportView(jPanel3);
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 596, Short.MAX_VALUE)
        );
        jSplitPane1.setLeftComponent(jPanel1);
        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 777, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 596, Short.MAX_VALUE)
        );
        jSplitPane1.setRightComponent(jPanel2);
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1)
        );
        initPanel();
    }// </editor-fold>                        
    private void initPanel() {
        JLabel jLabel1 = new JLabel("主页", new ImageIcon(this.getClass().getClassLoader().getResource("images/menu_bar_home_icon.png")), JLabel.LEFT);
        JLabel jLabel2 = new JLabel("我的应用", new ImageIcon(this.getClass().getClassLoader().getResource("images/menu_bar_app_icon.png")), JLabel.LEFT);
        JLabel jLabel3 = new JLabel("任务列表", new ImageIcon(this.getClass().getClassLoader().getResource("images/menu_bar_file_icon.png")), JLabel.LEFT);
        final JLabel[] jLabelArray = new JLabel[]{jLabel1, jLabel2, jLabel3};
        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        //水平组
        GroupLayout.SequentialGroup horizontalGroup = jPanel3Layout.createSequentialGroup();
        //垂直租
        GroupLayout.SequentialGroup verticalGroup = jPanel3Layout.createSequentialGroup();
        horizontalGroup.addGap(10);//设置水平间距
        GroupLayout.ParallelGroup pg = jPanel3Layout.createParallelGroup();
        for (final JLabel jLabel : jLabelArray) {
            jLabel.setOpaque(true);//设置为不透明
            jLabel.setBackground(null);//默认去掉背景
            jLabel.addMouseListener(new java.awt.event.MouseAdapter() {
                @Override
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    //设置边框
                    for (JLabel jLabel2 : jLabelArray) {
                        if (jLabel == jLabel2) {
                            jLabel2.setBackground(new Color(204, 204, 204));
                        } else {
                            jLabel2.setBackground(null);
                        }
                    }
                }
            });
            pg.addComponent(jLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE);
            verticalGroup.addGap(5);//设置垂直间隔
            verticalGroup.addGroup(jPanel3Layout.createParallelGroup().addComponent(jLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE));
        }
        horizontalGroup.addGroup(pg);
        jPanel3Layout.setHorizontalGroup(horizontalGroup);
        jPanel3Layout.setVerticalGroup(verticalGroup);
    }
    // Variables declaration - do not modify                     
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    // End of variables declaration                   
}

代码中用到的三张小图:

menu_bar_app_icon menu_bar_file_icon menu_bar_home_icon

这几张图是我从Mac Finder上扣的有点搓,需要放到src/images目录下。

发表回复