新手求助,有关java面板的。请各位大神解答。
在一个框架里建立两个面板,各显示两幅图,不固定面板大小:
如果我固定面板大小,就只能显示一个面板
import java.awt.*;
import javax.swing.*;
public class SKK{
public static void main (String[] args){
JFrame frame= new JFrame("啦啦啦");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int a= (int)(Math.random()*6+1);
int b= (int)(Math.random()*6+1);
int c= (int)(Math.random()*6+1);
int d= (int)(Math.random()*6+1);
JPanel panel1 =new JPanel();
//panel1.setPreferredSize(new Dimension(50,100));
panel1.setBackground(Color.green);
ImageIcon Icon1=new ImageIcon("die0"+a+".png");
JLabel label1=new JLabel(Icon1);
panel1.add(label1);
ImageIcon Icon2 =new ImageIcon("die0"+b+".png");
JLabel label2=new JLabel(Icon2);
panel1.add(label2);
JPanel panel2 =new JPanel();
//panel1.setPreferredSize(new Dimension(50,100));
panel2.setBackground(Color.red);
ImageIcon Icon3=new ImageIcon("die0"+c+".png");
JLabel label3=new JLabel(Icon3);
panel2.add(label3);
ImageIcon Icon4=new ImageIcon("die0"+d+".png");
JLabel label4=new JLabel(Icon4);
panel2.add(label4);
JPanel panel =new JPanel();
panel.setBackground(Color.pink);
panel.add(panel1);
panel.add(panel2);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}