java编写一个应用程序,其功能为:显示一个图形框架界面,在其图形框架上摆放三个按钮。三个按钮的标

如题所述

参考代码和注释如下

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

//本类继承自JFrame,实现了ActionListener接口
public class ButtonFrame extends JFrame implements ActionListener {

JButton jb1,jb2,jb3;
//构造方法: 组件的初始化, 排版, 窗口的设置
public ButtonFrame() {
jb1=new JButton("test1");
jb1.addActionListener(this);
jb2=new JButton("test2");
jb2.addActionListener(this);
jb3=new JButton("test3");
jb3.addActionListener(this);
add(jb1);
add(jb2);
add(jb3);
setLayout(new FlowLayout());
setTitle("窗口");// 窗口标题
setSize(280,220);// 窗口大小
setLocationRelativeTo(null);// 窗口居中
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 当窗口关闭时,程序结束
}

//处理按钮点击事件
public void actionPerformed(ActionEvent e) {
System.out.println("Button "+e.getActionCommand());

//比较啰嗦的写法
//JButton jbTemp = (JButton) e.getSource(); 
//if(jb1==jbTemp) {
//System.out.println("Button test1");
//}else if(jb2==jbTemp) {
//System.out.println("Button test2");
//}else if(jb3==jbTemp) {
//System.out.println("Button test3");
//}
}

public static void main(String[] args) {
new ButtonFrame().setVisible(true);//实例化窗口并可见
}
}

效果图

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-03-14
写好发你私信了 注意查收

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网