- 一自萧关起战尘
-
我刚好写了个浏览图片的。你看有帮助就改改好了
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,"这是一款简单的图片浏览器"+" "+
"虽然只能进行图片阅览"+" "+"以后改进 "+"作者: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;
}
}
}
- 不白九百
-
文件一:
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();
}
}
- Troublesleeper
-
看在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);
}
}
- 阿啵呲嘚
-
//-----这个是把图片当成背景用 希望对你有用
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);
}
}
- 左迁
-
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。