麻烦高手帮我改一下这个java程序
importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener...
import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;public class ccc extends JFrame implements ActionListener { JPanel n1; JButton n2; JButton n3; JButton n4; JButton n5; JButton n6; JButton n7; JButton n8; JButton n9; public ccc(){ super("省份"); n1=new JPanel(); n2=new JButton("广东"); n3=new JButton("江苏"); n4=new JButton("浙江"); n5=new JButton("山东"); n6=new JButton("福建"); n7=new JButton("山西"); n8=new JButton("湖北"); n9=new JButton("湖南"); n1.setLayout(new GridLayout(2,4)); n1.add(n2); n1.add(n3); n1.add(n4); n1.add(n5); n1.add(n6); n1.add(n7); n1.add(n8); n1.add(n9); n2.addActionListener(this); this.add(n1); this.setSize(800,600); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public static void main(String[] args) { new ccc(); } public void actionPerformed(ActionEvent e){ System.out.println("广州"); } 当点击到“广东”时,打印出“广州”当点击到江苏时,打印出“南京” 当点击到山东时,打印出济南
展开
2个回答
2014-01-09
展开全部
//兄弟,多查一下Java API就知道怎么做了
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ButtonEventDemo extends JFrame implements ActionListener {
JPanel n1;
JButton n2;
JButton n3;
JButton n4;
JButton n5;
JButton n6;
JButton n7;
JButton n8;
JButton n9;
public ButtonEventDemo() {
super("省份");
n1 = new JPanel();
n2 = new JButton("广东");
n3 = new JButton("江苏");
n4 = new JButton("浙江");
n5 = new JButton("山东");
n6 = new JButton("福建");
n7 = new JButton("山西");
n8 = new JButton("湖北");
n9 = new JButton("湖南");
// 添加动作监听器 开始
n2.addActionListener(this);
n3.addActionListener(this);
n4.addActionListener(this);
n5.addActionListener(this);
n6.addActionListener(this);
n7.addActionListener(this);
n8.addActionListener(this);
n9.addActionListener(this);
// 添加动作监听器 结束
n1.setLayout(new GridLayout(2, 4));
n1.add(n2);
n1.add(n3);
n1.add(n4);
n1.add(n5);
n1.add(n6);
n1.add(n7);
n1.add(n8);
n1.add(n9);
this.add(n1);
this.setSize(800, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new ButtonEventDemo();
}
public void actionPerformed(ActionEvent e) {
// 当点击到“广东”时,打印出“广州”当点击到江苏时,打印出“南京” 当点击到山东时,打印出济南
if(e.getSource()==n2) {
System.out.println(e.getActionCommand() + " ---> 广州");
} else if(e.getSource()==n3) {
System.out.println(e.getActionCommand() + " ---> 南京");
} else if(e.getSource()==n5) {
System.out.println(e.getActionCommand() + " ---> 济南");
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询