JButton 请问一下高人们,当单击JButton时本来是一个红色怎样变了黑色,就是实现颜色的交换。给出代码谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package swing_test;import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class ChangeColor implements ActionListener{ private JFrame jFrame; private JButton jb; public void load(){ jFrame = new JFrame("改变按钮的颜色"); jb = new JButton(); //设置按钮的背景为红色 jb.setBackground(Color.red); //为按钮添加事件监听器 jb.addActionListener(this); jFrame.add(jb); jFrame.setBounds(300, 400, 56, 80); jFrame.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { //将按钮的背景设置为黑色 jb.setBackground(Color.black); } public static void main(String[] args) { new ChangeColor().load(); }}大概就是这样。不过布局不好,按钮充满了整个JFrame,但你的基本要求达到了 package com.javamvp.swingdemo;import javax.swing.*;import java.awt.event.*;import java.awt.*;class CreatJF { public CreatJF() { JFrame jf = new JFrame("DEMO 02"); final Container c = jf.getContentPane(); c.setLayout(new FlowLayout()); JButton jb = new JButton("OK"); c.add(jb); jf.setSize(400, 300); // jf.pack(); jf.setVisible(true); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { c.setBackground(Color.black); } }); }}public class SwingDemo02 { public static void main(String args[]) { CreatJF jf = new CreatJF(); }} 呵呵谢谢你们了。单击JBouttn也可以用方法:setIcon(Icon I);和setPressedIcon(Icon I);来实现JBouttn交换图片。 先顶一下搂主,这不是什么恶意行为哦 群号:62464919,本群是IT技术群,包括(C#,Java,ruby,JavaScript,asp,jsp,python,asp.net,sql,mysql……众多语言)此群需要您来共同进步,学习中放松心情。管理员在次致敬,呵呵,搞的不好了。 正好做到个东西就加了个,package stums;import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;public class classInfoAdd extends JFrame implements ActionListener{ private JButton button; private JPanel panel; public classInfoAdd() { setTitle("增加班级信息"); setBounds(100, 100, 500, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); button = new JButton("按 钮"); button.setBackground(Color.RED); button.addActionListener(this); button.setBounds(10, 10, 80, 30); panel = new JPanel(); panel.setLayout(null); panel.add(button); getContentPane().add(panel); setVisible(true); } public static void main(String[] args) { new classInfoAdd() ; } public void actionPerformed(ActionEvent e) { Object o = e.getSource(); // TODO Auto-generated method stub if(o == button) { button.setBackground(Color.BLACK); } }} JAVA J2EE开发虚拟机装什么系统好 ActionEvent.getSource()的困惑 关于JpopupMenu的问题 新手求教怎样用JAVA实现将一个整形的10进制数转化为8进制 请问要如何在JDialog上绘制图形? 如何从一个窗体调用另一个窗体?(Java) 一个捆扰了很久的问题 请求有关文件的操作,如何创建目录,文件什么的?在先等呀。。。。。。。 有关多线程的问题 如何对菜单项创快捷键 F5????????????????????????????????? this引用有什么意义? Swing组件的坐标位置如何确定
package swing_test;import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.*;public class ChangeColor implements ActionListener
{
private JFrame jFrame;
private JButton jb;
public void load(){
jFrame = new JFrame("改变按钮的颜色");
jb = new JButton();
//设置按钮的背景为红色
jb.setBackground(Color.red);
//为按钮添加事件监听器
jb.addActionListener(this);
jFrame.add(jb);
jFrame.setBounds(300, 400, 56, 80);
jFrame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
//将按钮的背景设置为黑色
jb.setBackground(Color.black);
} public static void main(String[] args)
{
new ChangeColor().load();
}
}
大概就是这样。
不过布局不好,按钮充满了整个JFrame,但你的基本要求达到了
package com.javamvp.swingdemo;import javax.swing.*;
import java.awt.event.*;
import java.awt.*;class CreatJF {
public CreatJF() {
JFrame jf = new JFrame("DEMO 02");
final Container c = jf.getContentPane();
c.setLayout(new FlowLayout());
JButton jb = new JButton("OK");
c.add(jb);
jf.setSize(400, 300);
// jf.pack();
jf.setVisible(true);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c.setBackground(Color.black);
}
});
}}public class SwingDemo02 { public static void main(String args[]) {
CreatJF jf = new CreatJF();
}
}
单击JBouttn也可以用方法:setIcon(Icon I);和setPressedIcon(Icon I);来实现JBouttn交换图片。
群号:62464919,本群是IT技术群,包括(C#,Java,ruby,JavaScript,asp,jsp,python,asp.net,sql,mysql……众多语言)此群需要您来共同进步,学习中放松心情。管理员在次致敬,呵呵,搞的不好了。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;public class classInfoAdd extends JFrame implements ActionListener
{
private JButton button;
private JPanel panel;
public classInfoAdd()
{
setTitle("增加班级信息");
setBounds(100, 100, 500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button = new JButton("按 钮");
button.setBackground(Color.RED);
button.addActionListener(this);
button.setBounds(10, 10, 80, 30);
panel = new JPanel();
panel.setLayout(null);
panel.add(button);
getContentPane().add(panel);
setVisible(true);
}
public static void main(String[] args)
{
new classInfoAdd()
;
} public void actionPerformed(ActionEvent e)
{
Object o = e.getSource();
// TODO Auto-generated method stub
if(o == button)
{
button.setBackground(Color.BLACK);
}
}}