java窗口中加载图片?

网上查了很久就是没一个满意的,要不就是程序没法运行,要不就是啥都没说明。现在都把我搞晕了,好心的java学友们帮个忙,给个完整的窗口中加载图片源码,我自己再研究。不要代码... 网上查了很久就是没一个满意的,要不就是程序没法运行,要不就是啥都没说明。现在都把我搞晕了,好心的java学友们帮个忙,给个完整的窗口中加载图片源码,我自己再研究。不要代码断,谢谢。
比如,一个800x600的窗口,中间有张图片就行了,其他没什么要求。要完整的源码。
等分的就别急了,过两天要是没满意能用的,我就把分加到100了,然后150分,200分.
.............................
展开
 我来答
lqqnjust
推荐于2016-08-31 · TA获得超过415个赞
知道小有建树答主
回答量:627
采纳率:0%
帮助的人:474万
展开全部
我刚好写了个浏览图片的。你看有帮助就改改好了

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.Rectangle;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Picture extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
*
*/

private JPanel state;//状态面板
private JLabel statebar;
private JLabel timebar;
private JPanel client;//中间客户区
private JLabel pic;//图片标签
private TimeThread timethread;
private JToolBar jtoolbar;
private URL[] filepath;

public Picture(String str) throws AWTException
{
super(str);
//windows风格
try { // 使用Windows的界面风格
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}

//菜单栏
JMenuBar Menubar=new JMenuBar();
JMenu MenuFile = new JMenu("文件(F)");
MenuFile.setMnemonic('F');
JMenu MenuAbout = new JMenu("关于(H)");
MenuAbout.setMnemonic('H');
Menubar.add(MenuFile);
Menubar.add(MenuAbout);
//菜单项
JMenuItem OpenItem = new JMenuItem("打开图片文件(O)",'O');
OpenItem.setMnemonic('o');
JMenuItem LoadItem = new JMenuItem("批量导入图片(L)",'L');
LoadItem.setMnemonic('L');
JMenuItem ExitItem = new JMenuItem("退出(X)",'X');
ExitItem.setMnemonic('X');
JMenuItem AboutItem = new JMenuItem("关于(A)",'A');
AboutItem.setMnemonic('A');
MenuFile.add(OpenItem);
MenuFile.add(ExitItem);
MenuAbout.add(AboutItem);

//菜单项选项时间注册
OpenItem.addActionListener(new ActionListener()
{

public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
openfile();
}
});
ExitItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});

if (!SystemTray.isSupported())
{
System.out.println("SystemTray is not supported");
return;
}
SystemTray tray = SystemTray.getSystemTray();
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.getImage("image/tray.gif");
PopupMenu menu = new PopupMenu();
MenuItem ExitItemTray=new MenuItem("退出");
ExitItemTray.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});

menu.add(ExitItemTray);
TrayIcon icon = new TrayIcon(image,"图片浏览器");
icon.setImageAutoSize(true);
icon.setPopupMenu(menu);
tray.add(icon);

AboutItem.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
showhelp();
}

});
this.setDefaultCloseOperation(EXIT_ON_CLOSE);

//设置菜单栏
super.setJMenuBar(Menubar);

timebar=new JLabel();
// 时间线程
timethread =new TimeThread();
timethread.start();
//状态栏信息栏
statebar=new JLabel();
statebar.setText("未选定");

client=new JPanel();
add(client,BorderLayout.CENTER);
client.setBorder(BorderFactory.createTitledBorder(""));
pic =new JLabel();
client.add(pic,BorderLayout.CENTER);
pic.setSize(client.getWidth()-200, client.getHeight());

state = new JPanel();
add(state,BorderLayout.SOUTH);//状态栏
state.setBorder(BorderFactory.createTitledBorder(""));
//状态栏两个面板
state.add(timebar);
timebar.setBorder(BorderFactory.createTitledBorder(""));
state.setLayout(new FlowLayout(FlowLayout.LEFT));
state.add(statebar);

jtoolbar = new JToolBar();

JButton jb_next=new JButton("上一个");

jb_next.addActionListener(new ActionListener (){

public void actionPerformed(ActionEvent e) {

}

});
JButton jb_pre=new JButton("下一个");

JButton jb_play=new JButton("播 放");

jtoolbar.add(jb_next);
jtoolbar.add(jb_pre);
jtoolbar.add(jb_play);
add(jtoolbar,BorderLayout.NORTH);

setSize(700,400);//窗口大小
setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/2-350,150);//设置位置

setVisible(true);//显示
}

//打开图片
public void openfile()
{
JFileChooser f = new JFileChooser(); // 查找文件
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"图片文件(*.jpg,*.gif,*.png)", "jpg", "gif","png");
f.setFileFilter(filter);
f.showOpenDialog(this);
File[] files=new File[100];

files=f.getSelectedFile().listFiles();

ImageIcon img=new ImageIcon(f.getSelectedFile().getPath());
pic.setIcon(img);

statebar.setText("像素大小: "+img.getIconWidth()+"*"+img.getIconHeight()+" 文件位置: "+
f.getSelectedFile().getPath().toString()+" 文件大小: "+f.getSelectedFile().length()/1024+"KB"
);

}

//显示帮助
public void showhelp()
{
JOptionPane.showMessageDialog(this,"这是一款简单的图片浏览器"+"\n"+
"虽然只能进行图片阅览"+"\n"+"以后改进\n"+"作者:lqqnjust");
}
//主函数
public static void main(String[] args) throws Exception {
// TODO 自动生成方法存根

new Picture("图片浏览");
}
//时间线程类
public class TimeThread extends Thread
{
public void run()
{
//不停循环时间刷新
while(true){
Date d=new Date();//获取时间
SimpleDateFormat sdf=new SimpleDateFormat("kk:mm:ss");//转换格式
timebar.setText(sdf.format(d));
try{
Thread.sleep(1000);//1s执行一次
}catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
//托盘类
public class CreateTray
{
private CreateTray(){}
private CreateTray ct=null;
/** *//**创建单实列*/
public CreateTray getInstance()
{
//因为使用了判断语句,所以要用getInstance()方法
if(ct==null)
{
ct=new CreateTray();
}
return ct;
}
}

}
freish
2009-07-15 · TA获得超过2878个赞
知道大有可为答主
回答量:3153
采纳率:0%
帮助的人:2683万
展开全部
文件一:
import java.awt.Component;
import java.awt.Cursor;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.JTree;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

public class JFileTree extends JTree implements Serializable {
public static final FileSystemView fileSystemView = FileSystemView
.getFileSystemView();
DefaultTreeModel treeModel;

/**
* 指定过滤器的文件树
*
* @param filter
* FileFilter 指定过滤器
*/
public JFileTree(java.io.FileFilter filter) {
FileNode root = new FileNode(fileSystemView.getRoots()[0], filter);
treeModel = new DefaultTreeModel(root);
root.explore();
treeModel.nodeStructureChanged(root);
this.setModel(treeModel);
addTreeExpansionListener(new JFileTreeExpandsionListener());
setCellRenderer(new JFileTreeCellRenderer());
}

/**
* 取得当前选择的节点
*
* @return FileNode
*/
public FileNode getSelectFileNode() {
TreePath path = getSelectionPath();
if (path == null || path.getLastPathComponent() == null) {
return null;
}
return (FileNode) path.getLastPathComponent();
}

/**
* 取得当前选择的文件或目录
*
* @return File
*/
public File getSelectFile() {
FileNode node = getSelectFileNode();
return node == null ? null : node.getFile();
}

class JFileTreeCellRenderer extends DefaultTreeCellRenderer {
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, sel, expanded,
leaf, row, hasFocus);
try {
closedIcon = fileSystemView.getSystemIcon(((FileNode) value)
.getFile());
openIcon = closedIcon;
setIcon(closedIcon);
} catch (Exception ex) {
}
return this;
}
}

class JFileTreeExpandsionListener implements TreeExpansionListener {
public JFileTreeExpandsionListener() {
}

public void treeExpanded(TreeExpansionEvent event) {
TreePath path = event.getPath();
if (path == null || path.getLastPathComponent() == null)
return;
setCursor(new Cursor(Cursor.WAIT_CURSOR));
FileNode node = (FileNode) path.getLastPathComponent();
node.explore();
JTree tree = (JTree) event.getSource();
DefaultTreeModel treeModel = (DefaultTreeModel) tree.getModel();
treeModel.nodeStructureChanged(node);
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}

public void treeCollapsed(TreeExpansionEvent event) {
}
}

public static class FileNode extends DefaultMutableTreeNode {
private boolean explored = false;
private java.io.FileFilter filter = null;

public FileNode(File file, java.io.FileFilter filter) {
this.filter = filter;
setUserObject(file);
}

public boolean getAllowsChildren() {
return isDirectory();
}

public boolean isDirectory() {
return !isLeaf();
}

public boolean isLeaf() {
return getFile().isFile();
}

public File getFile() {
return (File) getUserObject();
}

public boolean isExplored() {
return explored;
}

public void setExplored(boolean b) {
explored = b;
}

public String toString() {
if (getFile() instanceof File)
return fileSystemView.getSystemDisplayName((File) getFile());
else
return getFile().toString();
}

/**
* 展开节点
*/
public void explore() {
if (!explored) {
explored = true;
File file = getFile();
File[] children = file.listFiles();
if (children == null || children.length == 0) {
return;
}
// 过滤后排序,选加入排序后的目录, 再加入排序后的文件
ArrayList listDir = new ArrayList();
ArrayList listFile = new ArrayList();
for (int i = 0; i < children.length; ++i) {
File f = children[i];
if (filter.accept(f)) {
if (f.isDirectory()) {
listDir.add(f);
} else {
listFile.add(f);
}
}
}
Collections.sort(listDir);
Collections.sort(listFile);
for (int i = 0; i < listDir.size(); i++) {
add(new FileNode((File) listDir.get(i), filter));
}
for (int i = 0; i < listFile.size(); i++) {
add(new FileNode((File) listFile.get(i), filter));
}
}
}
}
}

文件二
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileFilter;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;

public class PictShow extends JFrame {
private ImageIcon icon = new ImageIcon();
private JPanel pictPanel;
private JFileTree pictTree;

public PictShow() throws Exception {
super("JPG图片资源管理器");
buildFrame();
}

private void buildFrame() throws Exception {
Container container = getContentPane();
JSplitPane split = new JSplitPane();

pictPanel = new JPanel() {// 显示图片的面板
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(icon.getImage(), 10, 10, null);
}
};

JScrollPane scroll=new JScrollPane();
scroll.setViewportView(pictPanel);
split.add(scroll, JSplitPane.RIGHT);

pictTree = new JFileTree(new FileFilter() {
public boolean accept(File pathname) {
if (pathname != null) {
if (pathname.isDirectory()) {
return true;
}
String name = pathname.getName().toLowerCase();
return name.endsWith(".jpg");
}

return false;
}
});

pictTree.addTreeSelectionListener( //添加监听器
new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
File file = pictTree.getSelectFile();
if (file != null && file.isFile()
&& file.getName().toLowerCase().endsWith(".jpg")) {
icon = new ImageIcon(file.getAbsolutePath());
pictPanel.setPreferredSize(new Dimension(icon.getIconWidth(),icon.getIconHeight()));
pictPanel.revalidate();
pictPanel.repaint();
}
}
}
);

split.add(new JScrollPane(pictTree), JSplitPane.LEFT); //将显示图片的面板添加到split中
setSize(800, 600);
split.setDividerLocation((int)(getSize().width*0.3));

container.add(split); //将split添加到容器用于显示
//屏幕居中
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
setLocation((screenSize.width-frameSize.width)/2 , (screenSize.height-frameSize.height)/2);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) throws Exception {
new PictShow();
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
渡轮555
2009-07-15 · TA获得超过534个赞
知道小有建树答主
回答量:863
采纳率:100%
帮助的人:653万
展开全部
看在50分面子上...从网上找的...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test
{

private JFrame frame = new JFrame("背景图片测试");

private JPanel imagePanel ;

private ImageIcon background;

public static void main(String[] args)
{
new Test();
}

public Test()
{
background = new ImageIcon("1.jpg");//背景图片
JLabel label = new JLabel(background);//把背景图片显示在一个标签里面
// 把标签的大小位置设置为图片刚好填充整个面板
label.setBounds(0,0,background.getIconWidth(),background.getIconHeight());
// 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
imagePanel = (JPanel)frame.getContentPane();
imagePanel.setOpaque(false);
// 内容窗格默认的布局管理器为BorderLayout
imagePanel.setLayout(new FlowLayout());
JButton b=new JButton("测试按钮");
imagePanel.add(b);
b.setOpaque(false);
frame.getLayeredPane().setLayout(null);
// 把背景图片添加到分层窗格的最底层作为背景
frame.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(background.getIconWidth(),background.getIconHeight());
frame.setVisible(true);
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
win720520
2009-07-15 · 超过30用户采纳过TA的回答
知道答主
回答量:160
采纳率:50%
帮助的人:68.7万
展开全部
//-----这个是把图片当成背景用 希望对你有用

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.GrayFilter;
import javax.swing.*;

public class 背景图片 extends JFrame {
public 背景图片()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final ImageIcon imageIcon = new ImageIcon("Sunset.jpg"); //图片路径
JPanel p = new JPanel() {
Image image = imageIcon.getImage();

Image grayImage = GrayFilter.createDisabledImage(image);
{
setOpaque(false);
}
public void paint(Graphics g) {
g.drawImage(imageIcon.getImage(), 0, 0, this.getWidth(),this.getHeight(),this);
super.paint(g);
}
};
JScrollPane scrollPane = new JScrollPane(p);
Container content = this.getContentPane();
content.add(scrollPane, BorderLayout.CENTER);
this.setSize(500,400);
this.setVisible(true);
}

public static void main(String[] args)
{
背景图片 m=new 背景图片();
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
daay1986
2009-07-15 · TA获得超过6018个赞
知道大有可为答主
回答量:2208
采纳率:0%
帮助的人:1466万
展开全部
public class TestFrame {

public static void main(String[] args) {
new TestFrame().init();
}

private void init() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(0, 0, 800, 600);
JLabel label = new JLabel();
label.setSize(800, 600);
label.setIcon(new javax.swing.ImageIcon("D:\\Backup\\我的文档\\My Pictures\\20070804115158926.jpg")); // NOI18N
frame.getContentPane().add(label);
frame.setVisible(true);
}
}

label.setIcon(new javax.swing.ImageIcon("D:\\Backup\\我的文档\\My Pictures\\20070804115158926.jpg"));这一大段是图片路径,注意是双斜杠啊。
另外这个问题最麻烦的是怎么读图片,你也可以用ImageIO来读图,详细看下api。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式