java图形界面美化_Java图形化编程美化
去掉原来的界面的装饰,改用自己装饰:
先看看效果,如下图

如何实现:
1.去掉原来的windows的装饰:
this.setSize(340, 256);
this.setUndecorated(true);
尽管去除了装饰元素,但在界面布局中,默认显示图标及标题是必要的。然而在桌面最下面的任务栏中将无法显示相关信息如果缺少这些元素则会严重影响用户体验
URL url5 = this.getClass().getResource("/p_w_picpath/client_title.png");
this.setIconImage(new ImageIcon(url5).getImage());
this.setTitle("用户登录");
this.setLocation(new ScreenLocation(348,250).getLocation());
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setResizable(false);
2.移动界面的代码:
Point loc = null;
Point tmp = null;
boolean isDragged = false;
//拖动窗体的方法
private void setDragable() {
this.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent e) {
isDragged = false;
//当鼠标不拖动时,设置鼠标显示的样式
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
public void mousePressed(java.awt.event.MouseEvent e) {
tmp = new Point(e.getX(), e.getY());
isDragged = true;
//当鼠标点击时,鼠标显示的样式
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
});
//添加鼠标运动监听器
this.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent e) {
if (isDragged) {
//重新设置界面的位置
loc = new Point(getLocation().x + e.getX() - tmp.x,
getLocation().y + e.getY() - tmp.y);
setLocation(loc);
}
}
});
}
3.添加界面最下化与关闭按钮:
//处理关闭窗口按钮
//com为界面的容器,exit为按钮JButton
com.add(exit);
exit.setBounds(300, 1, 37, 20);
exit.setBorder(null);
exitButton = new ExitButton(); iconObject = new ImageIcon(); iconObject Load Image at "/p_w_picpath/login_exit.jpg"; exitButton Set Icon To iconObject
exit.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
exit的图标被设置为新的JIcon图像文件路径为/p_w_picpath/login_exit2.jpg
}
public void mouseExited(MouseEvent e) {
exit.退出页面...使用创建新的JIcon对象来显示图片路径为指定路径的图片。
该段代码的作用是将指定位置的登录退出页面图标加载到当前组件中。
}
public void mouseClicked(MouseEvent e) {
if(e.getClickCount() == 1){
System.exit(0);
}
}
});
//处理最小化窗口按钮
//com为界面的容器,minux为按钮JButton
com.add(minux);
minux.setBounds(272,1, 29, 20);
minux.setBorder(null);
通过调用minux.iconSet方法并赋值图片文件路径"/p_w_picpath/login_minux.jpg"/至该对象中来实现图标显示
minux.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
通过调用 setIcon 方法将 minux 的图标设置为路径为 /p_w_picpath/login_minux2.jpg 的新 ImageIcon 对象。
}
public void mouseExited(MouseEvent e) {
iconSet(new ImageIcon(this.getClass().getResource("/p_w_picpath/login_minux.jpg")));
}
public void mouseClicked(MouseEvent e) {
if(e.getClickCount() == 1){
//从界面管理容器中取得主界面,然后最小化
FaceManager.getFace("login").setExtendedState(JFrame.ICONIFIED);
}
}
});
需要将显示内容设置为一张图片,并使用JLabel组件来指定背景图像的位置;同时将主窗口布局设为空以避免影响整体显示效果
URL url1 = this.getClass().getResource("/p_w_picpath/login.jpg");
background = new JLabel(new ImageIcon(url1));
Container com = this.getContentPane();
com.setLayout(null);
com.add(background);
background.setBounds(0, 0, 340, 256);
//下面是添加可以移动的方法
setDragable();
全部代码如下
publicclassClientLoginextendsJFrameimplementsActionListener {
privatestaticfinallongserialVersionUID = 1L;
publicstaticString serverIp ="127.0.0.1";
JLabel background,jl_uname,jl_psd,jl_ip;
JPanel con,control;
JTextField username,ipaddr;
JPasswordField password;
JButton jb_login,jb_cannel,jb_regist;
JButton exit =newJButton();
JButton minux =newJButton();
Point loc =null;
Point tmp =null;
booleanisDragged =false;
//拖动窗体的方法
privatevoidsetDragable() {
this.addMouseListener(newjava.awt.event.MouseAdapter() {
publicvoidmouseReleased(java.awt.event.MouseEvent e) {
isDragged =false;
setCursor(newCursor(Cursor.DEFAULT_CURSOR));
}
publicvoidmousePressed(java.awt.event.MouseEvent e) {
tmp =newPoint(e.getX(), e.getY());
isDragged =true;
setCursor(newCursor(Cursor.DEFAULT_CURSOR));
}
});
this.addMouseMotionListener(newjava.awt.event.MouseMotionAdapter() {
publicvoidmouseDragged(java.awt.event.MouseEvent e) {
if(isDragged) {
loc =newPoint(getLocation().x + e.getX() - tmp.x,
getLocation().y + e.getY() - tmp.y);
setLocation(loc);
}
}
});
}
//构造函数
publicClientLogin() {
FaceManager.addFace("login",this);
URL url1 =this.getClass().getResource("/p_w_picpath/login.jpg");
background =newJLabel(newImageIcon(url1));
con =newJPanel(null);
jl_uname =newJLabel("用户名:",JLabel.CENTER);
jl_psd =newJLabel("密码:",JLabel.CENTER);
username =newJTextField();
password =newJPasswordField();
jl_ip =newJLabel("服务IP:");
ipaddr =newJTextField(serverIp);
con.add(jl_uname);
con.add(username);
con.add(jl_psd);
con.add(password);
con.add(jl_ip);
con.add(ipaddr);
control =newJPanel();
//处理南部
URL url2 =this.getClass().getResource("/p_w_picpath/denglu.gif");
jb_login =newJButton(newImageIcon(url2));
jb_login.addActionListener(this);
URL url3 =this.getClass().getResource("/p_w_picpath/quxiao.gif");
jb_cannel =newJButton(newImageIcon(url3));
jb_cannel.addActionListener(this);
URL url4 =this.getClass().getResource("/p_w_picpath/xiangdao.gif");
jb_regist =newJButton(newImageIcon(url4));
jb_regist.addActionListener(this);
//control.setBackground(Color.pink);
Container com =this.getContentPane();
com.setLayout(null);
com.add(jl_uname);
jl_uname.setBounds(50,120,50,20);
com.add(username);
username.setBounds(110,120,150,20);
com.add(jl_psd);
jl_psd.setBounds(50,155,50,20);
com.add(password);
password.setBounds(110,155,150,20);
com.add(jl_ip);
jl_ip.setBounds(50,190,50,20);
com.add(ipaddr);
ipaddr.setBounds(110,190,150,20);
com.add(jb_login);
jb_login.setBounds(40,225,66,20);
com.add(jb_cannel);
jb_cannel.setBounds(130,225,66,20);
com.add(jb_regist);
jb_regist.setBounds(220,225,66,20);
com.add(background);
background.setBounds(0,0,340,256);
setDragable();
//处理关闭窗口按钮
com.add(exit);
exit.setBounds(300,1,37,20);
exit.setBorder(null);
exit按钮设置为getImageIcon(通过类资源获取login_exit_path.jpg)
exit.addMouseListener(newMouseAdapter() {
publicvoidmouseEntered(MouseEvent e) {
exit将该图像通过newImageIcon函数传递给内存中的路径变量
}
publicvoidmouseExited(MouseEvent e) {
exit将图片节点设置为指定位置的图片文件路径,并将其传递给newImageIcon方法执行操作
}
publicvoidmouseClicked(MouseEvent e) {
if(e.getClickCount() ==1){
System.exit(0);
}
}
});
//处理最小化窗口按钮
com.add(minux);
minux.setBounds(272,1,29,20);
minux.setBorder(null);
minux.icon = minux.icon(通过调用this.getClass().getResource("/p_w_picpath/login_minux.jpg")获取图片路径)
minux.addMouseListener(newMouseAdapter() {
publicvoidmouseEntered(MouseEvent e) {
该系统将图标集合命名为newImageIcon,并通过获取该类资源路径 /p_w_picpath/login_minux2.jpg 来设置该图标集合。
}
publicvoidmouseExited(MouseEvent e) {
该 minux 对应地拥有一个新的 icon set 由其类加载器中的指定图片路径配置
}
publicvoidmouseClicked(MouseEvent e) {
if(e.getClickCount() ==1){
FaceManager.getFace("login").setExtendedState(JFrame.ICONIFIED);
}
}
});
this.setUndecorated(true);
URL url5 =this.getClass().getResource("/p_w_picpath/client_title.png");
this.setIconImage(newImageIcon(url5).getImage());
this.setSize(340,256);
this.setTitle("用户登录");
this.setLocation(newScreenLocation(348,250).getLocation());
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setResizable(false);
}
//处理事件
publicvoidactionPerformed(ActionEvent e) {
if(e.getSource() == jb_cannel){
//取消
username.setText("");
password.setText("");
ipaddr.setText("");
}elseif(e.getSource() == jb_login){
String ip = ipaddr.getText().trim();
if(ip !=null&& ip.length()>0)
{
serverIp = ip;
}
User u =newUser();
u.setUsername(username.getText().trim());
u.setPassword(newString(password.getPassword()));
u.setAction(1);
//检测用户
AlmClientUser qcu =newAlmClientUser();
intresult = qcu.checkUser(u);
if(1== result){
newClientFace(u.getUsername());
//关闭登陆页面
this.dispose();
}elseif(2== result){
显示消息窗口提示错误:'无法建立服务器连接'
}elseif(3== result){
JOptionPane.showMessageDialog(this,"服务器暂时关闭,请稍后再试");
}elseif(4== result){
JOptionPane.showMessageDialog(this,"无法与服务器通信,请稍后再试");
}else{
JOptionPane.showMessageDialog(this,"用户名或密码错误");
}
}elseif(e.getSource() == jb_regist){
//注册
UserReg reg =newUserReg(this);
reg.setVisible(true);
}
}
}
