標題:
ImageIcon 類別 (五)
[打印本頁]
作者:
tonyh
時間:
2016-4-23 16:54
標題:
ImageIcon 類別 (五)
本帖最後由 tonyh 於 2016-6-14 19:26 編輯
設計一個隨機抽取撲克牌的小程式, 執行面如下.
設計導引
JFrame 的位置與大小: 100, 100, 500, 400
JLabel 的位置與大小: 80, 10, 234,351
JButton 的位置與大小: 350, 310, 100, 30
[attach]1599[/attach]
[attach]1600[/attach]
本帖隱藏的內容需要回復才可以瀏覽
作者:
李允軒
時間:
2016-4-23 17:25
本帖最後由 李允軒 於 2016-4-23 18:02 編輯
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Ch01 implements ActionListener {
private JButton bt1;
private JFrame fm;
private JLabel lb;
private ImageIcon icon_s;
private ImageIcon icon,icon1;
Ch01(){
bt1=new JButton("下一張");
bt1.addActionListener(this);
bt1.setBounds(350, 310, 100, 30);
bt1.setVisible(true);
icon_s=new ImageIcon(Ch01.class.getResource("pic/poker_icon.png"));
icon=new ImageIcon(Ch01.class.getResource("pic/poker"+(int)(Math.random()*53+1)+".png"));
lb=new JLabel(icon);
lb.setBounds(80, 10, 234,351);
fm=new JFrame("poker");
fm.setIconImage(icon_s.getImage());
fm.setBounds(100, 100, 500, 400);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb);
fm.add(bt1);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==bt1){
icon1=new ImageIcon(Ch01.class.getResource("pic/poker"+(int)(Math.random()*53+1)+".png"));
lb.setIcon(icon1);
}
}
public static void main(String[] args) {
Ch01 app=new Ch01();
}
}
複製代碼
作者:
劉得恩
時間:
2016-4-23 17:29
本帖最後由 劉得恩 於 2016-4-23 17:39 編輯
import javax.swing.*;
import java.awt.event.*;
public class Ch92 implements ActionListener
{
private JFrame f=new JFrame("23123");
private JButton next=new JButton("下一個");
private ImageIcon ii[]=new ImageIcon[54],ic;
private JLabel l;
int r[]=new int[54];
Ch92()
{
f.setVisible(true);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
ic=new ImageIcon(Ch92.class.getResource("pic/poker0.png" ));
f.setBounds(100,100,500,400);
f.setIconImage(ic.getImage());
for(int i=0;i<54;i++)
{
ii[i]=new ImageIcon(Ch92.class.getResource("pic/poker"+(i+1)+".png" ));
}
l=new JLabel( ii[ (int)(Math.random()*54) ] );
l.setBounds(80, 10, 234,351);
f.add(l);
next.setBounds(350, 310, 100, 30);
next.addActionListener(this);
f.add(next);
}
public static void main(String[] args)
{
new Ch92();
}
public void actionPerformed(ActionEvent e)
{
l.setIcon(ii[(int)(Math.random()*54)]);
}
}
複製代碼
作者:
林宇翔
時間:
2016-4-23 17:36
package ch90;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Ch90 implements ActionListener{
private JFrame fm;
private JLabel lb;
private JButton btn1;
private ImageIcon icon1;
private ImageIcon icon_s;
Ch90()
{
icon_s=new ImageIcon(Ch90.class.getResource("pic/poker_icon.png"));
icon1=new ImageIcon(Ch90.class.getResource("pic/poker"+(int)(Math.random()*52+1)+".png"));
lb.setBounds(80, 10, 234,351);
btn1=new JButton("下一張");
btn1.setBounds(350, 310, 100, 30);
btn1.addActionListener(this);
btn1.setVisible(true);
fm=new JFrame("任抽一張撲克牌");
fm.setBounds(100, 100, 500, 400);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb);
fm.add(btn1);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn1)
icon1=new ImageIcon(Ch90.class.getResource("pic/poker"+(int)(Math.random()*52+1)+".png"));
lb.setIcon(icon1);
}
public static void main(String[] args) {
Ch90 app=new Ch90();
}
}
複製代碼
作者:
張峻瑋
時間:
2016-4-23 17:38
import javax.swing.*;
import java.awt.event.*;
public class Ch92 implements ActionListener{
private JFrame fm=new JFrame("任抽一張撲克牌");
private JButton btn=new JButton("下一張");
private JLabel lb;
private ImageIcon icon[]=new ImageIcon[54],icon0;
int x=(int)(Math.random()*54);
Ch92()
{
icon0=new ImageIcon(Ch92.class.getResource("pic/poker_icon.png"));
for(int i=0;i<54;i++)
icon[i]=new ImageIcon(Ch92.class.getResource("pic/poker"+(i+1)+".png"));
lb=new JLabel(icon[x]);
lb.setBounds(80, 10, 234,351);
btn.setBounds(370, 310, 100, 30);
btn.addActionListener(this);
fm.setIconImage(icon0.getImage());
fm.setBounds(100,100,500,400);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(btn);
fm.add(lb);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn)
{
int y=(int)(Math.random()*54); //0~53
lb.setIcon(icon[y]);
}
}
public static void main(String[] args) {
Ch92 app=new Ch92();
}
}
複製代碼
作者:
張彥承
時間:
2016-6-14 21:00
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class A90 implements ActionListener
{
private JFrame fm;
private ImageIcon ii_s;
private ImageIcon ii[]=new ImageIcon[54];
private JLabel lb;
private JButton btn;
private int x=(int)(Math.random()*54);
A90()
{
ii_s=new ImageIcon(A90.class.getResource("pic/poker_icon.png"));
for(int i=0;i<54;i++)
ii[i]=new ImageIcon(A90.class.getResource("pic/poker"+(i+1)+".png"));
lb=new JLabel(ii[x]);
lb.setBounds(80, 10, 234,351);
btn=new JButton("下一張");
btn.setBounds(350, 310, 100, 30);
btn.addActionListener(this);
fm=new JFrame("隨機撲克牌");
fm.setIconImage(ii_s.getImage());
fm.setBounds(100, 100, 500, 400);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(btn);
fm.add(lb);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn)
{
x=(int)(Math.random()*54);
lb.setIcon(ii[x]);
}
}
public static void main(String[] args) {
new A90();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2