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);
}
});
}
}
//改那里可以实现
setBackground设置背景色!可使用JPanel
布局随便选,下面程序使用了边框(BorderLayout)和流水(FlowLayout)2种布局方式!
顺便帮你加了一个按钮事件!有问题再追问吧!~
import java.awt.BorderLayout;大神。你知道我的那个哪里有错吗?
追答加上JPanel容器,用这个改变底色,不要直接使用JFrame
明细一点嘛,新手