‘壹’ 怎么在java里添加背景图片
可以新建个面板,在面板里放入带图片的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);
‘贰’ java窗体如何添加背景图片
‘叁’ Java中如何做到单击按钮后更换背景图片
function mychange(num){ document.getElementById("div1").style.backgroundImage="url("+num+".jpg)"; document.getElementById("div2").style.backgroundImage="url("+num+".jpg)"; }
‘肆’ JAVA里Button按钮背景图片
何必要用java api的按钮呢,多不好看,自己写一个button
代码如下
package GUI;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
public class GameButton extends JButton {
/**
* 单人游戏的按钮
*/
public static final int SINGLE = 1;
/**
* 多人游戏按钮
*/
public static final int MULTI = 2;
/**
* 回放游戏的按钮
*/
public static final int REPLAY = 3;
/**
* 排行榜的按钮
*/
public static final int RECORD = 4;
/**
* 离开游戏的按钮
*/
public static final int EXIT = 5;
/**
* 新游戏的按钮
*/
public static final int NEW_GAME = 6;
/**
* 暂停游戏的按钮
*/
public static final int PAUSE_GAME = 7;
/**
* 继续游戏的按钮
*/
public static final int CONTINUE_GAME = 8;
/**
* 设置游戏的按钮
*/
public static final int SET_GAME = 9;
/**
* 返回的按钮
*/
public static final int BACK = 10;
/**
* 世界的按钮
*/
public static final int WORLD = 11;
/**
* 聊天的按钮
*/
public static final int TALK = 12;
/**
* 竞赛的按钮
*/
public static final int COMPETITION = 13;
/**
* 登陆的按钮
*/
public static final int LOGIN = 14;
/**
* 注册的按钮
*/
public static final int REGISTER = 15;
/**
* 保存游戏的按钮
*/
public static final int SAVE_FILE = 16;
/**
* 游戏回放的按钮
*/
public static final int GAME_REPLAY = 17;
/**
* 选择文件的按钮
*/
public static final int CHOOSE_FILE = 18;
/**
* 按钮的图片
*/
private Image[] gameButtonImage;
/**
* 侦测点击事件
*/
private Shape shape;
/**
* 按钮的类型;
*/
private int type;
/**
* 按钮状态
*/
private int state;
/**
* 进入按钮音效
*/
private AudioClip mouseEnterMusic;
/**
* 按下按钮的音效
*/
private AudioClip mousePressMusic;
/**
* 带参数的构造函数
*
* @param type
* 按钮的类型
*/
public GameButton(int type) {
this.type = type;
this.state = 0;
mouseEnterMusic = Applet.newAudioClip(getClass().getResource(
"/Audio/MouseEnter.wav"));
mousePressMusic = Applet.newAudioClip(getClass().getResource(
"/Audio/MousePress.wav"));
gameButtonImage = new Image[3];
for (int i = 0; i < gameButtonImage.length; i++) {
gameButtonImage[i] = new ImageIcon(this.getClass().getResource(
"/Image/Button/" + type + "-" + (i + 1) + ".png"))
.getImage();
}
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent arg0) {
state = 2;
repaint();
mousePressMusic.play();
}
@Override
public void mouseExited(MouseEvent arg0) {
state = 0;
repaint();
}
@Override
public void mouseEntered(MouseEvent arg0) {
state = 1;
repaint();
mouseEnterMusic.play();
}
public void mouseReleased(MouseEvent arg0) {
state = 1;
repaint();
}
});
this.setCursor(new Cursor(Cursor.HAND_CURSOR));
// 这些声明把按钮扩展为一个圆而不是一个椭圆。
Dimension size = getPreferredSize();
size.width = size.height = 3 * Math.max(size.width, size.height);
setPreferredSize(size);
// 这个调用使JButton不画背景,而允许我们画一个圆的背景。
setContentAreaFilled(false);
}
/**
* 如果按钮改变大小,产生一个新的形状对象。
*/
public boolean contains(int x, int y) {
if (shape == null || !shape.getBounds().equals(getBounds())) {
shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());
}
return shape.contains(x, y);
}
/**
* 重载JButton中的画方法
*/
protected void paintComponent(Graphics g) {
JPanel panel = new JPanel();
panel.setOpaque(false);
g.drawImage(gameButtonImage[state], 0, 0, getSize().width - 1,
getSize().height - 1, panel);
}
/**
* 重载按钮的边界,但是不画任何东西
*/
protected void paintBorder(Graphics g) {
}
/**
* 初始化按钮
*/
public void reInit() {
this.state = 0;
repaint();
}
}
你只需要把图片的路径改下,我这个按钮是圆形的按钮,还有音乐的添加哦,例如按按钮后会有声音,当然你也可以把按钮声音去掉,还有这个按钮可以控制大小,按钮时圆的还是方的,都是很容易控制的哦,呵呵
‘伍’ java Button背景图片的设置
import javax.swing.ImageIcom;
ImageIcon icon = new ImageIcon("图片路径");
jbutton.setIcon(icon);
‘陆’ java按钮上加了背景图片为文字就被覆盖了要怎么办 我不想用ps在图片上加字
图片、文字,都自己draw上,,,,,,先draw图片、再draw文字
‘柒’ java按钮背景图片上文字的问题
同问题,找了好久终于解决了。当你设置好Button的文本和背景图后,在你的按钮控件代码中加上btnNewButton.setHorizontalTextPosition(SwingConstants.CENTER);注意!!!btnNewButton是我按钮的实例化对象名,用这个指令时把你的实例化按钮对象名替换了btnNewButton就好了
‘捌’ 在Java应用程序中,如何实现在背景图片上显示按钮,也就是在背景图片上添加个按钮
用JLabel显示图片,在JLabel上添加按钮
例如:
JLabel lblImg = new JLabel(new ImageIcon("iimg/tupian.png"));
JButton btn = new JButton();
lblImg.add(btn);
‘玖’ 在Java中如何给我的动作按钮添加图片作为背景(如下图)
publicclassCreateIcon{
staticStringpath=System.getProperty("user.dir")+"\WebRoot\Img\";
publicstaticImageIconadd(StringImageName){
ImageIconicon=newImageIcon(path+ImageName);
returnicon;
}
}
先定义一个方法,然后下面调用,注意把图片放在项目的WebRoot\Img的目录下即可
finalJLabellabel=newJLabel();
ImageIconloginIcon=CreateIcon.add("backImg.jpg");
label.setIcon(loginIcon);
‘拾’ JAVA点击按钮改变背景图片 跪求代码·
//如果建工程,图片放到工程根目录
importjava.awt.Container;
importjava.awt.FlowLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.ImageIcon;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
{
publicStudent(){
init();
}
privatevoidinit(){
finalContainerc=getContentPane();
finalJLabelimgLabel=newJLabel();
JButtonbtnPic=newJButton("换背景图片");
btnPic.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
ImageIconimg=newImageIcon(System.getProperty("user.dir")+"\a.png");
imgLabel.setIcon(img);
imgLabel.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
}
});
((JPanel)getContentPane()).setOpaque(false);
getLayeredPane().add(imgLabel,newInteger(Integer.MIN_VALUE));
setLayout(newFlowLayout());
add(btnPic);
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicstaticvoidmain(String[]args){
newStudent().setVisible(true);
}