Java中swing使用ImageIcon类添加图片

代码如下

 package com.liuyanzhao;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.WindowConstants;
    public class Demo3 extends JFrame {
        public void GUI() {
            setTitle("图像测试");
            JPanel panel = new JPanel();
            JLabel label = new JLabel();
            ImageIcon img = new ImageIcon("images/logo.jpg");// 创建图片对象
            label.setIcon(img);
            panel.add(label);
            add(panel);
            setExtendedState(JFrame.MAXIMIZED_BOTH);// JFrame最大化
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 让JFrame的关闭按钮起作用
            setVisible(true);// 显示JFrame
        }
        public static void main(String args[]) {
            Demo3 d = new Demo3();
            d.GUI();
        }
    }

其中 images/logo.jpg 是图片的路径,记得images是项目(Project)目录下的文件夹,跟src同级的。具体路径问题可以参考这篇文章 java ImageIcon路径问题


本文链接:https://liuyanzhao.com/4081.html

  • 15
    点赞
  • 81
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
package com.shou.loginfjame; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException; import java.util.List; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.xml.bind.util.ValidationEventCollector; import com.shou.LoginUtil.LoginUser; import com.shou.dao.LoginDao; import com.shuo.util.ValidCode; public class LoginFjame extends JFrame implements ActionListener { private JFrame frame = new JFrame("登录"); private JPanel panel = new JPanel(); private JLabel tiel = new JLabel("龍丶逸小说登录系统"); // 创建标题 private JLabel userLabel = new JLabel("用户名:"); // 创建UserJLabel private JTextArea userText=new JTextArea("请输入内容",7, 30); // 获取登录名 private JLabel passLabel = new JLabel("密 码:"); // 创建PassJLabel private JPasswordField passText = new JPasswordField(20); // 密码框隐藏 private JLabel verCodeLa = new JLabel("验证码:"); // 验证码 private JTextField inputCode = new JTextField(); // 验证码框 private ValidCode vcode = new ValidCode(); // 验证码内容 JTextField jt_code; private JButton loginButton = new JButton("登录"); // 创建登录按钮 private JButton registerButton = new JButton("注 册"); // 创建注册按钮 private JButton newPasswordButton = new JButton("忘记密码"); // 创建注册按钮 private JButton exitButton = new JButton("退出"); JTextField field = null; public LoginFjame() { System.out.println("====================================="); System.out.println("== 龍丶逸小说系统 =="); System.out.println("== V1.1.1.0 =="); System.out.println("====================================="); WinLogin(); } public void WinLogin() { panel.setLayout(null); // 设置布局为 null // 创建标题名称 this.tiel.setFont(new Font("宋体", 1, 20)); this.tiel.setBounds(150, 30, 300, 25); this.panel.add(this.tiel); // 创建 UserJLabel this.userLabel.setFont(new Font("宋体", 1, 13)); this.userLabel.setBounds(70, 80, 80, 25); this.panel.add(userLabel); // 创建文本域用于用户输入 this.userText.setBounds(145, 80, 165, 25); this.panel.add(this.userText); // 注册 this.registerButton.setFont(new Font("宋体", 1, 15)); this.registerButton.setContentAreaFilled(false); this.registerButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.registerButton.setBounds(320, 80, 100, 25); this.panel.add(this.registerButton); // 变成小手 this.registerButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 创建PassJLabel this.passLabel.setFont(new Font("宋体", 1, 13)); this.passLabel.setBounds(70, 110, 80, 25); this.panel.add(this.passLabel); // 密码输入框 隐藏 this.passText.setBounds(145, 110, 165, 25); this.panel.add(this.passText); // 忘记密码 this.newPasswordButton.setFont(new Font("宋体", 1, 15)); this.newPasswordButton.setContentAreaFilled(false); this.newPasswordButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.newPasswordButton.setBounds(320, 110, 100, 25); this.panel.add(this.newPasswordButton); // 变成小手 this.newPasswordButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 验证码code this.verCodeLa.setFont(new Font("宋体", 1, 13)); this.verCodeLa.setBounds(70, 140, 80, 25); this.panel.add(this.verCodeLa); // 验证码框 this.inputCode.setBounds(145, 140, 165, 25); this.panel.add(this.inputCode); // 验证码图片 this.vcode.setBounds(320, 140, 165, 25); this.panel.add(this.vcode); System.out.println(this.vcode); // 创建登录按钮 this.loginButton.setFont(new Font("宋体", 1, 15)); this.loginButton.setBounds(95, 190, 80, 25); this.panel.add(this.loginButton); // 变成小手 this.loginButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 退出按钮 this.exitButton.setFont(new Font("宋体", 1, 15)); this.exitButton.setBounds(230, 190, 80, 25); this.panel.add(this.exitButton); // 变成小手 this.exitButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 设置窗体的位置及大小 this.frame.setSize(460, 355); frame.setLocationRelativeTo(null); // 在屏幕显示 frame.add(this.panel); // 添加面板 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置X号后关闭 //设置按钮 this.registerButton.addActionListener(this); //注册按钮 this.newPasswordButton.addActionListener(this); //忘记密码 this.loginButton.addActionListener(this); //登录 this.exitButton.addActionListener(this); //退出 // 往窗体里放其他控件 frame.setVisible(true); // 设置窗体可见 } @Override public void actionPerformed(ActionEvent e) { JButton bt = (JButton) e.getSource(); // 获取按钮信息 String str = bt.getText(); // 获取用户名 String name = this.userText.getText().trim(); // 获取密码 String password = this.passText.getText().trim(); // 获取验证码 String code = this.inputCode.getText().trim(); // 获取jsp验证码 String vcode = this.vcode.getCode(); // 登录 if (str.equals("登录")) { System.out.println("登录"); // 验证码转为大写 String Dcode = code.toUpperCase(); String Dvcode = vcode.toUpperCase(); // 验证码判断 if (Dcode.equals(Dvcode)) { //获取页面的用户名 String username=this.userText.getText().trim(); // 根据用户名查看是否有该用户 try { List loginUser=new LoginDao().queryAll(username); String a=loginUser.toString(); System.out.println(a.toString()); if(!a.toString().equals("[]")){ //密码判断 String mysqlPasword=loginUser.get(0).l_password(); if(mysqlPasword.equals(password)){ //登录成功 JOptionPane pane = new JOptionPane("登录成功"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); }else{ JOptionPane pane = new JOptionPane("密码错误错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } }else{ JOptionPane pane = new JOptionPane("用户名错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } /*System.out.println(loginUser.toString()); String sqlUername=loginUser.get(0).getL_username();*/ /*int sqlpassword=loginUser.get(0).getL_power();*/ /*System.out.println("loginF:"+sqlUername);*/ } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { JOptionPane pane = new JOptionPane("验证码错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); System.out.println(dialog.getFont()); dialog.show(); } } else // 退出 if (str.equals("退出")) { System.out.println("退出"); System.exit(0); } else // 注册 if (str.equals("注 册")) { System.out.println("注 册"); } // 注册 else if (str.equals("忘记密码")) { System.out.println("忘记密码"); } else { System.out.println("异常错误"); } } public boolean isValidCodeRight() { System.out.println(this.jt_code.getText()); if (this.jt_code == null) { return false; } if (this.vcode == null) { return true; } if (this.vcode.getCode().equals(this.jt_code.getText())) { return true; } return false; } public static void main(String[] args) { new LoginFjame(); } }
目 录 一、开发背景……………………………………………………………………….1 二、可行分析……………………………………………………………………….1 三、设计内容……………………………………………………………………….2 四、设计要求……………………………………………………………………….2 五、具体设计……………………………………………………………………….2 六、具体分工………………………………………………………………….……4 七、程序设计……………………………………………………………………….4 1、主程序模块…………………………………………………….....…........14 1、添加模块…………………………………………………….....…........14 2、删除模块……………………………………………….........………....22 4、背景框格设计……………………………………………...………......32 八、实习心得……………………………………………………………………….35 一、开发背景 在信息化不断发展的今天,社会成员相互之间联系越来越紧密,联系方式也越来越 多。我们除了手机,移动电话等常规联系方式外,现在还有了电子邮箱、MSN、QQ等信息 化的联系方式,那么为了方便我们将每个联系人的多个联系方式保存在一起,方便由于 一种联系方式联系不到某人时,可查询其他联系方式,以前的"纸质"通讯录已经越来越 赶不上信息化的速度,势必被淘汰,所以我们用所学过的知识实现一个"非纸质"通讯录 。 二、可行分析 介绍了一个简单实用的个人通讯录管理系统java工程,详细描述了本人在编程时候的编 程思想和在MyEclipse 6.5调试运行的步骤,简明扼要地介绍了整个工程的功能和实用方法。这个个人通讯录 管理系统含有添加、删除、查找等基本功能,同时也添加了一些有趣的小功能-- 支持背景图片的更换。在程序完成了与数据库的通信,并同样可以对数据库进行添加 、查找、删除等功能。 三、设计内容 设计GUI界面的个人通讯录,用户可以添加,删除,修改,查询,打印通讯录的记 录! 四,设计要求 用图形用户界面实现: 1、能实现增加记录,删除记录,显示所有记录,查询记录,文件备份。 2、通讯录的功能包括:姓名,电话,Email等。 3、使用数据库做后台连接! 五、具体设计 文件分别编译生成相应的字节码(class)文件。然后,用java解释器运 行主: 主界面 六、具体分工 我参与设计添加页面,添加方法,删除页面,查询部分操作资以及源管理模块,背景 框格布局操作。 添加界面 删除界面 七、 程序设计 1. 主程序设计模块 package com.zzk.frame; import java.awt.BorderLayout; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.URL; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JToolBar; import javax.swing.SwingConstants; import javax.swing.WindowConstants; import com.swtdesigner.SwingResourceManager; import com.zzk.background.BackgroundPanel; import com.zzk.typestate.SaveTypeState; public class MainFrame extends JFrame { public MainFrame() { super(); setTitle("通讯录管理系统"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setBounds(100,60,800,600); URL url = LoginFrame.class.getResource("/image/main.jpg"); // 获得图片的URL Image image=new ImageIcon(url).getImage(); // 创建图像对
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.io.*; //主 public class Chess{ public static void main(String args[]){ new ChessMainFrame("国象棋:观棋不语真君子,棋死无悔大丈夫"); } } //主框架 class ChessMainFrame extends JFrame implements ActionListener,MouseListener,Runnable{ //玩家 JLabel play[] = new JLabel[32]; //棋盘 JLabel image; //窗格 Container con; //工具栏 JToolBar jmain; //重新开始 JButton anew; //悔棋 JButton repent; //退出 JButton exit; //当前信息 JLabel text; //保存当前操作 Vector Var; //规则对象(使于调用方法) ChessRule rule; /** ** 单击棋子 ** chessManClick = true 闪烁棋子 并给线程响应 ** chessManClick = false 吃棋子 停止闪烁 并给线程响应 */ boolean chessManClick; /** ** 控制玩家走棋 ** chessPlayClick=1 黑棋走棋 ** chessPlayClick=2 红棋走棋 默认红棋 ** chessPlayClick=3 双方都不能走棋 */ int chessPlayClick=2; //控制棋子闪烁的线程 Thread tmain; //把第一次的单击棋子给线程响应 static int Man,i; ChessMainFrame(){ new ChessMainFrame("国象棋"); } /** ** 构造函数 ** 初始化图形用户界面 */ ChessMainFrame(String Title){ //获行客格引用 con = this.getContentPane(); con.setLayout(null); //实例化规则 rule = new ChessRule(); Var = new Vector(); //创建工具栏 jmain = new JToolBar(); text = new JLabel("欢迎使用象棋对弈系统"); //当鼠标放上显示信息 text.setToolTipText("信息提示"); anew = new JButton(" 新 游 戏 "); anew.setToolTipText("重新开始新的一局"); exit = new JButton(" 退 出 "); exit.setToolTipText("退出象棋程序程序"); repent = new JButton(" 悔 棋 "); repent.setToolTipText("返回到上次走棋的位置"); //把组件添加到工具栏 jmain.setLayout(new GridLayout(0,4)); jmain.add(anew); jmain.add(repent); jmain.add(exit); jmain.add(text); jmain.setBounds(0,0,558,30); con.add(jmain); //添加棋子标签 drawChessMan(); //注册按扭监听 anew.addActionListener(this); repent.addActionListener(this); exit.addActionListener(this); //注册棋子移动监听 for (int i=0;i<32;i++){ con.add(play[i]); play[i].addMouseListener(this); } //添加棋盘标签 con.add(image = new JLabel(new ImageIcon("image\\Main.GIF"))); image.setBounds(0,30,558,620); image.addMouseListener(this); .................... ....................
杨教授工作室 精心创作的优秀程序员 职业提升必读系列资料 杨教授工作室,版权所有,盗版必究, 1/16 页 1.1 Java Swing GUI 组件技术教学参考示例(第 2 部分) 1.1.1 Swing ToolBar 组件的应用实例 1、窗口内的内容面板 JFrame 对象本身是不能附加组件的(尽管它为顶层容器,但是不能把组件直接加到顶层 容器),也就是说调用 frame.append(...)这样的方法会出错,是不正确的。添加组件应该放 在内容面板(Content Panel),内容面板是一个 Container 对象,添加到 JFrame 的组件都只能放 到它上面。 Swing 窗体含有一个称为内容面板的容器(ContentPane),在顶层容器上放内容面板, 然后把组件加入到内容面板。 2、JToolBar 是用于显示常用工具控件的容器 (1)主要的用途 一般我们在设计软件时, 会将所有功能依放置在菜单(JMenu),但当功能数量相当多时, 可能造成用户操作一个简单的操作就必须繁复的寻找菜单相关的功能,这将造成用户操作 上的负担。若我们能将一般常用的功能以工具栏方式呈现在菜单下,让用户很快得到他想要 的功能,不仅增加用户使用软件的意愿,也加速工作的运行效率.这就是使用 ToolBar 的好处。 (2)用户可以拖拽出一个独立的可显示工具控件的窗口,常用方法有: 1) JToolBar(String name) //构造方法 2) getComponentIndex(Component c) //返回一个组件的序号 3) getComponentAtIndex(int i) //得到一个指定序号的组件 3、添加一个 NavigatorToolBar (1)从 javax.swing.JToolBar 继承、包名称为 com.px1987.javaapplication.swing.toolbar 杨教授工作室 精心创作的优秀程序员 职业提升必读系列资料 杨教授工作室,版权所有,盗版必究, 2/16 页 (2)NavigatorToolBar 的代码示例 package com.px1987.javaapplication.swing.toolbar; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JToolBar; import com.px1987.javaapplication.swing.SwingExplorerMainFrame; public class NavigatorToolBar extends JToolBar { private SwingExplorerMainFrame oneSwingExplorerMainFrame=null; JLabel backButton=null; JLabel pullDownButton=null; public NavigatorToolBar() { super(); } 杨教授工作室 精心创作的优秀程序员 职业提升必读系列资料 杨教授工作室,版权所有,盗版必究, 3/16 页 public NavigatorToolBar(String name,SwingExplorerMainFrame oneSwingExplorerMainFrame) { super(name); this.oneSwingExplorerMainFrame=oneSwingExplorerMainFrame; this.setFloatable(false); //设置该工具条不可拖动 createAllButtonsInToolBar(); } public NavigatorToolBar(int orientation) { super(orientation); } public NavigatorToolBar(String name) { super(name); } public NavigatorToolBar(String name, int orientation) { super(name, orientation); } public void createAllButtonsInToolBar(){ backButton=new JLabel("后退"); backButton.setToolTipText("点击我可以回退到上个面板"); backButton.setIcon(new ImageIcon(getClass().getResource( "/images/mainframe/toolbar/navigator/b

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半缘修道半缘君丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值