千里冰封
          JAVA 濃香四溢
          posts - 151,comments - 2801,trackbacks - 0

                 YOYOPlayer的設(shè)置里面,開始每個單獨的設(shè)置用的是JtabbedPane來分隔,后來覺得不太好看,于是就模仿千千靜聽的設(shè)置一樣,做了一個ListBar的效果,把所有設(shè)置選項的標(biāo)頭放在左邊,并加了一些效果,這樣看上去更漂亮一些,呵呵.

                 以前的設(shè)置界面:


                 現(xiàn)在的設(shè)置界面:


                 主要運(yùn)用的還是swing的優(yōu)秀的架構(gòu),它可插拔的外觀,我重寫的是JlistUI,并不需要重寫paintComponent,這樣比重寫painComponent方法方便多了,并且可以隨時通過setUI把外觀設(shè)回來.

                 代碼如下:

          /*
           * ListBar.java
           *
           * Created on 2008年1月5日, 下午10:49
           
          */
          package com.hadeslee.yoyoplayer.setting;

          import com.hadeslee.yoyoplayer.util.Util;
          import java.awt.BorderLayout;
          import java.awt.Color;
          import java.awt.Component;
          import java.awt.Dimension;
          import java.awt.Graphics;
          import java.awt.Image;
          import java.awt.Rectangle;
          import java.awt.event.MouseEvent;
          import java.awt.event.MouseListener;
          import java.awt.event.MouseMotionListener;
          import java.io.IOException;
          import java.util.HashMap;
          import java.util.Map;
          import java.util.Vector;
          import java.util.logging.Level;
          import java.util.logging.Logger;
          import javax.imageio.ImageIO;
          import javax.swing.JList;
          import javax.swing.JPanel;
          import javax.swing.JScrollPane;
          import javax.swing.ListCellRenderer;
          import javax.swing.ListModel;
          import javax.swing.ListSelectionModel;
          import javax.swing.SwingUtilities;
          import javax.swing.event.ListSelectionEvent;
          import javax.swing.event.ListSelectionListener;
          import javax.swing.plaf.basic.BasicListUI;

          /**
           *
           * 
          @author  Administrator
           
          */
          public class ListBar extends javax.swing.JPanel implements MouseListener, MouseMotionListener {

              
          private Vector<String> names;//存放列表里面的名字
              private Map<String, Component> map;//用于存放名字對應(yīng)的內(nèi)容面板的映射
              private int onIndex = -1;//鼠標(biāo)所在的對應(yīng)列表的下標(biāo)
              /** Creates new form ListBar */
              
          public ListBar() {
                  initComponents();
                  initOther();
              }

              
          private void initOther() {
                  map 
          = new HashMap<String, Component>();
                  names 
          = new Vector<String>();
                  list.setUI(
          new YOYOListUI());
                  list.setFixedCellHeight(
          25);
                  list.addMouseListener(
          this);
                  list.addListSelectionListener(
          new ListSelectionListener() {

                      
          public void valueChanged(ListSelectionEvent e) {
                          
          if (!e.getValueIsAdjusting()) {
                              updateSelected();
                          }
                      }
                  });
                  list.addMouseMotionListener(
          this);
                  list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);


              }

              
          public int getBarComponentCount() {
                  
          return map.size();
              }

              
          public Component getBarComponent(int index) {
                  
          return map.get(names.get(index));
              }

              
          public Component getComponent(String name) {
                  
          return map.get(name);
              }

              
          private void updateSelected() {
                  Object value 
          = list.getSelectedValue();
                  
          if (value != null) {
                      String name 
          = value.toString();
                      Component com 
          = map.get(name);
                      
          if (com != null) {
                          content.removeAll();
                          content.add(com, BorderLayout.CENTER);
          //                content.revalidate();
                          SwingUtilities.updateComponentTreeUI(content);
                      }
                  }
              }

              
          public void addComponent(String name, Component com) {
                  
          if (!names.contains(name)) {
                      names.add(name);
                  }
                  Dimension size 
          = com.getPreferredSize();
                  Dimension me 
          = content.getPreferredSize();
                  me.width 
          = size.width > me.width ? size.width : me.width;
                  me.height 
          = size.height > me.height ? size.height : me.height;
                  content.setPreferredSize(me);
                  map.put(name, com);
                  list.setListData(names);
              }

              
          public void setSelectedComponent(String name) {
                  Component com 
          = map.get(name);
                  
          if (com != null) {
                      list.setSelectedValue(name, 
          true);
                      content.removeAll();
                      content.add(com, BorderLayout.CENTER);
          //                content.revalidate();
                      SwingUtilities.updateComponentTreeUI(content);
                  }
              }

              
          public void removeComponent(String name) {
              }

              
          public void removeComponent(Component com) {

              }

              
          private class YOYOListUI extends BasicListUI {

                  
          private Image line,  select;

                  
          public YOYOListUI() {
                      
          try {
                          line 
          = ImageIO.read(this.getClass().getResource("line.png"));
                          select 
          = ImageIO.read(this.getClass().getResource("select.png"));
                      } 
          catch (IOException ex) {
                          Logger.getLogger(ListBar.
          class.getName()).log(Level.SEVERE, null, ex);
                      }
                  }

                  @Override
                  
          protected void paintCell(Graphics g, int row, Rectangle rowBounds, ListCellRenderer cellRenderer, ListModel dataModel, ListSelectionModel selModel, int leadIndex) {
                      
          int width = rowBounds.width;
                      
          int height = rowBounds.height;
                      
          int x = rowBounds.x;
                      
          int y = rowBounds.y;
                      g.translate(x, y);
                      String s 
          = dataModel.getElementAt(row).toString();
                      g.setColor(Color.BLACK);
                      Util.drawString(g, s, (width 
          - Util.getStringWidth(s, g)) / 2, (height - Util.getStringHeight(s, g)) / 2);
                      
          if (selModel.isSelectedIndex(row)) {
                          g.setColor(
          new Color(48106198));
                          g.fillRect(
          00, rowBounds.width, rowBounds.height);
                          g.setColor(Color.WHITE);
                          Util.drawString(g, s, (width 
          - Util.getStringWidth(s, g)) / 2, (height - Util.getStringHeight(s, g)) / 2);
                          g.drawImage(line, (width 
          - line.getWidth(list)) / 2, height - 1, list);
                          g.drawImage(select, 
          2, (height - select.getHeight(list)) / 2, list);
                          g.setColor(
          new Color(120,149,226));
                          g.drawRect(
          00, width-1, height-1);
                          g.setColor(
          new Color(192192255));
                          g.drawLine(
          0, height-1, width, height-1);
                      } 
          else {
                          g.drawImage(line, (width 
          - line.getWidth(list)) / 2, height - 1, list);
                      }
                      
          if (row == onIndex) {
                          g.setColor(
          new Color(151180226));
                          g.fillRect(
          00, rowBounds.width, rowBounds.height);
                          g.setColor(Color.WHITE);
                          Util.drawString(g, s, (width 
          - Util.getStringWidth(s, g)) / 2, (height - Util.getStringHeight(s, g)) / 2);
                          g.setColor(
          new Color(120149226));
                          g.drawLine(
          0, height-1, width, height-1);
                      }
                      g.translate(
          0 - x, 0 - y);
                  }
              }

              
          public void mouseClicked(MouseEvent e) {
              }

              
          public void mousePressed(MouseEvent e) {
              }

              
          public void mouseReleased(MouseEvent e) {

              }

              
          public void mouseEntered(MouseEvent e) {
              }

              
          public void mouseExited(MouseEvent e) {
                  onIndex 
          = -1;
                  list.repaint();
              }

              
          public void mouseDragged(MouseEvent e) {
              }

              
          public void mouseMoved(MouseEvent e) {
                  onIndex 
          = list.locationToIndex(e.getPoint());
                  list.repaint();
              }

              
          /** This method is called from within the constructor to
               * initialize the form.
               * WARNING: Do NOT modify this code. The content of this method is
               * always regenerated by the Form Editor.
               
          */
              
          // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
              private void initComponents() {

                  jScrollPane1 
          = new javax.swing.JScrollPane();
                  list 
          = new javax.swing.JList();
                  content 
          = new javax.swing.JPanel();

                  jScrollPane1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

                  list.setModel(
          new javax.swing.AbstractListModel() {
                      String[] strings 
          = { "Item 1""Item 2""Item 3""Item 4""Item 5" };
                      
          public int getSize() { return strings.length; }
                      
          public Object getElementAt(int i) { return strings[i]; }
                  });
                  jScrollPane1.setViewportView(list);

                  content.setBorder(javax.swing.BorderFactory.createEtchedBorder());
                  content.setLayout(
          new java.awt.BorderLayout());

                  javax.swing.GroupLayout layout 
          = new javax.swing.GroupLayout(this);
                  
          this.setLayout(layout);
                  layout.setHorizontalGroup(
                      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                      .addGroup(layout.createSequentialGroup()
                          .addContainerGap()
                          .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 
          105, javax.swing.GroupLayout.PREFERRED_SIZE)
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                          .addComponent(content, javax.swing.GroupLayout.DEFAULT_SIZE, 
          520, Short.MAX_VALUE)
                          .addContainerGap())
                  );
                  layout.setVerticalGroup(
                      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                          .addContainerGap()
                          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                              .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 
          396, Short.MAX_VALUE)
                              .addComponent(content, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 
          396, Short.MAX_VALUE))
                          .addContainerGap())
                  );
              }
          // </editor-fold>                        
              
          // Variables declaration - do not modify                     
              private javax.swing.JPanel content;
              
          private javax.swing.JScrollPane jScrollPane1;
              
          private javax.swing.JList list;
              
          // End of variables declaration                   
          }

           




          盡管千里冰封
          依然擁有晴空

          你我共同品味JAVA的濃香.
          posted on 2008-01-16 13:12 千里冰封 閱讀(3105) 評論(2)  編輯  收藏 所屬分類: JAVASE

          FeedBack:
          # re: YOYOPlayer開發(fā)手記(六)ListBar的實現(xiàn)
          2008-01-16 13:24 | BeanSoft
          Good work!  回復(fù)  更多評論
            
          # re: YOYOPlayer開發(fā)手記(六)ListBar的實現(xiàn)
          2008-01-17 11:31 | wangdei
          我這個網(wǎng)站 http://www.yaonba.com 能不能也搞成你那樣的啊?  回復(fù)  更多評論
            
          主站蜘蛛池模板: 贵阳市| 昌吉市| 泽普县| 上林县| 和静县| 巴彦淖尔市| 竹溪县| 正阳县| 武定县| 怀柔区| 昌平区| 深圳市| 龙州县| 尼玛县| 武定县| 浪卡子县| 彩票| 锦屏县| 安岳县| 湘乡市| 南通市| 高陵县| 栾城县| 大理市| 商河县| 锡林浩特市| 凤阳县| 中卫市| 株洲市| 承德县| 滕州市| 隆林| 东丰县| 日土县| 容城县| 松江区| 五原县| 西峡县| 双牌县| 什邡市| 永德县|