一个关于Jbutton颜色的问题 在线等

war384 2007-11-21 09:14:16
各位大侠:
我想改变JButton那个默认的灰色,但是用setBackground(Color.red)方法,始终不能改变,后来把那个Jbutton加了个setFocusPaint(false)方法,结果有些Jbutton 的颜色改了,有些又没有改了,有没有人能告诉我这是位什么啊?怎样改变Jbutton的颜色呢?
...全文
812 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
foreveryone 2007-11-28
  • 打赏
  • 举报
回复
哈哈,我的可以哦


war384 2007-11-23
  • 打赏
  • 举报
回复
以下是这个程序的主类,还有些类没有写出来,但是看这个已经够了.主要代码就下面这些。我想改变的是Epanel这个类中的set_Button这个按钮的颜色,灰色太难看了,哪位大侠能帮忙指点一下啊,谢谢哈...
public class ClientRun extends JFrame implements ActionListener, MouseListener{
static CPanel cpanel;
static DPanel dpanel;
static EPanel epanel;
Container container;
public ClientRun(){
setTitle("Follow Me");
try {
// 将LookAndFeel设置成Windows样式
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception ex) {
ex.printStackTrace();
}
cpanel=new CPanel();
cpanel.setLayout(null);
dpanel=new DPanel();
container=this.getContentPane();
container.setLayout(null);
container.add(cpanel);
cpanel.setSize(200,50);
cpanel.setLocation(0,0);
container.add(dpanel);
dpanel.setSize(200,484);
dpanel.setLocation(0,50);
dpanel.friend_Label.addMouseListener(this);
dpanel.crowd_Label.addMouseListener(this);
dpanel.music_Label.addMouseListener(this);
epanel=new EPanel();
container.add(epanel);
epanel.setSize(199,99);
epanel.setLocation(0,535);
epanel.setOpaque(true);
epanel.exit_Button.addActionListener(this);
setLocation(805,0);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(210,615);
setVisible(true);
}

public static void main(String [] args){
ClientRun cRun=new ClientRun();
}

public void actionPerformed(ActionEvent e) {
if(e.getSource()==epanel.exit_Button){
System.exit(0);
}

}
public void mouseClicked(MouseEvent arg) {
// TODO Auto-generated method stub
Object obj=arg.getSource();
if(obj==dpanel.friend_Label){
dpanel.cLayout.show(dpanel.center_Panel,"1");
}
if(obj==dpanel.crowd_Label){
dpanel.cLayout.show(dpanel.center_Panel,"2");
}
if(obj==dpanel.music_Label){
dpanel.cLayout.show(dpanel.center_Panel,"3");
}
}
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub

}
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}
}
class CPanel extends JPanel{
JLabel imageLabel,nameLabel;
Image image1,image2;
Font font;
public CPanel(){
try {
image1=ImageIO.read(new File(this.getClass().getResource("images/owen.bmp").getPath()));//用相对路径加载bmp文件
image2=ImageIO.read(new File(this.getClass().getResource("images/pic3.bmp").getPath()));//用绝对路径加载bmp文件
} catch (IOException e) {
e.printStackTrace();
}
font=new Font("TimesRoman",Font.BOLD,12);
imageLabel=new JLabel(new ImageIcon(image1));
nameLabel=new JLabel(" 3 2 9 8 0 6 4 7 4");
setLayout(null);
add(imageLabel);
imageLabel.setSize(60,60);
imageLabel.setLocation(0,-5);
add(nameLabel);
nameLabel.setSize(120,30);
nameLabel.setLocation(60,10);
nameLabel.setFont(font);
//setBackground(new Color(127, 255, 212));

//this.setSize(194, 54);
//setLayout(new FlowLayout());

}
public void paintComponent(Graphics g) {
ImageIcon icon =new ImageIcon(image2);
Image image2 = icon.getImage();
g.drawImage(image2,0,0,200,60,this);
}
}
class DPanel extends JPanel{
CardLayout cLayout=new CardLayout();
//JButton friend_Button,crowd_Button,music_Button;
JPanel center_Panel,button_Panel,imagePanel,listPanel1;
JLabel friend_Label,crowd_Label,music_Label;
Image image1,image2,image3,image4,image5;
JList qunlist;
DefaultListModel dm;

public DPanel(){
try {
image1=ImageIO.read(new File(this.getClass().getResource("images/west.BMP").getPath()));
image2=ImageIO.read(new File(this.getClass().getResource("images/center2.BMP").getPath()));
image3=ImageIO.read(new File(this.getClass().getResource("images/q1.BMP").getPath()));
image4=ImageIO.read(new File(this.getClass().getResource("images/q2.BMP").getPath()));
image5=ImageIO.read(new File(this.getClass().getResource("images/music.BMP").getPath()));
} catch (IOException e) {
e.printStackTrace();
}
String qun[]={" 学生群"," 教师群"," 医生群"," 军人群"," 公务员群"," 其他群"};
dm=new DefaultListModel();
for(int i=0;i<6;i++){
dm.addElement(qun[i]);
}
qunlist=new JList(dm);
friend_Label=new JLabel(new ImageIcon(image3));
button_Panel=new ImagePanel(40,500,image1);
crowd_Label=new JLabel(new ImageIcon(image4));
music_Label=new JLabel(new ImageIcon(image5));

button_Panel.setLayout(null);
button_Panel.add(friend_Label);
friend_Label.setSize(50,40);
friend_Label.setLocation(-5,14);
button_Panel.add(crowd_Label);
crowd_Label.setSize(55,40);
crowd_Label.setLocation(-5,160);
button_Panel.add(music_Label);
music_Label.setSize(50,40);
music_Label.setLocation(-3,314);
this.setLayout(null);
this.add(button_Panel);
button_Panel.setSize(50,489);
button_Panel.setLocation(-5,0);
listPanel1=new ListPanel1();
center_Panel=new ImagePanel(171,700,image2);
center_Panel.setLayout(cLayout);
center_Panel.add(listPanel1,"1");
center_Panel.add(qunlist,"2");
//center_Panel.add(new JLabel("OK"),"2");
center_Panel.add(new JLabel("音乐"),"3");
this.add(center_Panel);
center_Panel.setSize(170,489);
center_Panel.setLocation(35,0);
}
}
class EPanel extends JPanel{
JButton set_Button,exit_Button;
Image image;
Font font;
//Container container;
EPanel(){
try {
image=ImageIO.read(new File(this.getClass().getResource("images/north1.BMP").getPath()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
font=new Font("TimesRoman",Font.BOLD,13);
setLayout(null);
set_Button=new JButton("设置");
exit_Button=new JButton("退出");
add(set_Button);
set_Button.setFont(font);
set_Button.setSize(60,25);
set_Button.setLocation(14,11);
add(exit_Button);
exit_Button.setFont(font);
exit_Button.setSize(60,25);
exit_Button.setLocation(120,11);
}
public void paintComponent(Graphics g) {
ImageIcon icon =new ImageIcon(image);
Image img = icon.getImage();
g.drawImage(image, 0, 0,210,70,this);
}

}


jaguarlsl 2007-11-23
  • 打赏
  • 举报
回复
代码贴来看看
wunan320 2007-11-22
  • 打赏
  • 举报
回复
是要改按键颜色还是按键文字颜色
lchng2008 2007-11-22
  • 打赏
  • 举报
回复
这样就可以了。
lchng2008 2007-11-22
  • 打赏
  • 举报
回复
JButton btn=new JButton("Test Button");
btn.setForeground(Color.red);
war384 2007-11-22
  • 打赏
  • 举报
回复
3楼,我式了下你的方法,单独用是可以的,但在我那个程序里,我先有个panel,用的是setLyaout(null)然后再加了panel用的是cardLayout,我想问下要改变那个panel的opaque属性才能改变第二个panel上的Jbutton的颜色啊? 再说下我是想改变Jbutton的颜色不是上面文字的颜色
dracularking 2007-11-21
  • 打赏
  • 举报
回复
JFrame jf=new JFrame();
jf.setLayout(new FlowLayout());
JButton jb=new JButton("jbutton");
jb.setBackground(Color.RED);
JLabel jl=new JLabel("jlabel");
jf.add(jb);
jf.add(jl);
jf.setSize(new Dimension(30,30));
jf.setVisible(true);


the background color is used only if the component is opaque.
war384 2007-11-21
  • 打赏
  • 举报
回复
我试过了啊不行
ly2wow 2007-11-21
  • 打赏
  • 举报
回复
不是有颜色的静态量吗,直接调用不行吗

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧