当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《如何在面板容器中显示图像?》经验,为你挑选了1个好方法。

每个人,

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

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

我想在EAST的容器上显示我的图像

这怎么可能?

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);

}

}



1> Peter Lang..:

创建一个JLabel无文本("")并用于setIcon设置要显示的图像.

这是一个例子.

推荐阅读
Chloemw
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有