java二级考试简单应用题,2015年计算机二级考试JAVA上机模拟试卷(2)

简单应用题

42本题的功能是用按钮来控制文字的颜色。窗口中有三个按钮“Yellow”、“Blue”和“Red”,它们分别对应文字标签中文本的颜色为黄色、蓝色和红色,单击任意一个按钮,文字标签中的文本就变成按钮对应的颜色。

import java.awt.*;

import java.awt.event.*;

import javflx.swing.*;

class ButtonPanel extends JPanel implements ActionL-

istener{

public ButtonPanel(){

yellowButton=new J Button("Yellow");

blueButton=new JButton("Blue");

redButton=new JButton("Red");

j1=new JLabel("I am from China!");

add(yellowButton);

add(blueButton);

add(redButton);

add(j1);

yellowButtofl.addActionListener(this);

blueButton.addActionListener(this);

redButton.addActionListener(this);

}

public void actionPerformed(ActionEvent evt){

0bject source=evt.getSource();

Color color=getForeground();

if(source= =yellowButton)color=Color.

yellow;

else if(source= =blueButton)color=Color.

blue;

else if(source= =redButton)color=

Color.red;

;

;

}

private JButton yellowButton;

private JButton blueButton;

private JButton redButton;

private JLabel jl;

}

class ButtonFrame extends JFrame{

public ButtonFrame(){

setTitle("exam l6");

setSize(300,200);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(O);

}

});

Container contentPane=getContentPane();

contentPane.add(new ButtonPanel());

}

}

public class java2{

public static void main(String[]args){

JFrame frame=new ButtonFrame();

frame.show();

}

}

参考解析:

第1处:jl.setForeground(color)

第2处:jl.repaint()

【解析】在构件类的方法中,setForeground()为设置构件的前景色,repaint()为重新绘制构件。

相关推荐:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值