第 1 頁:選擇題 |
第 3 頁:基本操作題 |
第 4 頁:簡(jiǎn)單應(yīng)用題 |
第 5 頁:綜合應(yīng)用題 |
簡(jiǎn)單應(yīng)用題
42本題的功能是用按鈕來控制文字的顏色。窗口中有三個(gè)按鈕“Yellow”、“Blue”和“Red”,它們分別對(duì)應(yīng)文字標(biāo)簽中文本的顏色為黃色、藍(lán)色和紅色,單擊任意一個(gè)按鈕,文字標(biāo)簽中的文本就變成按鈕對(duì)應(yīng)的顏色。
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()
【解析】在構(gòu)件類的方法中,setForeground()為設(shè)置構(gòu)件的前景色,repaint()為重新繪制構(gòu)件。
相關(guān)推薦:
2015年計(jì)算機(jī)二級(jí)《JAVA》章節(jié)習(xí)題及答案匯總
2015年計(jì)算機(jī)二級(jí)公共基礎(chǔ)知識(shí)考點(diǎn)測(cè)試題匯總
北京 | 天津 | 上海 | 江蘇 | 山東 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
廣東 | 河北 | 湖南 | 廣西 | 河南 |
海南 | 湖北 | 四川 | 重慶 | 云南 |
貴州 | 西藏 | 新疆 | 陜西 | 山西 |
寧夏 | 甘肅 | 青海 | 遼寧 | 吉林 |
黑龍江 | 內(nèi)蒙古 |