我想在JFrame中使用一个JComboBox来更换背景图片,现在问题是,只能选一个背景,然后就不能更改了,我估计是第一次选的盖住了第二次选的,求各位大虾看看import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;public class Frame1 { public static void main(String args[]) {
MyFrame1 f = new MyFrame1();
f.setSize(new Dimension(380, 620));
}
}class MyFrame1 extends JFrame {
JComboBox jc = new JComboBox(); MyFrame1() {
Container c = getContentPane();
JPanel jp = new JPanel();
jp.setOpaque(false);
c.add(jp); jp.setLayout(null);
jc.addItem("bk1");
jc.addItem("bk2");
jc.addItem("bk3");
jp.add(jc);
jc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
setBak(jc.getSelectedIndex());
}
});
jp.add(jc);
jc.setBounds(300, 500, 50, 20); pack();
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void setBak(int x) {
/* getLayeredPane().remove(getLayeredPane().);*/
((JPanel) this.getContentPane()).setOpaque(false);
ImageIcon img = new ImageIcon("c://bg" + x + ".jpg");
JLabel background = new JLabel(img);
this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
repaint();
}}
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;public class Frame1 { public static void main(String args[]) {
MyFrame1 f = new MyFrame1();
f.setSize(new Dimension(380, 620));
}
}class MyFrame1 extends JFrame {
JComboBox jc = new JComboBox(); MyFrame1() {
Container c = getContentPane();
JPanel jp = new JPanel();
jp.setOpaque(false);
c.add(jp); jp.setLayout(null);
jc.addItem("bk1");
jc.addItem("bk2");
jc.addItem("bk3");
jp.add(jc);
jc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
setBak(jc.getSelectedIndex());
}
});
jp.add(jc);
jc.setBounds(300, 500, 50, 20); pack();
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void setBak(int x) {
/* getLayeredPane().remove(getLayeredPane().);*/
((JPanel) this.getContentPane()).setOpaque(false);
ImageIcon img = new ImageIcon("c://bg" + x + ".jpg");
JLabel background = new JLabel(img);
this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
repaint();
}}
或者用removeAll吧,反正layeredpane上也没其它东西
我试了下,会把那个JComboBox也会去除
ContentPane是不是在LayeredPane里面?
会把我加在contentPane里面的JComboBox也给去除了
不过每次都是在最上层加一个Lable。
自己优化哈
我在contentPane里面加的按钮,只有在new Integer(Integer.MIN_VALUE)) 的时候才能正确的显示
你为何这么做?
先说你的主要问题。为什么每次都new一个JLabel?用不着吧。第一次new出来以后使用就得了。
第二,既然你使用的是JLayeredPane。那么,这上面增加组件是要分层次的,大的显示在小的前面。
this.getLayeredPane().add(background, new Integer(IntegerMIN_VALUE)); .
实际上,你取一个最小整型不是有效的。因为取出的时候,层数最小也是0.而你添加的时候,制定一个很小的数字,而之前的最小也按照0去处理。既然你传的币0小,当然是添加到之前的label的后面去了,就被遮挡住了。
既然你已经写作如此。那就这样好了,JLabel background = new JLabel();拿到MyFrame1的类定义,作为类的成员变量。你的setBak方法加入如下一段代码:
if (layeredPane.getIndexOf(background) >= 0) {
layeredPane.remove(background);
}
jp.add(jc);
你写了两次。不过这个倒是影响不算太大。
像那种label初始化一次被用啊这种可以以后慢慢实践去习惯
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;public class BackgroundExample { public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final JFrame frame = new JFrame("Rich");
final String[] images = {"background/01.jpg",
"background/02.jpg",
"background/03.jpg",
"background/04.jpg"};
final JComboBox backgrounds;
final BackgroundPanel backgroundPanel = new BackgroundPanel();
backgrounds = new JComboBox(images);
//backgrounds.setSelectedIndex(1);
backgrounds.addActionListener(new ActionListener() { @Override
public void actionPerformed(final ActionEvent e) {
JComboBox box = (JComboBox)e.getSource();
String background = (String)box.getSelectedItem();
try {
backgroundPanel.changeBackground(background);
backgroundPanel.repaint();
} catch (IOException e1) {
e1.printStackTrace();
}
} });
frame.setUndecorated(false);
backgroundPanel.setOpaque(false);
frame.getContentPane().add(backgroundPanel,BorderLayout.CENTER);
frame.getContentPane().add(backgrounds,BorderLayout.NORTH);
frame.setBounds(100, 100, 750, 560);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class BackgroundPanel extends JPanel{ /**
*
*/
private static final long serialVersionUID = 11111111111111L;
private BufferedImage background;
private TexturePaint texture; public void changeBackground(final String image) throws IOException {
this.background = ImageIO.read(this.getClass().getClassLoader().getResource(image));
Rectangle rect = new Rectangle(0,0,this.background.getWidth(),this.background.getHeight());
this.texture = new TexturePaint(this.background,rect);
} @Override
public void paintComponent(final Graphics g) { if(this.texture!= null) {
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(this.texture);
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
} super.paintComponent(g);
}
}
classes\BackgroundExample$1$1.class
classes\BackgroundExample$1.class
classes\BackgroundExample.class
classes\BackgroundPanel.class
resources\background\01.jpg
resources\background\02.jpg
resources\background\03.jpg
resources\background\04.jpg
sources\BackgroundExample.javajavac sources\BackgroundExample.java -d classes
javaw -cp classes;resources BackgroundExamplejdk6u17
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;public class BackgroundExample { public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final JFrame frame = new JFrame("Rich");
final String[] images = {"img1.jpg",
"img2.jpg",
"img3.jpg",
"img4.jpg"};
final JComboBox backgrounds;
final BackgroundPanel backgroundPanel = new BackgroundPanel();
backgrounds = new JComboBox(images);
//backgrounds.setSelectedIndex(1);
backgrounds.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) {
JComboBox box = (JComboBox)e.getSource();
String background = (String)box.getSelectedItem();
try {
backgroundPanel.changeBackground(background);
backgroundPanel.repaint();
} catch (IOException e1) {
e1.printStackTrace();
}
} });
frame.setUndecorated(false);
backgroundPanel.setOpaque(false);
frame.getContentPane().add(backgroundPanel,BorderLayout.CENTER);
frame.getContentPane().add(backgrounds,BorderLayout.NORTH);
frame.setBounds(100, 100, 750, 560);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class BackgroundPanel extends JPanel{ /**
*
*/
private static final long serialVersionUID = 11111111111111L;
private BufferedImage background;
private TexturePaint texture; public void changeBackground(final String image) throws IOException {
this.background = ImageIO.read(this.getClass().getClassLoader().getResource(image));
Rectangle rect = new Rectangle(0,0,this.background.getWidth(),this.background.getHeight());
this.texture = new TexturePaint(this.background,rect);
} @Override
public void paintComponent(final Graphics g) { if(this.texture!= null) {
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(this.texture);
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
} super.paintComponent(g);
}
}
这样就行啦!!!
可以第一个设置为默认的!
图片 自行切换。