java中图片插入怎么插入,看代码,图片有个专门的包,怎么调用包里面的图片
packageUI;importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;importimage.*;p...
package UI;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import image.*;
public class Ui extends JFrame implements ActionListener{
private static final long serialVersionUID = 590895195330246427L;
private JButton up;
private JButton down;
private JButton left;
private JButton right;
private JTextField text;
private Container con=getContentPane();
int x,y;
JPanel panel;
public Ui(){
super("位图按钮");
setSize(260,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//向上移动设置
Icon iup=new ImageIcon("up.jpg");
up=new JButton(iup);
up.addActionListener(this);
up.setToolTipText("向上移动");
//向下移动设置
Icon idown=new ImageIcon("down.jpg");
down=new JButton(idown);
down.addActionListener(this);
down.setToolTipText("向下移动");
//向左移动设置
Icon ileft=new ImageIcon("left.jpg");
left=new JButton(ileft);
left.setToolTipText("向左移动");
left.addActionListener(this);
//向右移动设置
Icon iright=new ImageIcon("right.jpg");
right=new JButton(iright);
right.setToolTipText("向右移动");
right.addActionListener(this);
//中间文本区域
text=new JTextField("位图按钮实例");
panel=new JPanel();
}
public void setLayout(){
con.setLayout(new BorderLayout());
con.add(up,BorderLayout.NORTH);
con.add(down,BorderLayout.SOUTH);
con.add(left,BorderLayout.WEST);
con.add(right,BorderLayout.EAST);
panel.setLayout(null);
panel.add(text);
x=0; y=0;
text.setBounds(x, y, 80, 20);//此处依据xy坐标轴,y代表上距离,x代表左距离
con.add(panel,BorderLayout.CENTER);}
@SuppressWarnings("deprecation")
public static void main(String[] args) {
// TODO Auto-generated method stub
Ui ui=new Ui();
ui.setLayout();
ui.show();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==up){
y-=2;
}
if(e.getSource()==down){
y+=2;
}
if(e.getSource()==left){
x-=2;
}
if(e.getSource()==right){
x+=2;
}
text.setBounds(x, y,80, 20);
}
} 展开
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import image.*;
public class Ui extends JFrame implements ActionListener{
private static final long serialVersionUID = 590895195330246427L;
private JButton up;
private JButton down;
private JButton left;
private JButton right;
private JTextField text;
private Container con=getContentPane();
int x,y;
JPanel panel;
public Ui(){
super("位图按钮");
setSize(260,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//向上移动设置
Icon iup=new ImageIcon("up.jpg");
up=new JButton(iup);
up.addActionListener(this);
up.setToolTipText("向上移动");
//向下移动设置
Icon idown=new ImageIcon("down.jpg");
down=new JButton(idown);
down.addActionListener(this);
down.setToolTipText("向下移动");
//向左移动设置
Icon ileft=new ImageIcon("left.jpg");
left=new JButton(ileft);
left.setToolTipText("向左移动");
left.addActionListener(this);
//向右移动设置
Icon iright=new ImageIcon("right.jpg");
right=new JButton(iright);
right.setToolTipText("向右移动");
right.addActionListener(this);
//中间文本区域
text=new JTextField("位图按钮实例");
panel=new JPanel();
}
public void setLayout(){
con.setLayout(new BorderLayout());
con.add(up,BorderLayout.NORTH);
con.add(down,BorderLayout.SOUTH);
con.add(left,BorderLayout.WEST);
con.add(right,BorderLayout.EAST);
panel.setLayout(null);
panel.add(text);
x=0; y=0;
text.setBounds(x, y, 80, 20);//此处依据xy坐标轴,y代表上距离,x代表左距离
con.add(panel,BorderLayout.CENTER);}
@SuppressWarnings("deprecation")
public static void main(String[] args) {
// TODO Auto-generated method stub
Ui ui=new Ui();
ui.setLayout();
ui.show();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==up){
y-=2;
}
if(e.getSource()==down){
y+=2;
}
if(e.getSource()==left){
x-=2;
}
if(e.getSource()==right){
x+=2;
}
text.setBounds(x, y,80, 20);
}
} 展开
2个回答
推荐于2018-03-31
展开全部
import image.*; 这句我没看懂,java的图像类Image被封装在java.awt中了,所以这句是有问题的,应去掉。在eclipse中也会报错。
至于“图片有个专门的包,怎么调用包里面的图片”,我没懂你的意思。JAVA中image类不提供任何图片,所以请说明你的意思。
至于“图片有个专门的包,怎么调用包里面的图片”,我没懂你的意思。JAVA中image类不提供任何图片,所以请说明你的意思。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询