java怎么用一个按钮实现两个不同颜色的切换?
importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;classMyFrameextendsJFrame...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class MyFrame extends JFrame implements ActionListener{
JButton b1=new JButton("换色");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
public MyFrame(){
super();
this.setLayout(new GridLayout(2,1));
b1.addActionListener(this);
p1.setBackground(Color.BLUE);
p2.add(b1);
add(p1);
add(p2);
setSize(200,200);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
p1.setBackground(Color.RED);
}
}
}
public class Test2{
public static void main(String args[]){
new MyFrame().show();
}
}
求高手在源代码上改改!
谢谢了 展开
import javax.swing.*;
import java.awt.event.*;
class MyFrame extends JFrame implements ActionListener{
JButton b1=new JButton("换色");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
public MyFrame(){
super();
this.setLayout(new GridLayout(2,1));
b1.addActionListener(this);
p1.setBackground(Color.BLUE);
p2.add(b1);
add(p1);
add(p2);
setSize(200,200);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
p1.setBackground(Color.RED);
}
}
}
public class Test2{
public static void main(String args[]){
new MyFrame().show();
}
}
求高手在源代码上改改!
谢谢了 展开
1个回答
展开全部
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class MyFrame extends JFrame implements ActionListener{
JButton b1=new JButton("换色");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
public MyFrame(){
super();
this.setLayout(new GridLayout(2,1));
b1.addActionListener(this);
p1.setBackground(Color.BLUE);
p2.add(b1);
add(p1);
add(p2);
setSize(200,200);
}
public void actionPerformed(ActionEvent e){
if(p1.getBackground()==Color.RED){
p1.setBackground(Color.BLUE);
}else{
p1.setBackground(Color.RED);
}
}
}
public class Test2{
public static void main(String args[]){
new MyFrame().show();
}
}
import javax.swing.*;
import java.awt.event.*;
class MyFrame extends JFrame implements ActionListener{
JButton b1=new JButton("换色");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
public MyFrame(){
super();
this.setLayout(new GridLayout(2,1));
b1.addActionListener(this);
p1.setBackground(Color.BLUE);
p2.add(b1);
add(p1);
add(p2);
setSize(200,200);
}
public void actionPerformed(ActionEvent e){
if(p1.getBackground()==Color.RED){
p1.setBackground(Color.BLUE);
}else{
p1.setBackground(Color.RED);
}
}
}
public class Test2{
public static void main(String args[]){
new MyFrame().show();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询