import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest extends JFrame {
public ButtonTest() {
setTitle("wo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTest();
setVisible(true);
}
public void setTest() {
setSize(300, 200);
makeB("Y",Color.YELLOW);
}
public void makeB(String n, final Color c) {
JButton a = new JButton(n);
add(a);
a.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setBackground(c);
}
});
}
public static void main(String[] args) {
new ButtonTest();
}
}请问我这个设置出来的背景怎么颜色不对呢?
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest extends JFrame {
public ButtonTest() {
setTitle("wo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTest();
setVisible(true);
}
public void setTest() {
setSize(300, 200);
makeB("Y",Color.YELLOW);
}
public void makeB(String n, final Color c) {
JButton a = new JButton(n);
add(a);
a.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setBackground(c);
}
});
}
public static void main(String[] args) {
new ButtonTest();
}
}请问我这个设置出来的背景怎么颜色不对呢?
我现在也学到这了这个地方比较迷惑!你加我的qq群咱们交流! 群号:74552467; 衷心的欢迎各位学习java的朋友加入!
setBackground(c);
改为
((JPanel)getContentPane()).setOpaque(false);
getRootPane().setBackground(c);
要么
getContentPane().setBackground(c);
要么
((JPanel)getContentPane()).setOpaque(false);
getRootPane().setBackground(c);
支持
改下代码,就可以看到了: public void makeB(String n, final Color c) {
JButton a = new JButton(n);
add(a,BorderLayout.NORTH);
a.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getContentPane().setBackground(c);
}
});
}
但是那个设置JPanel为opaque的方法还是没用
这个是什么原因呢?
class TranslucentButton extends JButton {
public TranslucentButton(){super()};
public TranslucentButton(String label) {super(label);}
public TranslucentButton(Icon icon){super(icon);}
public TranslucentButton(Action action){super(action);}
public TranslucentButton(String label,Icon icon){super(label,icon);} @Override
public void paintComponent(Graphics g) {
BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics gButton = image.getGraphics();
gButton.setClip(g.getClip()); // Have the superclass render the button for us
super.paintComponent(gButton); Graphics2D g2d = (Graphics2D) g;
AlphaComposite newComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .6f);
g2d.setComposite(newComposite); // Copy the button's image to the destination graphics, translucently
g2d.drawImage(image, 0, 0, null);
} private static final long serialVersionUID = 23239L;
}
class TranslucentButton extends JButton
{
public TranslucentButton(){super();}
public TranslucentButton(String label) {super(label);}
public TranslucentButton(Icon icon) {super(icon);}
public TranslucentButton(Action action){super(action);}
public TranslucentButton(String label,Icon icon){super(label,icon);} @Override
public void paintComponent(Graphics g)
{
BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics gButton = image.getGraphics();
gButton.setClip(g.getClip());
// Have the superclass render the button for us
super.paintComponent(gButton);
Graphics2D g2d = (Graphics2D) g;
AlphaComposite newComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .6f);
g2d.setComposite(newComposite);
// Copy the button's image to the destination graphics, translucently
g2d.drawImage(image, 0, 0, null);
}
private static final long serialVersionUID = 23239L;
}