简答题 Java
题号 #2244496

本题用复选框来控制字体的显示,窗口中有一个标签和两个复选按钮,这两个复选按钮分别对应的字体的两个特点:加粗和倾斜,任意选中一个按钮或两个都选中,标签上的字符串就显示对应的字体。
tmport.java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class CheckBoxFrame extends JFrame implements Ac-
tionListener{
public CheckBoxFrame(){
setTitle("CheckBoxTest");
setSize(300,200);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)(
System.exit(O);
}
});
JPanel P=new JPanel();
bold=addCheckBox(p,"Bold");
italic=addCheckBox(P,"Italic");
getContentPane().add(p,"South");
panel=new CheekBoxTestPanel();
getContentPane().add(panel,"Center");
}
public JCheckBox addCheekBox(JPanel p,String
name){
JCheckBox c=new JCheekBox(name):
c.addActionListener(       );
P.add(c);
return C;
}
public void       {
int m=(bold.isSelected()?Font.BOLD:O)+
(italic.isSelected()?Font.ITALIC:O);
panel.setFont(m);
}
private CheckBoxTestPanel panel;
private JCheckBox bold;
private JCheckBox italic;
}
class CheckBoxTestPanel extends JPanel{
public CheckBoxTestPanel(){
setFont(Font.PLAIN);
}
public void setFont(int m){
setFont(new Font("SansSerif",m,12));
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
9.drawstring("The quick brown fox jumps over
the lazy dog.",0,50);
}
}
public class java2{
public static void main(String[]args){
JFrame frame=new CheckBoxFrame();
frame.show();
}
}

进入在线模考

参考答案

暂无

答案解析

第1处:this
第2处:actionPerformed(ActionEvent evt)
【解析】第l处注册监听器进行授权,该方法的参数是事件处理的对象;第2处是actionPerformed方法通过读取ActionEvent对象的相关信息来得到事件发生时的情况。

你可能感兴趣的试题

1 本题的功能是用按钮来控制文本框中文本的颜色。窗口中有两个带有文字标题的面板“Sample text”和“Text col—or control”,窗口的底部还有一个复选按钮“Disable chan-ges”。在“Sample text”面板中有一个带有宇符串的文本框,而在“Text color control”面板中有三个按钮“Black”、“Red”和“Green”,并且每个按钮上都有一个对应颜色的圆。单击任意按钮,文本框的文本变成对应的颜色,如果选中“Disa-ble changes”复选框,则三个颜色按钮变为不可用,如果取消选中复选框,则三个按钮变为可用。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class java3 extends JFrame{
private JPanel upper,middle,lower;
private JTextField text;
private JButton black,red,green;
private JCheckBox disable;
public java3(String titleText){
super(titleText);
addWindowListener(new WindowAdapter(){
public void
windowClosing(WindowEvent e){
System.exit(0);
}
});
upper=new JPanel();
upper.setBorder(BorderFactory.ereateTitledBor-
der("Sample text")):
Upper.setlayout(new BorderLayout());
text=new JTextField("Change the color of this
text");
upper.add(text,BorderLayout.CENTER);
middle=new JPanel();
middle.setBorder(BorderFactory.createTitledBor-
der("Text color control"));
middle.setLayout(new FlowLayout(FlowLayout.
CENTER)):
black=new JButton("Black",new ColorIcon(
Color.black));
black.addActionListener(new ButtonListener(
Color.black));
middle.add(black);
red=new JButton("Red",new ColorIeon(Col-
or.red));
red.addActionListener(new ButtonListener(Col-
or.red));
middle.add(red);
green=new JButton("Green",new ColorIcon(
Color.green));
green.addActionListener(new ButtonListener(
Color.green));
middle.add(green);
lower=new JPanel();
lower.setLayout(new FlowLayout(FlowLayout.
RIGHT));
disable=new JCheckBox("Disable changes");
disable.addhemListener(new hemListener(){
public void itemStateChanged(hemEvent e){
boolean enabled
= (e.getStateChange()
= =ItemEvent.DESELECTED);
black.setEnabled(enabled);
red.setEnabled(enabled);
green.setEnabled(enabled);
}
}
);
lower.add(disable):
Container cp=getContentPane();
cp.add(upper,BorderLayout.NORTH);
ep.add(middle,BorderLayout.CENTER);
ep.add(10wer,BorderLayout.SOUTH);
pack();
setVisible(true);
}
class ButtonListener extends ActionListener{
private Color c;
public ButtonListener(Color c)f
this.c=c;
}
public void aetionPerformed(ActionEvent e){
text.setForeground(c);
}
}
class ColorIcon implements Icon{
private Color c;
private static final int DIAMETER=10;
public Colorlcon(Color c){
c=c;
}
public void paintleon(Component cp,Graphics g,
int X,int Y){
g.setColor(e);
g.fillOval(x,Y,DIAMETER,DIAMETER);
g.setColor(Color.black);
g.drawOval(x,y,DIAMETER,DIAMETER);
} 
public int getIconHeight(){
return DIAMETER:
}
public int getlconWidth(){ 
return DIAMETER;
}
}
public static void main(String[]args){
new java3("java3");
}
}
3 本题的功能是用按钮来控制文字的颜色。窗口中有三个按钮“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(); 
}
}
找试题 · 找答案 · 找资料 在线模考