大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
打开eclipse——File——Import——General——Existing Projects into Workspace——Next——Browse,将图片上的上一层目录工程文件打开就可以
公司主营业务:成都做网站、网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联建站是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联建站推出项城免费做网站回馈大家。
我刚好写了个浏览图片的。你看有帮助就改改好了
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;
}
}
}
在java swing中需要为容器添加图片,或者背景图片。
提供两种简单的解决方案,一种利用JPanel,另一种利用JLabel
1.JPanel(源代码)
package oo;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Drawing {
JFrame jframe = new JFrame();
public static JPanel GImage = null;
public Drawing() {
initFrame();
}
// 初始化窗口
public void initFrame() {
// 利用JPanel添加背景图片
GImage = new JPanel() {
protected void paintComponent(Graphics g) {
ImageIcon icon = new ImageIcon("image\\benbenla.jpg");
Image img = icon.getImage();
g.drawImage(img, 0, 0, icon.getIconWidth(),
icon.getIconHeight(), icon.getImageObserver());
jframe.setSize(icon.getIconWidth(), icon.getIconHeight());
}
};
jframe.setTitle("测试背景图片");
jframe.add(GImage);
jframe.pack();
jframe.setVisible(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Drawing();
}
}
2.JLabel源代码
package swing.draw;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Drawing2 {
JLabel jlpic = new JLabel();
JFrame jframe = new JFrame();
public Drawing2() {
init1Frame();
}
public void init1Frame() {
ImageIcon icon = new ImageIcon("image\\benbenla.jpg");
icon.setImage(icon.getImage().getScaledInstance(icon.getIconWidth(),
icon.getIconHeight(), Image.SCALE_DEFAULT));
System.out.println(icon.getIconHeight() + "" + icon.getIconWidth());
jlpic.setBounds(0, 0, 1366, 768);
jlpic.setHorizontalAlignment(0);
jlpic.setIcon(icon);
jframe.setSize(1366, 768);
jframe.add(jlpic);
jframe.pack();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
}
public static void main(String args[]) {
new Drawing2();
}
}
可以新建个面板,在面板里放入带图片的JLabel,填满面板即可。
JPanel jp = new JPanel(); //新建面板
jp.setLayout(new FlowLayout()); //设置面板布局
ImageIcon ii=new ImageIcon(getClass().getResource("/Picture/i.jpg"));
JLabel uppicture=new JLabel(ii); //往面板里加入JLabel
this.setVisible(true);
给你代码,看一下注释,为是防止左右移动时鼠标是水平的移动,所以加了一个范轩,
在二十个象素内,认为是水平的。
你看一下吧
还好你这个是鼠标按住移动,如果只是鼠移动就麻烦了
----------------------------------------
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
public class App extends JFrame implements MouseListener, ActionListener {
int x = 0;
int y = 0;
File[] selectedFiles = null;
int fileIndex = 0;
int width = 200;
int height = 200;
public App() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400, 300);
setResizable(false);
getContentPane().setLayout(null);
JPanel panel = new ImagePanel();
panel.setBounds(12, 40, 370, 218);
getContentPane().add(panel);
addMouseListener(this);
JButton btnBrowse = new JButton("Browse");
btnBrowse.addActionListener(this);
btnBrowse.setBounds(12, 9, 91, 21);
getContentPane().add(btnBrowse);
setVisible(true);
}
public static void main(String[] args) {
new App();
}
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG GIF Images", "jpg", "gif");
// 设置文件类型
chooser.setFileFilter(filter);
// 打开选择器面板
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
selectedFiles = chooser.getSelectedFiles();
repaint();
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
Point point = MouseInfo.getPointerInfo().getLocation();
x = point.x;
y = point.y;
}
public void mouseReleased(MouseEvent e) {
Point point = MouseInfo.getPointerInfo().getLocation();
int thisX = point.x;
int thisY = point.y;
System.out.println("thisX=" + thisX + " " + "thisY=" + thisY);
if ((y - thisY 20 y - thisY 0)
|| (y - thisY 0 y - thisY -20)) {
// Y 在20范围内移动认为是水平移动
if (x thisX) {
// right
if (selectedFiles != null
fileIndex selectedFiles.length - 1) {
fileIndex++;
}
} else {
// left
if (selectedFiles != null fileIndex 0) {
fileIndex--;
}
}
} else {
if (x thisX) {
// 右下
width += 20;
height += 20;
} else {
// 左上
width -= 20;
height -= 20;
}
}
repaint();
}
class ImagePanel extends JPanel {
public void paint(Graphics g) {
super.paint(g);
if (selectedFiles != null) {
ImageIcon icon = new ImageIcon(selectedFiles[fileIndex]
.getPath());
g.drawImage(icon.getImage(), 0, 0, width, height, this);
}
}
}
}
找到了,很久以前写的一个简单画图,呵呵~当时要求用AWT写,很难受。
package net.miqiang.gui;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
/**
* 简单画图板程序
* 好久没用 AWT 了,写起来真别扭,如果用 swing 会很舒服,有空再改写吧。
*
* @author 米强
*
*/
public class TestMain extends Frame {
// 画板
private Palette palette = null;
// 显示当前颜色的面板
private Panel nonceColor = null;
// 画笔粗细
private Label drawWidth = null;
// 画笔端点的装饰
private Label drawCap = null;
// 选取颜色按钮的监听事件类
private ButtonColorAction buttonColorAction = null;
// 鼠标进入按钮后光标样式的监听事件类
private ButtonCursor buttonCursor = null;
// 画笔样式的监听事件
private ButtonStrokeAction buttonStrokeAction = null;
/**
* 构造方法
*
*/
public TestMain() {
// 设置标题栏文字
super("简易画图板");
// 构造一个画图板
palette = new Palette();
Panel pane = new Panel(new GridLayout(2, 1));
// 画笔颜色选择器
Panel paneColor = new Panel(new GridLayout(1, 13));
// 12 个颜色选择按钮
Button [] buttonColor = new Button[12];
Color [] color = {Color.black, Color.blue, Color.cyan, Color.darkGray, Color.gray, Color.green, Color.magenta, Color.orange, Color.pink, Color.red, Color.white, Color.yellow};
// 显示当前颜色的面板
nonceColor = new Panel();
nonceColor.setBackground(Color.black);
paneColor.add(nonceColor);
buttonColorAction = new ButtonColorAction();
buttonCursor = new ButtonCursor();
for(int i = 0; i buttonColor.length; i++){
buttonColor[i] = new Button();
buttonColor[i].setBackground(color[i]);
buttonColor[i].addActionListener(buttonColorAction);
buttonColor[i].addMouseListener(buttonCursor);
paneColor.add(buttonColor[i]);
}
pane.add(paneColor);
// 画笔颜色选择器
Panel paneStroke = new Panel(new GridLayout(1, 13));
// 控制画笔样式
buttonStrokeAction = new ButtonStrokeAction();
Button [] buttonStroke = new Button[11];
buttonStroke[0] = new Button("1");
buttonStroke[1] = new Button("3");
buttonStroke[2] = new Button("5");
buttonStroke[3] = new Button("7");
buttonStroke[4] = new Button("9");
buttonStroke[5] = new Button("11");
buttonStroke[6] = new Button("13");
buttonStroke[7] = new Button("15");
buttonStroke[8] = new Button("17");
buttonStroke[9] = new Button("■");
buttonStroke[10] = new Button("●");
drawWidth = new Label("3", Label.CENTER);
drawCap = new Label("●", Label.CENTER);
drawWidth.setBackground(Color.lightGray);
drawCap.setBackground(Color.lightGray);
paneStroke.add(drawWidth);
for(int i = 0; i buttonStroke.length; i++){
paneStroke.add(buttonStroke[i]);
buttonStroke[i].addMouseListener(buttonCursor);
buttonStroke[i].addActionListener(buttonStrokeAction);
if(i = 8){
buttonStroke[i].setName("width");
}else{
buttonStroke[i].setName("cap");
}
if(i == 8){
paneStroke.add(drawCap);
}
}
pane.add(paneStroke);
// 将画笔颜色选择器添加到窗体中
this.add(pane, BorderLayout.NORTH);
// 将画图板添加到窗体中
this.add(palette);
// 添加窗口监听,点击关闭按钮时退出程序
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// 设置窗体 ICON 图标
this.setIconImage(Toolkit.getDefaultToolkit().createImage("images/palette.png"));
// 设置窗口的大小
this.setSize(new Dimension(400, 430));
// 设置窗口位置,处于屏幕正中央
this.setLocationRelativeTo(null);
// 显示窗口
this.setVisible(true);
}
/**
* 程序入口
*
* @param args
* 字符串数组参数
*/
public static void main(String[] args) {
new TestMain();
}
/**
* 选取颜色按钮的监听事件类
* @author 米强
*
*/
class ButtonColorAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
Color color_temp = ((Button)e.getSource()).getBackground();
nonceColor.setBackground(color_temp);
palette.setColor(color_temp);
}
}
/**
* 鼠标进入按钮变换光标样式监听事件类
* @author 米强
*
*/
class ButtonCursor extends MouseAdapter {
public void mouseEntered(MouseEvent e) {
((Button)e.getSource()).setCursor(new Cursor(Cursor.HAND_CURSOR));
}
public void mouseExited(MouseEvent e) {
((Button)e.getSource()).setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}
/**
* 设置画笔的监听事件类
* @author 米强
*
*/
class ButtonStrokeAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
Button button_temp = (Button) e.getSource();
String name = button_temp.getName();
if(name.equalsIgnoreCase("width")){
drawWidth.setText(button_temp.getLabel());
palette.setStroke(Float.parseFloat(button_temp.getLabel()));
}else if(name.equalsIgnoreCase("cap")){
drawCap.setText(button_temp.getLabel());
if(button_temp.getLabel().equals("■")){
palette.setStroke(BasicStroke.CAP_SQUARE);
}else if(button_temp.getLabel().equals("●")){
palette.setStroke(BasicStroke.CAP_ROUND);
}
}
}
}
}
/**
* 画板类
*
* @author 米强
*
*/
class Palette extends Panel implements MouseListener, MouseMotionListener {
// 鼠标 X 坐标的位置
private int mouseX = 0;
// 上一次 X 坐标位置
private int oldMouseX = 0;
// 鼠标 Y 坐标的位置
private int mouseY = 0;
// 上一次 Y 坐标位置
private int oldMouseY = 0;
// 画图颜色
private Color color = null;
// 画笔样式
private BasicStroke stroke = null;
// 缓存图形
private BufferedImage image = null;
/**
* 构造一个画板类
*
*/
public Palette() {
this.addMouseListener(this);
this.addMouseMotionListener(this);
// 默认黑色画笔
color = new Color(0, 0, 0);
// 设置默认画笔样式
stroke = new BasicStroke(3.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
// 建立 1280 * 1024 的 RGB 缓存图象
image = new BufferedImage(1280, 1024, BufferedImage.TYPE_INT_RGB);
// 设置颜色
image.getGraphics().setColor(Color.white);
// 画背景
image.getGraphics().fillRect(0, 0, 1280, 1024);
}
/**
* 重写 paint 绘图方法
*/
public void paint(Graphics g) {
super.paint(g);
// 转换为 Graphics2D
Graphics2D g2d = (Graphics2D) g;
// 获取缓存图形 Graphics2D
Graphics2D bg = image.createGraphics();
// 图形抗锯齿
bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 设置绘图颜色
bg.setColor(color);
// 设置画笔样式
bg.setStroke(stroke);
// 画线,从上一个点到新的点
bg.drawLine(oldMouseX, oldMouseY, mouseX, mouseY);
// 将缓存中的图形画到画板上
g2d.drawImage(image, 0, 0, this);
}
/**
* 重写 update 方法
*/
public void update(Graphics g) {
this.paint(g);
}
/**
* @return stroke
*/
public BasicStroke getStroke() {
return stroke;
}
/**
* @param stroke 要设置的 stroke
*/
public void setStroke(BasicStroke stroke) {
this.stroke = stroke;
}
/**
* 设置画笔粗细
* @param width
*/
public void setStroke(float width) {
this.stroke = new BasicStroke(width, stroke.getEndCap(), stroke.getLineJoin());
}
/**
* 设置画笔端点的装饰
* @param cap 参考 java.awt.BasicStroke 类静态字段
*/
public void setStroke(int cap) {
this.stroke = new BasicStroke(stroke.getLineWidth(), cap, stroke.getLineJoin());
}
/**
* @return color
*/
public Color getColor() {
return color;
}
/**
* @param color 要设置的 color
*/
public void setColor(Color color) {
this.color = color;
}
public void mouseClicked(MouseEvent mouseEvent) {
}
/**
* 鼠标按下
*/
public void mousePressed(MouseEvent mouseEvent) {
this.oldMouseX = this.mouseX = mouseEvent.getX();
this.oldMouseY = this.mouseY = mouseEvent.getY();
repaint();
}
public void mouseReleased(MouseEvent mouseEvent) {
}
/**
* 鼠标进入棋盘
*/
public void mouseEntered(MouseEvent mouseEvent) {
this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
}
/**
* 鼠标退出棋盘
*/
public void mouseExited(MouseEvent mouseEvent) {
this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
/**
* 鼠标拖动
*/
public void mouseDragged(MouseEvent mouseEvent) {
this.oldMouseX = this.mouseX;
this.oldMouseY = this.mouseY;
this.mouseX = mouseEvent.getX();
this.mouseY = mouseEvent.getY();
repaint();
}
public void mouseMoved(MouseEvent mouseEvent) {
}
}