可以在JFrame框架或着容器里添加图片好?告之下方法谢谢
import java.awt.*;
import javax.swing.*;
public class ZDsoft extends JFrame {
JTextField textName;
JPasswordField textPassword;
JButton QueDing,QuXiao;
public ZDsoft(){
super( "用户登陆界面 ");
JLabel labelName2=new JLabel( "--------------用户登陆中------------------ ");
labelName2.setForeground(Color.BLUE);
Container c=getContentPane();
JPanel panel=new JPanel();
panel.setBackground(Color.green);
panel.add(labelName2);
JLabel labelName=new JLabel( "用户名: ");
textName=new JTextField(15);
panel.add(labelName);
panel.add(textName);
JLabel labelPassword=new JLabel( " 密码: ");
textPassword=new JPasswordField(15);
panel.add(labelPassword);
panel.add(textPassword);
QueDing=new JButton( "确定 ");
QueDing.setBackground(Color.yellow);
QuXiao=new JButton( "取消 ");
QuXiao.setBackground(Color.yellow);
panel.add(QueDing);
panel.add(QuXiao);
JLabel wf=new JLabel( "版权所有 重庆*******学院 ");
wf.setForeground(Color.red);
panel.add(wf);
c.add(panel);
}
public static void main(String[] args){
ZDsoft jframe=new ZDsoft();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setSize(250,180);
jframe.setVisible(true);
}
}
上面只是我写的框架,还没的事件响应的,主要是我还没学.我现在很想知道的是如何在我所设计的框架之中添加图片??就象QQ一样,它上面都有图片...我很好奇,哪个老师帮我在我的代码上加下下图片.我会很感谢你的..
[解决办法]
Icon icon=new ImageIcon( "icon.gif/jpg ");
JLabel label=new JLabel(icon);
JFrame frame=new JFrame();
frame.add(label,BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(W,H);
frame.setVisible(true);
[解决办法]
import java.awt.*;
import javax.swing.*;
public class ZDsoft extends JFrame {
JTextField textName;
JPasswordField textPassword;
JButton QueDing,QuXiao;
public ZDsoft(){
super( "用户登陆界面 ");
Container c=getContentPane();
Icon icon=new ImageIcon( "icon.gif/jpg ");
JLabel label=new JLabel(icon);
c.add(label,BorderLayout.NORTH);
JLabel labelName2=new JLabel( "--------------用户登陆中------------------ ");
labelName2.setForeground(Color.BLUE);
JPanel panel=new JPanel();
panel.setBackground(Color.green);
panel.add(labelName2);
JLabel labelName=new JLabel( "用户名: ");
textName=new JTextField(15);
panel.add(labelName);
panel.add(textName);
JLabel labelPassword=new JLabel( " 密码: ");
textPassword=new JPasswordField(15);
panel.add(labelPassword);
panel.add(textPassword);
QueDing=new JButton( "确定 ");
QueDing.setBackground(Color.yellow);
QuXiao=new JButton( "取消 ");
QuXiao.setBackground(Color.yellow);
panel.add(QueDing);
panel.add(QuXiao);
JLabel wf=new JLabel( "版权所有 重庆*******学院 ");
wf.setForeground(Color.red);
panel.add(wf);
//panel 好象加的东西太多了吧 注意布局
c.add(panel,BorderLayout.CENTER);
}
public static void main(String[] args){
ZDsoft jframe=new ZDsoft();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setSize(250,180);
jframe.setVisible(true);
}
}
[解决办法]
把图片放在与你的.java文件同一个文件夹中。
你可以jdk文档,
类 ImageIcon:一个 Icon 接口的实现,它根据 Image 绘制 Icon
即 Icon icon=new ImageIcon( "icon.gif/jpg ");
JLabel label=new JLabel(icon);
修改:public ZDsoft(){
super( "用户登陆界面 ");
JLabel labelName2=new JLabel( "--------------用户登陆中------------------ ");
labelName2.setForeground(Color.BLUE);
JPanel panel=new JPanel();
this.getContentPane(panel);
panel.setBackground(Color.green);
panel.add(labelName2);
JLabel labelName=new JLabel( "用户名: ");
textName=new JTextField(15);
panel.add(labelName);
panel.add(textName);
JLabel labelPassword=new JLabel( " 密码: ");
textPassword=new JPasswordField(15);
panel.add(labelPassword);
panel.add(textPassword);
QueDing=new JButton( "确定 ");
QueDing.setBackground(Color.yellow);
QuXiao=new JButton( "取消 ");
QuXiao.setBackground(Color.yellow);
Icon icon=new ImageIcon( "icon.gif/jpg ");//使用指定的图像构造一个标签
JLabel label=new JLabel(icon);
panel。add(label);//将标签加入
panel.add(QueDing);
panel.add(QuXiao);
................
