① java swing設置成圖片那種選擇框,怎麼弄
自己使用一個窗口去枚舉各圖片,讓用戶選擇
~~~~~~~
~~~~~~~~~
~
~~~~
~~~~~~~
② java加入圖片的方法
("String「)
("pictrue/pic_"+pictureID+"_"+(i*3+j+1)+".jpg");
上面兩個都是一樣的字元串的意思,只要new ImageIcon("")不報錯就沒有問題,不要去糾結加不加引號的問題
我看的pic_1_2等都是沒有後綴的,是否文件名有問題,或者在仔細檢查一下路徑
③ java小程序如何實現以下操作:點擊菜單選項中的「選擇圖片」出現一個打開文件對話框,可以自己選擇圖片
自己寫的,不過有一點就是圖片的大小是固定的
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
public class Tupian extends JFrame implements ActionListener{
JLabel jl=new JLabel("圖片");
JMenuBar jmb=new JMenuBar();
JMenu jm=new JMenu("文件");
JMenuItem jmi=new JMenuItem("選擇圖片");
JPanel jp=new JPanel(new FlowLayout(FlowLayout.CENTER));
public Tupian() {
super("瀏覽圖片");
jmb.add(jm);
jm.add(jmi);
jp.add(jl);
jmi.addActionListener(this);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
this.add(jmb,BorderLayout.NORTH);
this.add(jp,BorderLayout.CENTER);
this.setSize(800,600);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
JFileChooser chooser=new JFileChooser();
int i=chooser.showOpenDialog(this);
if(i==chooser.APPROVE_OPTION){
Image image=new ImageIcon(chooser.getSelectedFile().getPath()).getImage();//調整
image=image.getScaledInstance(300, 400, Image.SCALE_DEFAULT );//圖像300,400
jl.setIcon(new ImageIcon(image));
jl.setText("");
}
if(i==chooser.CANCEL_OPTION)return;
}
public static void main (String[] args) {
new Tupian();
}
}
④ 提問了很多次的問題,Java,如何修改下面的程序,才能讓圖片可以選擇
供你參考一下吧。。。。
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.awt.image.BufferedImage;
{
JPaneljPanel;
JLabeljLabel;
Imageimage;
ImageIconimageIcon;
JButtonjButton;
floatwww=1;
floathhh=1;
publiclianxi3(){
super("圖片");
jButton=newJButton("變大");
jButton.addActionListener(this);
image=Toolkit.getDefaultToolkit().getImage("src/img1.jpg");
jLabel=newJLabel();
jPanel=newJPanel();
imageIcon=newImageIcon(image);
jLabel.setIcon(imageIcon);
jPanel.add(jLabel);
this.add(jLabel,BorderLayout.NORTH);
this.add(jButton,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(600,600);
}
publicstaticvoidmain(String[]args){
newlianxi3();
}
publicvoidbig(){
intwidth=image.getHeight(jLabel);
intheight=image.getWidth(jLabel);
doublebigger=1.5;
www=(float)(width*bigger);
hhh=(float)(height*bigger);
BufferedImagebufImg=newBufferedImage(image.getWidth(null),image.getHeight(null),BufferedImage.TYPE_4BYTE_ABGR);
Graphicsg=bufImg.createGraphics();
g.drawImage(image,0,0,null);
g.dispose();
imageIcon.setImage(rotateImage(bufImg,30));
//imageIcon.setImage(imageIcon.getImage().getScaledInstance((int)www,(int)hhh,Image.SCALE_DEFAULT));
jLabel.setIcon(imageIcon);
jLabel.setSize((int)www,(int)hhh);
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==jButton){
big();
System.out.println("aaa");
}
}
(,
finalintdegree){
intw=bufferedimage.getWidth();
inth=bufferedimage.getHeight();
intnw=(int)(bufferedimage.getWidth()*1.414);
intnh=(int)(bufferedimage.getHeight()*1.414);
inttype=bufferedimage.getColorModel().getTransparency();
BufferedImageimg;
Graphics2Dgraphics2d;
(graphics2d=(img=newBufferedImage(nw,nh,type))
.createGraphics()).setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(degree),nw/2,nh/2);
graphics2d.translate(nw/2-w/2,nh/2-h/2);
graphics2d.drawImage(bufferedimage,0,0,null);
graphics2d.dispose();
returnimg;
}
}
⑤ 如何在Java程序中選擇添加圖片,再存到資料庫中
存儲圖片:
//載入驅動程序類
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:denglu","sa","sa");
//建立資料庫聯機,其中denglu為資料庫名,sa為連接資料庫的帳號及密碼。
Statement stmt=con.createStatement(); //建立Statement對象
FileInputStream str=new FileInputStream(filename); //圖片文件路徑
String sql="insert into picturenews(id,image) values(?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1,id); //ID號
pstmt.setBinaryStream(2,str,str.available()); //圖片數據
pstmt.execute();
//將數據存入資料庫
out.println("Success,You Have Insert an Image Successfully");
圖片讀取:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:denglu","sa","sa");
Statement stmt=con.createStatement();
ResultSet rs=null;
String sql = "select image from picturenews WHERE id="+id;
rs=stmt.executeQuery(sql);
if(rs.next()) {
//圖片輸出的輸出流
InputStream in = rs.getBinaryStream("image");
byte b[] = new byte[0x7a120];
for(int i = in.read(b); i != -1;)
{
//將緩沖區的輸入輸出到頁面
in.read(b);
}
}
/**
* 獲得數據後可以按照自己的方法進行處理或者顯示
*/
JLabel label=new JLabel(new ImageIcon(b)); //用JLabel進行顯示
.....
⑥ 選擇圖片在面板顯示,java代碼如何實現
是這樣的,你在面板上搞一個和面板一樣大的JLabel
然後,通過JFileChooser獲得路徑,利用這個圖片的路徑,構建一個ImageIcon
最後,根據這個ImageIcon去給JLabel對象setIcon(ImageIcon對象);
具體地:
1.panel.add(label,BorderLayout.CENTER);
2.ImageIcon
icon
=
new
ImageIcon(url);
3.label.setIcon(icon);
⑦ 選擇圖片在面板顯示,java代碼如何實現
是這樣的,你在面板上搞一個和面板一樣大的JLabel
然後,通過JFileChooser獲得路徑,利用這個圖片的路徑,構建一個ImageIcon
最後,根據這個ImageIcon去給JLabel對象setIcon(ImageIcon對象);
具體地:
1.panel.add(label,BorderLayout.CENTER);
2.ImageIcon icon = new ImageIcon(url);
3.label.setIcon(icon);
⑧ java截取圖片
呵呵,很明確的告訴你:可以!
代碼半小時後出來!!!
……
終於出來了(呵呵,好像超過了半小時哈)且看代碼:
importjava.awt.Color;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjavax.imageio.ImageIO;
importjavax.swing.JApplet;
publicclassTestextendsJApplet{
Stringaddrs="F:\images\mm.bmp";//改成自己的圖片路徑
BufferedImagemm,child;
CutImageci;
publicTest(){
try{
mm=ImageIO.read(newFile(addrs));
}catch(IOExceptione){
System.out.println("圖片讀取失敗!");
e.printStackTrace();
}
ci=newCutImage(mm);
child=ci.getChildImage(50,0,150,220);
}
publicvoidinit(){
}
publicvoidpaint(Graphicsg){
g.setColor(Color.red);
g.drawString("原圖:",0,10);
g.drawImage(mm,20,10,this);
g.drawString("ci.getChildImage(50,0,150,220)截取後的圖片",mm.getWidth()+30,10);
g.drawImage(child,mm.getWidth()+50,20,this);
}
}
importjava.awt.Image;
importjava.awt.image.BufferedImage;
publicclassCutImage{
privateBufferedImageimg;
privateBufferedImagechild;
publicCutImage(){
}
publicCutImage(BufferedImageim){
img=im;
}
publicCutImage(Imageim){
img=(BufferedImage)im;
}
publicvoidsetImg(BufferedImageimg){
this.img=img;
}
(intx,inty,intwidth,intheight){
intcw=width;
intch=height;
intpw=img.getWidth();
intph=img.getHeight();
if(pw<x+width){
System.out.println("給出的參數超出原圖片的范圍!程序會自動減小寬度或高度");
cw=pw-x;
}
if(ph<y+height){
System.out.println("給出的參數超出原圖片的范圍!程序會自動減小寬度或高度");
ch=ph-y;
}
child=newBufferedImage(cw,ch,BufferedImage.TYPE_INT_ARGB);
for(inti=0;i<ch;i++){
for(intj=0;j<cw;j++){
child.setRGB(j,i,img.getRGB(x+j,y+i));
}
}
returnchild;
}
}
呵呵,希望樓主能夠滿意哦,如果你願意的話,稍微改一下代碼就可以把截取的圖片child報春到你的電腦上了。下面程序的運行效果吧!
⑨ JAVA點擊"打開"按鈕,選擇圖片並打開
聲明一個JFileChooser 對象(這里假設取名jfc)後 調用jfc的showOpenDialog。之後。貌似返回int值。
根據int 值判斷是選擇的文件還是取消了選擇。
然後用jfc的方法獲取文件名和路徑。
然後使用Runtime.getRuntime().exec();方法來執行程序(這里程序改成一個圖片文件,不知是否會啟動默認工具,這個我沒嘗試,只是我的思路,如有錯誤,請諒解,僅供參考!)。
這個是我通過搜索一系列網頁,查出來的結果。
不保證正確性。希望對你有所幫助。
不保證正確,僅供參考。
⑩ JAVA編程,用戶選擇圖片格式
文件保存什麼格式?你可以嘗試獲取文件名的擴展名,來決定存儲什麼類型.
String fileType=fileName.substring(fileName.lastIndexOf("."));
System.out.println(fileType);//列印出來
if(fileType.equals("psd")){
//如何這里不寫了,你自己寫下
}else if (fileType.equals("psd")){
//如何這里不寫了,你自己寫下
}