50 喜欢·83 浏览
解答题

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

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class ButtonPanel extends JPanel implements ActionListener

public ButtonPanel()

yellowButton=new JButton("Yellow");

blueButton=new JButton("Blue");

redButton=new JButton("Red");

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

add(yellowButton);

add(blueButton);

add(redButton);

add(j1);

yellowButton.addActionListener(this);

blueButton.addActionListener(this);

redButton.addActionListener(this);

public void actionPerformed(ActionEvent evt)

Object source=evt.getSource();

Color color=getForcground();

if(source==yellowButton)color=Color.yellow;

else if(

主观题和计算题请自行在草稿纸上作答

题目答案

第1处:j1.setForeground(color) 第2处:j1.repaint()

答案解析

在构件类的方法中,setForeground()为设置构件的前景色,repaint()为重新绘制构件。
举一反三

Fatal error: Uncaught Error: Call to a member function query() on null in F:\duqu8.com\SaxueFrame\common\funarticle.php:16 Stack trace: #0 F:\duqu8.com\SaxueFrame\common\funarticle.php(51): formatContent('<p>\xE5\x9C\xA8\xE8\x80\x83\xE7\x94\x9F\xE6\x96\x87...', Object(blockquestionlist)) #1 F:\duqu8.com\SaxueFrame\blocks\block_questionlist.php(107): formatQuestion(Array, Object(blockquestionlist)) #2 F:\duqu8.com\SaxueFrame\lib\template\template.php(640): blockquestionlist->setcontent(true) #3 F:\duqu8.com\SaxueFrame\data\compiled\templates\wap\question.detail.html.php(354): saxue_get_eloop(Array) #4 F:\duqu8.com\SaxueFrame\lib\template\template.php(329): include('F:\\duqu8.com\\Sa...') #5 F:\duqu8.com\SaxueWap\detail.php(81): saxuetpl->fetch('F:/duqu8.com/Sa...') #6 {main} thrown in F:\duqu8.com\SaxueFrame\common\funarticle.php on line 16