如何在面板容器中显示图像?

时间:2010-03-08 10:10:14

标签: java swing

每个人,

以下是我的代码,显示4个面板

一个在北方,......西,南

我想在EAST处显示am at image

怎么可能?

public class ImageProcessor extends JApplet {



JPanel panel1,panel2,panel3,panel4,panel5;
JTextField nameTxt,addTxt,phoneTxt,emailTxt;
JButton capture,download,cancle,sendEmail;
JLabel head,name,add,phone,email;


//function to align components using gridBagLayOut..
    private GridBagConstraints getConstraints(int gridx, int gridy,int gridwidth, int gridheight, int anchor)
    {
        GridBagConstraints c =new GridBagConstraints();
        c.insets = new Insets(10,10,10,10);
        c.ipadx = 0;
        c.ipady = 0;
        c.gridx = gridx;
        c.gridy = gridy;
        c.gridwidth = gridwidth;
        c.gridheight = gridheight;
        c.anchor = anchor;
        return c;
    }
//ends here...



public void init() {


    panel1 = new JPanel();
    panel2 = new JPanel();
    panel3 = new JPanel();
    panel4 = new JPanel();
    panel5 = new JPanel();

    nameTxt = new JTextField(20);
    addTxt = new JTextField(20);
    phoneTxt = new JTextField(20);
    emailTxt = new JTextField(20);

    capture = new JButton("capture");
    download = new JButton("download");
    sendEmail = new JButton("sendEmail");

    head = new JLabel("BUSINESS CARD READER");
    name = new JLabel("NAME:");
    add = new JLabel("ADDRESS:");
    phone = new JLabel("PHONE:");
    email = new JLabel("EMAIL:");

    Container myPane = getContentPane();
    myPane.setLayout(new BorderLayout());

    panel1.setLayout(new BorderLayout());



    panel2.setLayout(new GridBagLayout());
    panel2.add(head,getConstraints(0,0,1,1,GridBagConstraints.CENTER));

    panel3.setLayout(new FlowLayout());
    panel3.add(capture);
    panel3.add(download);
    panel3.add(sendEmail);

    panel4.setLayout(new GridBagLayout());

    panel4.add(name,getConstraints(0,0,1,1,GridBagConstraints.CENTER));
    panel4.add(nameTxt,getConstraints(1,0,1,1,GridBagConstraints.CENTER));

    panel4.add(add,getConstraints(0,1,1,1,GridBagConstraints.CENTER));
    panel4.add(addTxt,getConstraints(1,1,1,1,GridBagConstraints.CENTER));

    panel4.add(phone,getConstraints(0,2,1,1,GridBagConstraints.CENTER));
    panel4.add(phoneTxt,getConstraints(1,2,1,1,GridBagConstraints.CENTER));

    panel4.add(email,getConstraints(0,3,1,1,GridBagConstraints.CENTER));
    panel4.add(emailTxt,getConstraints(1,3,1,1,GridBagConstraints.CENTER));


    panel1.add(panel2,BorderLayout.NORTH);
    panel1.add(panel3,BorderLayout.SOUTH);
    panel1.add(panel4,BorderLayout.WEST);
    panel1.add(panel5,BorderLayout.EAST);
    setSize(500,500);
    myPane.add(panel1,BorderLayout.CENTER);


}
public void start(){
    this.setSize(800,500);

}

}

2 个答案:

答案 0 :(得分:6)

创建没有文字JLabel的{​​{1}}并使用setIcon设置要显示的图片。

Here就是一个例子。

答案 1 :(得分:1)

JButtonJLabel上的图片添加到panel5

JButton buttonForPicture = new JButton();
buttonForPicture.setBorder(new EmptyBorder(0, 0, 0, 0));
buttonForPicture.setOpaque(false);
buttonForPicture.setIcon(new ImageIcon(imageFilePath));
panel5.add(buttonForPicture);