BufferedImage image=ImageIO.read(new File(fileName));//读取图片
JLabel jl=new JLabel(new ImageIcon(image)); //把图片放在JLABEL中显示
.
.
.
Graphics2D g=image.createGraphics();
g.drawString("为啥不好用呢",0,0);
g.dispose();大概代码就是这样了,所有组件放在一个JFRAME中。无论怎么弄这行字也整不到图片上,劳烦各位大大指条明路吧。
JLabel jl=new JLabel(new ImageIcon(image)); //把图片放在JLABEL中显示
.
.
.
Graphics2D g=image.createGraphics();
g.drawString("为啥不好用呢",0,0);
g.dispose();大概代码就是这样了,所有组件放在一个JFRAME中。无论怎么弄这行字也整不到图片上,劳烦各位大大指条明路吧。
解决方案 »
- 类型在没有被完全初始化之前就生成实例对象
- 难题,高手进来解决一下证明你的实力
- 一个字符串截取的问题
- 各位大侠好 我刚学JAVA 就是在带参数的方法不知道怎么应用好
- 树形实例算法续
- <thinking in java>学习笔记跟大家一起分享,有什么不对的请指出.
- 新手:这个函数为什么不能关闭窗口
- 么用swing做一个报表式的录入,跪求——图样在附件中,大家看看用什么控件做合适。
- 谁能告诉我java的socket是非阻塞的吗?就是读的同时能写!!!很急哦!
- ObjectInputStream 遍历读取.tmp中的类对象
- 有Delphi编程基础,不懂c/c++,可以不可以直接学习Java
- 求助:怎样控制JTextField中输入字符的长度?
g.drawString("为啥不好用呢",0,0);0,0划在一个点了
改成了g.drawString("为啥不好用呢",50,100);还是不好用。。
.
.
.
Graphics2D g=image.createGraphics();
g.drawString("为啥不好用呢",0,0);
g.dispose();
JLabel jl=new JLabel(new ImageIcon(image)); //把图片放在JLABEL中显示----------------这样你试试!
完整代码如下。。JDK6,各位大大们再帮帮忙吧,小弟脑瓜都炸了package testapp;import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;import javax.imageio.ImageIO;
import javax.swing.*;import sun.awt.image.GifImageDecoder;
import sun.awt.image.InputStreamImageSource;public class ImageFrame extends JFrame { BufferedImage image;
//Image image; ImagePanel l; Panel pl;
//JPanel l; Container c; JPanel p; JButton jx; JTextField jtf; Panel pjx; public ImageFrame() {
super(); this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
ImageFrame.this.setVisible(false);
ImageFrame.this.dispose();
}
}); l = new ImagePanel();
l.setLayout(new BorderLayout()); jx = new JButton("Change"); jtf = new JTextField(6); pjx = new Panel(); JButton b1 = new JButton("OPEN");
b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
// TODO 自動生成されたメソッド・スタブ
FileDialog d = new FileDialog(ImageFrame.this, "Load", FileDialog.LOAD);
//d.setSize(300,300);
d.setLocation(100, 100);
d.show(); String dir = d.getDirectory();
String fileName = d.getFile(); if (fileName != null) {
if (!(fileName.endsWith(".gif") || fileName.endsWith(".GIF"))) {
JOptionPane jd = new JOptionPane("Error", JOptionPane.INFORMATION_MESSAGE);
jd.showMessageDialog(ImageFrame.this, "Please Select a gif/GIF file"); this.actionPerformed(e);
} else {
try {
// Graphics g=l.getGraphics();
// BufferedImage image=(BufferedImage) Toolkit.getDefaultToolkit().getImage(dir+fileName);
// System.out.println(image.getHeight());
// l.paint(g,image,200,200); // p.add(l,"Center");
// c.add(p);
// ImageFrame.this.setVisible(true); //p.add(l,"Center");
//c.add(p); image = ImageIO.read(new File(dir + fileName));
JLabel jl = new JLabel(new ImageIcon(image));
l.removeAll();
l.add(new Label(""), "North");
l.add(jl, "Center");
JPanel px = new JPanel();
px.setLayout(new BorderLayout());
px.add(new JLabel(" X:" + image.getHeight() + " Y:" + image.getWidth()), "Center");
pjx.add(jtf);
pjx.add(jx);
px.add(pjx, "South");
pl.add(px, "East");
ImageFrame.this.setVisible(true);
} catch (Exception x) {
System.out.println(x);
}
}
} else {
return;
}
} });
JButton b2 = new JButton("SAVE");
b2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
// TODO 自動生成されたメソッド・スタブ
FileDialog d = new FileDialog(ImageFrame.this, "Save", FileDialog.SAVE);
//d.setSize(300,300);
d.setLocation(100, 100);
d.show(); String dir = d.getDirectory();
String fileName = d.getFile(); try {
ImageIO.write(image, "GIF", new File(dir + fileName));
System.out.println("ok");
} catch (IOException e1) {
// TODO 自動生成された catch ブロック
e1.printStackTrace();
}
} }); jx.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
// TODO 自動生成されたメソッド・スタブ
if (image != null) {
String value = jtf.getText();
Graphics2D g = image.createGraphics();
// System.out.print(image.);
// System.out.println(" "+image.getMinY());
//Graphics g=image.getGraphics();
//g.setFont(new Font("宋体", Font.PLAIN, 18)); g.drawString(value, 50, 100); //image.flush(); g.dispose();
JLabel jl = new JLabel(new ImageIcon(image));
l.add(jl, "Center");
ImageFrame.this.setVisible(true);
//ImageFrame.this.setVisible(true);
}
} }); p = new JPanel();
p.setBackground(Color.WHITE);
p.setLayout(new BorderLayout()); pl = new Panel();
pl.setLayout(new BorderLayout());
pl.add(l, "Center");
p.add(pl, "Center"); JPanel p2 = new JPanel();
//p2.setBackground(Color.WHITE);
p2.add(b1);
p2.add(b2); p.add(p2, "South"); JPanel blank = new JPanel();
blank.setLayout(new BorderLayout());
blank.add(new Label(""), "North");
blank.add(new Label(""), "Center");
blank.add(new Label(""), "South"); p.add(blank, "North"); c = this.getContentPane();
c.add(p); this.setResizable(false);
this.setSize(800, 600);
this.setLocation(150, 80);
this.setVisible(true);
}}