可以利用标签组件来设置。
具体方法如下:
利用标签组件来设置,具体代码如下:
JPanelpnlMain=new JPanel()//创建面板pnlMain。
getContentPane().add(pnlMain)//将pnlMain设置为主面板。
Iconi=new ImageIcon("背景.jpg")/*用源图片“背景.jpg”构造一个ImageIcon对象i,需要注意如果图片的路径使用的是相对路径,则图片文件必须放在类文件所在文件夹或项目的根文件夹中,否则图片的路径必须用绝对路径。*/
JLabellblLogo = new JLabel(i)//用指定的图片构造标签对象lb
this.getLayeredPane().add(lb, new Integer(Integer.MIN_VALUE))
//把标签放在第二层JlayerPane上。
lb.setBounds(0, 0,ii.getIconWidth(),i.getIconHeight())
//设置标签的尺寸,即背景图象的大小。
getConentPane().setOpaque(false)/*把内容面板设置为透明,这样整个框架的背景就不再是内容面板的背景色,而是第二层中标签的图像。*/
pnlMain.add(lb)//将标签添加到主面板pnlMain中。
用java设计一个简单的界面可以参考如下实例:
import javax.swing.JFrame//框架import javax.swing.JPanel//面板
import javax.swing.JButton//按钮
import javax.swing.JLabel//标签
import javax.swing.JTextField//文本框
import java.awt.Font//字体
import java.awt.Color//颜色
import javax.swing.JPasswordField//密码框
import java.awt.event.ActionListener//事件监听
import java.awt.event.ActionEvent//事件处理
import javax.swing.JOptionPane//消息窗口public class UserLogIn extends JFrame{
public JPanel pnluser
public JLabel lbluserLogIn
public JLabel lbluserName
public JLabel lbluserPWD
public JTextField txtName
public JPasswordField pwdPwd
public JButton btnSub
public JButton btnReset
public UserLogIn(){
pnluser = new JPanel()
lbluserLogIn = new JLabel()
lbluserName = new JLabel()
lbluserPWD = new JLabel()
txtName = new JTextField()
pwdPwd = new JPasswordField()
btnSub = new JButton()
btnReset = new JButton()
userInit()
}
public void userInit(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)//设置关闭框架的同时结束程序
this.setSize(300,200)//设置框架大小为长300,宽200
this.setResizable(false)//设置框架不可以改变大小
this.setTitle("用户登录")//设置框架标题
this.pnluser.setLayout(null)//设置面板布局管理
this.pnluser.setBackground(Color.cyan)//设置面板背景颜色
this.lbluserLogIn.setText("用户登录")//设置标签标题
this.lbluserLogIn.setFont(new Font("宋体",Font.BOLD | Font.ITALIC,14))//设置标签字体
this.lbluserLogIn.setForeground(Color.RED)//设置标签字体颜色
this.lbluserName.setText("用户名:")
this.lbluserPWD.setText("密 码:")
this.btnSub.setText("登录")
this.btnReset.setText("重置")
this.lbluserLogIn.setBounds(120,15,60,20)//设置标签x坐标120,y坐标15,长60,宽20
this.lbluserName.setBounds(50,55,60,20)
this.lbluserPWD.setBounds(50,85,60,25)
this.txtName.setBounds(110,55,120,20)
this.pwdPwd.setBounds(110,85,120,20)
this.btnSub.setBounds(85,120,60,20)
this.btnSub.addActionListener(new ActionListener()//匿名类实现ActionListener接口
{
public void actionPerformed(ActionEvent e){
btnsub_ActionEvent(e)
}
}
)
this.btnReset.setBounds(155,120,60,20)
this.btnReset.addActionListener(new ActionListener()//匿名类实现ActionListener接口
{
public void actionPerformed(ActionEvent e){
btnreset_ActionEvent(e)
}
}
)
this.pnluser.add(lbluserLogIn)//加载标签到面板
this.pnluser.add(lbluserName)
this.pnluser.add(lbluserPWD)
this.pnluser.add(txtName)
this.pnluser.add(pwdPwd)
this.pnluser.add(btnSub)
this.pnluser.add(btnReset)
this.add(pnluser)//加载面板到框架
this.setVisible(true)//设置框架可显
}
public void btnsub_ActionEvent(ActionEvent e){
String name = txtName.getText()
String pwd = String.valueOf(pwdPwd.getPassword())
if(name.equals("")){
JOptionPane.showMessageDialog(null,"账号不能为空","错误",JOptionPane.ERROR_MESSAGE)
return
}else if (pwd.equals("")){
JOptionPane.showMessageDialog(null,"密码不能为空","错误",JOptionPane.ERROR_MESSAGE)
return
}else if(true){
this.dispose()
}else{
JOptionPane.showMessageDialog(null,"账号或密码错误","错误",JOptionPane.ERROR_MESSAGE)
return
}
}
public void btnreset_ActionEvent(ActionEvent e){
txtName.setText("")
pwdPwd.setText("")
}
public static void main(String[] args){
new UserLogIn()
}
}
准备工作:
1、下载JDK;
2、下载Eclipse;
3、下载相应的EMF、GEF、VE;
开始安装:
1、安装JDK;
这个比较容易,标准的Windows安装程序,下一步,再下一步即可,安装完成后,也不需要重启。
2、安装Eclipse;
Eclipse是绿色软件,不需要安装,只需要解压缩,然后即可运行。
为了说明方便,我把它解压缩到C盘根目录下,得到C:\eclipse目录,运行C:\eclipse\eclipse.exe即可。
注意:下面的安装,需要先关闭eclipse程序。
3、安装EMF、GEF、VE;
对于Eclipse来说,这些都是它的插件,所以,安装方法都是一样的。插件下载地址http://download.eclips.org/ve
A、在C:\eclipse目录下,建立四个子目录:C:\eclipse\emf、C:\eclipse\gef、C:\eclipse\ve、C:\eclipse\links;
B、把下载的EMF、GEF、VE都解压缩到相应的目录中,即:把EMF压缩包解压缩到C:\eclipse\emf中,得到C:\eclipse\emf\eclipse目录,以此类推,完成GEF、VE的解压缩;
C、在C:\eclipse\links目录下,新建一个文本文件,名字可随便取,如:link.txt。
然后在link.txt文件中,加入以下三行文字:
path=emf
path=gef
path=VE