java JButton 点击按钮 背景变色
importjava.awt.Color;importjava.awt.FlowLayout;importjava.awt.event.ActionEvent;impor...
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Study extends JFrame implements ActionListener{
JButton btn ;
public Study(){
btn = new JButton();
this.setTitle("111");
this.setSize(500, 500);
this.add(btn);
btn.setBounds(100, 100, 100, 100);
setLayout(new FlowLayout());
this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Study();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stu
setBackground(Color.red);
}
});
}
}
//改那里可以实现 展开
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Study extends JFrame implements ActionListener{
JButton btn ;
public Study(){
btn = new JButton();
this.setTitle("111");
this.setSize(500, 500);
this.add(btn);
btn.setBounds(100, 100, 100, 100);
setLayout(new FlowLayout());
this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Study();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stu
setBackground(Color.red);
}
});
}
}
//改那里可以实现 展开
展开全部
setBackground设置背景色!可使用JPanel
布局随便选,下面程序使用了边框(BorderLayout)和流水(FlowLayout)2种布局方式!
顺便帮你加了一个按钮事件!有问题再追问吧!~
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test extends JFrame implements ActionListener {
private JPanel panel0 = null, panel2 = null;
private JButton b1 = null, b2 = null, b3 = null, b4 = null;
public Test() {
Container c = this.getContentPane();
//边框布局
c.setLayout(new BorderLayout());
//创建panel
panel0 = new JPanel();
panel2 = new JPanel();
//为2个panel设置底色
panel0.setBackground(Color.red);
panel2.setBackground(Color.BLUE);
//2个panel都是用流水布局
panel0.setLayout(new FlowLayout());
panel2.setLayout(new FlowLayout());
//创建按钮
b1 = new JButton("panel2黄色");
b2 = new JButton("panel2绿色");
b3 = new JButton("panel0橙色");
b4 = new JButton("panel0灰色");
/**
* 添加按钮事件
*/
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
/**
* 将按钮添加相应panel上
*/
panel0.add(b1);
panel0.add(new JLabel());
panel0.add(b2);
panel2.add(b3);
panel2.add(b4);
/**
* 将panel添加到容器
*/
c.add(panel0, BorderLayout.CENTER);
c.add(panel2, BorderLayout.EAST);
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == b1) {
panel2.setBackground(Color.yellow);
} else if (e.getSource() == b2) {
panel2.setBackground(Color.green);
} else if (e.getSource() == b3) {
panel0.setBackground(Color.ORANGE);
} else if (e.getSource() == b4) {
panel0.setBackground(Color.GRAY);
}
}
}
追问
大神。你知道我的那个哪里有错吗?
追答
加上JPanel容器,用这个改变底色,不要直接使用JFrame
展开全部
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Study extends JFrame{
JButton btn ;
public Study(){
this.setTitle("111");
this.setSize(500, 500);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
final Container jp = this.getContentPane();
btn = new JButton("abc");
jp.add(btn);
btn.setBounds(100, 100, 100, 100);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(btn)) {
jp.setBackground(Color.gray);
}
}
});
}
public static void main(String[] args) {
new Study();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你在按钮里光调用了修改颜色的代码 没有调用刷新当然没用了
追问
明细一点嘛,新手
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询