关于gui的问题

朝目标努力的苍白 2018-05-19 01:37:47
萌新求教如何在gui中插入多张图片,我想写一个抽卡程序,分为十连和单抽,单抽已经写完了,十连的方法我设想是产生十个随机数,存入数组中,然后通过产生的数组来画相关的图片,求问如何按一下按钮画出十张图片,以下是我写的画图的方法

public class canva1 extends Canvas implements ActionListener{
Image img[];
int pg=0;
int f1=0;
int arry[] = new int[10];
canva1(){

img = new Image[200];

Toolkit t1=getToolkit();
for(int i=0;i<=199;i++){
img[i]=t1.getImage("photo/0"+i+".jpg");

}

}

public void actionPerformed(ActionEvent e) {
if(e.getSource()==Panel.but1) {
//Panel panel1 = new Panel();
f1=1;
Random rand = new Random();
pg = (int)(Math.random()*200);
System.out.println(pg);
repaint();

}
if(e.getSource()==Panel.but2) {
f1=2;
for(int i=0;i<10;i++) {
arry[i]=(int)(Math.random()*200);
System.out.print(" "+arry[i]);
}


}
}
public void paint(Graphics g) {
if(f1 == 1) {
g.drawImage(img[pg], 30, 30, 88, 85,this);
}
if(f1==2) {

}
}
}
...全文
1024 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
遇见1995 2018-06-29
  • 打赏
  • 举报
回复
引用 7 楼 我自横刀香甜笑的回复:
[quote=引用 6 楼 weixin_38500325 的回复:]
如果是用eclipse的话可以用一下一个插件做界面设计,叫windowbuilder.
参见:https://blog.csdn.net/jason0539/article/details/21219043

他是要做动态的。。。。[/quote] ⊙∀⊙!这就尴尬了,求大神答复。
  • 打赏
  • 举报
回复
引用 6 楼 weixin_38500325 的回复:
如果是用eclipse的话可以用一下一个插件做界面设计,叫windowbuilder.
参见:https://blog.csdn.net/jason0539/article/details/21219043

他是要做动态的。。。。
遇见1995 2018-06-27
  • 打赏
  • 举报
回复
如果是用eclipse的话可以用一下一个插件做界面设计,叫windowbuilder.
参见:https://blog.csdn.net/jason0539/article/details/21219043
  • 打赏
  • 举报
回复

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AWTFrame extends JFrame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3948792087186265701L;
	private JPanel contentPane;
	Canva1 canvas = new Canva1();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AWTFrame frame = new AWTFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AWTFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 794, 638);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		canvas.setBounds(0, 39, 778, 551);
		contentPane.add(canvas);
		Button btn1 = new Button("btn1");
		btn1.addActionListener(this);
		btn1.setBounds(0, 10, 76, 23);
		contentPane.add(btn1);
		
		Button btn2 = new Button("btn2");
		btn2.addActionListener(this);
		btn2.setBounds(94, 10, 76, 23);
		contentPane.add(btn2);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() instanceof Button) {
			Button btn = (Button) e.getSource();
			System.out.println("click btn : " + btn.getName() + " label : " + btn.getLabel());
			if (btn.getLabel().equalsIgnoreCase("btn1")) {
				System.out.println("click btn1");
				// Panel panel1 = new Panel();
//				f1 = 1;
//				this.paint(this.getGraphics());
//				canvas.repaint();
				canvas.drawImages(1);
//				repaint();
			} else if (btn.getLabel().equalsIgnoreCase(
					"btn2")) {
				System.out.println("click btn2");
				canvas.drawImages(2);
//				f1 = 2;

			}
		}
		// if (e.getSource() == Panel.but2) {

		// }
		//
		// }
	}
}

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Canva1 extends Canvas {
	/**
	 * 
	 */
	private static final long serialVersionUID = 321971567505371826L;
	Image img[];
	int pg = 0;
	int f1 = 0;
	int arry[] = new int[10];

	public Canva1() {
		img = new Image[10];

		Toolkit t1 = getToolkit();
		for (int i = 0; i < 10; i++) {
			img[i] = t1.getImage("images/0" + i + ".jpg");

		}

	}
	@Override  
    public void repaint() {  
        Graphics g = this.getGraphics();  
        update(g);  
    }  
  
    @Override  
	public void paint(Graphics g) {
    	g.clearRect(0, 0, this.WIDTH, this.HEIGHT);
		Random rand = new Random();
    	if (f1 == 1) {
    		pg = rand.nextInt(10);
    		System.out.println(pg);
			g.drawImage(img[pg], 30, 30, 80, 80, this);
		}
		if (f1 == 2) {
			List<Integer> indexs = new ArrayList<Integer>();
			int startx = 30;
			int starty = 30;
			for(int i = 0; i < 10; i++){
	    		pg = rand.nextInt(10);
				while(indexs.contains(pg)){
		    		pg = rand.nextInt(10);
				}
				indexs.add(pg);
	    		System.out.println(pg);
				if(i< 5){
					System.out.println("1 row x:" + (10 + (i * 80)) );
					g.drawImage(img[pg], i * 80 + startx + 100, starty, 80, 80, this);
				}
				else{
					System.out.println("*************************");
					System.out.println("2 row x:" + (10 + (i-5) * 80 + 15));
					g.drawImage(img[pg], startx + (i-5) * 80 + 100, starty + 180, 80, 80, this);
				}
			}

		}
	}
    
    public void drawImages(int flag){
		this.repaint();
    }

}
这是分别画一个图和两层随机10个图的效果。参考下吧
weixin_41404782 2018-06-19
  • 打赏
  • 举报
回复
自己控制y坐标啊。 第一层: y1 第二层: y2 = y1 + 层高 + 层间距
qq_29152633 2018-05-31
  • 打赏
  • 举报
回复
java的gui一般不怎么用了,你可以在课余看看这个gui
  • 打赏
  • 举报
回复
引用 1 楼 qq_41592230 的回复:
用10个panel
我指的是在一个页面画十幅画,最好是分两层一行五个,刚刚我试着用for循环写出来了,但是现在问题是不知道怎么分两层了
qq_41592230 2018-05-19
  • 打赏
  • 举报
回复
用10个panel

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧