JAVA按钮设置了不显示
packagelab13;importjava.awt.*;importjava.awt.Button;importjava.awt.Color;importjava.a...
package lab13;
import java.awt.*;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class ActionEventFrame extends Frame implements ActionListener {
Button bt1,bt2,bt3;
public void ActionListener() {
Panel a=new Panel();
bt1=new Button("红色");
bt2=new Button("绿色");
bt3=new Button("蓝色");
a.add(bt1);
a.add(bt2);
a.add(bt3);
this.add(a,"North");
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String btnLabel=e.getActionCommand();
if (btnLabel.equals("红色")){
setBackground(Color.red);
}else if (btnLabel.equals("绿色")){
setBackground(Color.green);
}else if (btnLabel.equals("蓝色")){
setBackground(Color.blue);
}
}
public static void main(String[] args) {
ActionEventFrame b=new ActionEventFrame();
b.setTitle("ActionEvent事件处理");
b.setSize(300, 200);
b.setVisible(true);
b.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
}
}
//大佬帮忙看看,感觉没出错,就是运行不出来 展开
import java.awt.*;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class ActionEventFrame extends Frame implements ActionListener {
Button bt1,bt2,bt3;
public void ActionListener() {
Panel a=new Panel();
bt1=new Button("红色");
bt2=new Button("绿色");
bt3=new Button("蓝色");
a.add(bt1);
a.add(bt2);
a.add(bt3);
this.add(a,"North");
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String btnLabel=e.getActionCommand();
if (btnLabel.equals("红色")){
setBackground(Color.red);
}else if (btnLabel.equals("绿色")){
setBackground(Color.green);
}else if (btnLabel.equals("蓝色")){
setBackground(Color.blue);
}
}
public static void main(String[] args) {
ActionEventFrame b=new ActionEventFrame();
b.setTitle("ActionEvent事件处理");
b.setSize(300, 200);
b.setVisible(true);
b.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
}
}
//大佬帮忙看看,感觉没出错,就是运行不出来 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询