用java自己寫控制

來源:互聯網
上載者:User

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

//顏色選擇下拉框示範

public class IconComboBoxDemo extends JFrame{

 JLabel iconLabel=null;  //用來響應列表框選擇的變化
 JComboBox iconComboBox=null;  //定製的選擇下拉框

    public IconComboBoxDemo(){

      //定義Object二維數組,用於初始化下拉框,參數依次為表徵圖,顯示文本,提示文本
      Object[][] obj={
       {new ImageIcon("1.gif"),"旅遊","提供旅遊的最新資訊"},
       {new ImageIcon("2.gif"),"音樂","提供最新的音樂資訊,古典的、流行的..."},
       {new ImageIcon("3.gif"),"聊天","與朋友聊天"},
       {new ImageIcon("4.gif"),"影視","影視娛樂"},
       {new ImageIcon("5.gif"),"家居","家居世界"},
      };

      //初始化下拉框
      iconComboBox = new JComboBox();
      iconComboBox.setMaximumRowCount(3);  //設定最大可視行數
      iconComboBox.setRenderer(new IconRenderer()); //設定單元繪製器
      for (int i=0;i<obj.length;i++){   //增加數組中的所有元素到下拉框中
        iconComboBox.addItem(obj[i]);
      }


      //初始化iconLabel資訊
      iconLabel = new JLabel();

      //下拉框事件處理,用匿名類實現
      iconComboBox.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent evt){  //處理事件
         Object[] obj = (Object[])iconComboBox.getSelectedItem();  //得到選擇的內容,此處為一維數組
         iconLabel.setIcon((Icon)obj[0]);  //設定iconLabel的表徵圖
         iconLabel.setText(obj[1].toString());  //設定iconLabel的文本
       }
      });

      //增加組件到主表單上
      this.getContentPane().setLayout(new BorderLayout());  //設定布局管理器
      this.getContentPane().add(iconComboBox,BorderLayout.NORTH); //在上方增加下拉框
      this.getContentPane().add(iconLabel,BorderLayout.CENTER);  //在中間增加iconLabel,用於響應選擇的變化
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //關閉視窗時退出程式
      this.setSize(350,260);  //設定視窗尺寸
      this.setVisible(true);  //顯示視窗
    }

    public static void main(String[] args){
      new IconComboBoxDemo();
    }
}

import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

//帶表徵圖下拉框的單元繪製器,從JLabel類擴充,實現ListCellRenderer介面

public class IconRenderer extends JLabel implements ListCellRenderer{

   public Component getListCellRendererComponent(JList list, Object obj, int row, boolean sel, boolean hasFocus) {
      Object[] cell = (Object[])obj;   //得到行的參數
      setIcon((Icon)cell[0]);  //設定表徵圖
      setText(cell[1].toString()); //設定文本
      setToolTipText(cell[2].toString());  //設定提示文本
      setBorder(new LineBorder(Color.WHITE)); //設定邊界
      if (sel){
        setForeground(Color.MAGENTA);  //如果選中了,設定文本顏色為品紅色
      }
      else{
        setForeground(list.getForeground()); //如果未選中,設定文本顏色為預設色
      }
      return this;
   }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.