/***************************************** 展示JLabel *************************************************/ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ui21 extends JFrame implements ActionListener { Container c; JButton bot1=new JButton("文字"); JButton bot2=new JButton("圖形"); JButton bot0=new JButton("結束"); ImageIcon icon1; JLabel lab; String[] show=new String[3]; int cpcnt=0,gpcnt=0;; public ui21() { super("JLabel展示"); int i; c=getContentPane(); icon1 = new ImageIcon("bridge/0"+gpcnt+".GIF"); c.setLayout(new FlowLayout(FlowLayout.CENTER)); //lab=new JLabel("目前沒有東西!",icon1,JLabel.CENTER); lab=new JLabel("目前沒有東西!"); show[0]=new String("這是招牌一"); show[1]=new String("這是招牌二"); show[2]=new String("這是招牌三"); c.add(bot0); c.add(bot1); c.add(bot2); c.add(lab); bot0.addActionListener(this); bot1.addActionListener(this); bot2.addActionListener(this); setSize(400,400); setVisible(true); } public void paint(Graphics g) { super.paint(g); } /***按鈕事件的傾聽方法****/ public void actionPerformed(ActionEvent e) //按鈕事件的處理方法 { if (e.getSource()==bot1) { cpcnt=(cpcnt+1)%3; lab.setText(show[cpcnt]); } else if (e.getSource()==bot2) { gpcnt=(gpcnt+1)%9+1; icon1 = new ImageIcon("bridge/0"+gpcnt+".GIF"); //System.out.println("bridge/0"+gpcnt+".GIF"); lab.setIcon(icon1); } else if (e.getSource()==bot0) { System.exit(0); } repaint(); } public static void main(String args[]) //程式起點 { ui21 app=new ui21(); //畫圖 //處理視窗關閉要求 app.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0);} }); } }