java 人物屬性

寫代碼,可以瀏覽人物的屬性:

package day14;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class RoleSelector extends JFrame{
    /** 角色圖片標籤 **/
    private JLabel lbRoleImage;
    /** 角色屬性值 **/
    private JLabel[] lbValue;
    private String[][] strArr = {
            {"100","100","100","100","100"},
            {"200","200","200","200","200"},
            {"300","300","300","300","300"},
            {"400","400","400","400","400"},
            {"500","500","500","500","500"}
    };
    public RoleSelector(){
        JPanel pnBasic = new JPanel();
        pnBasic.setLayout(new BorderLayout());
        // 角色選擇區
        String[] strArrRoleName = {"Goddess","Assassin","Death","Garuda","Captain"};
        JComboBox cbRoleName = new JComboBox(strArrRoleName);
        cbRoleName.addActionListener(new SelectMonitor());
        pnBasic.add(cbRoleName,BorderLayout.NORTH);
        // 角色圖片區
        lbRoleImage = new JLabel();
        ImageIcon icon = new ImageIcon("fff/role/Goddess.png");
        lbRoleImage.setIcon(icon);
        pnBasic.add(lbRoleImage,BorderLayout.CENTER);
        // 角色屬性區:
        JPanel pnRoleAttr = new JPanel();
        pnRoleAttr.setLayout(new GridLayout(5,2,5,5));
              
        String[] strTitle = {"HP:","MP:","AP:","DP:","EXP:"};
        JLabel[] lbTitle = new JLabel[5];
        lbValue = new JLabel[5];
        for(int i=0;i<lbTitle.length;i++){
            lbTitle[i] = new JLabel(strTitle[i]);
            lbValue[i] = new JLabel(strArr[0][i]);
            pnRoleAttr.add(lbTitle[i]);
            pnRoleAttr.add(lbValue[i]);
        }
              
        pnBasic.add(pnRoleAttr,BorderLayout.EAST);
              
        // 綁定
        setContentPane(pnBasic);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("英雄");
        // setSize  setLocation
        setBounds(200,200,400,300);
//      pack();
        // 窗體可見
        setVisible(true);
    }
          
    // 下列列表的監聽
    private class SelectMonitor implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            JComboBox cb = (JComboBox) e.getSource();
            // 獲得選項的名字
            String strSelectName = (String) cb.getSelectedItem();
            // 獲得選項的下標
            int index = cb.getSelectedIndex();
            ImageIcon icon = new ImageIcon("fff/role/"+strSelectName+".png");
            lbRoleImage.setIcon(icon);
            pack();
            for(int i=0;i<lbValue.length;i++){
                lbValue[i].setText(strArr[index][i]);
            }
        }
    }
          
    public static void main(String[] args) {
        new RoleSelector();
    }
}

效果顯示:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章