[简答题]
本题的功能是用按钮来控制文本框中文本的颜色。窗口中有两个带有文字标题的面板“Sample text”和“Text color control”,窗口的底部还有一个复选按钮“Disable changes”。在“Sample text”面板中有一个带有字符串的文本框,而在“Text color control”面板中有三个按钮:“Black”、“Red”和“Green”,并且每个按钮上都有一个对应颜色的圆。单击任意按钮,文本框中的文本变成对应的颜色,如果选中“Disable 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 ColorIcon(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.addItemListener(new ItemListener()(
public void itemStateChanged(ItemEvent 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);
cp.add(middle,BorderLayout.CENTER);
cp.add(10wer,BorderLayout.SoUTH);
pack();
setVisible(true);
}
class ButtonListener extends ActionListener{
private Color c;
public ButtonListener(Color c){
this.c=c;
}
public void actionPerformed(ActionEvent e){
text.setForeground(c);
}
}
class ColorIcon implements Icon{
private Color c;
private static final int DIAMETER=10;
public ColorIcon(Color c){
c=c;
}
public void paintlcon(Component cp,Graphics g,
int x,int y){
g.setColor(c);
g.fillOval(X,y,DIAMETER,DIAMETER);
g.setColor(Color.black);
g.drawOval(x,y,DIAMETER,DIAMETER);
}
public int getlconHeight(){
return DIAMETER;
}
public int getlconWidth(){
return DIAMETER;
}
}
public static void main(String[]args){
new java3("advance");
}
}
请在此输入您的答案
10[简答题]
本题的功能是用按钮来控制文本框中文本的颜色。窗口中有两个带有文字标题的面板“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");
}
}
请在此输入您的答案
11[简答题]
下面程序中,在主窗口单击鼠标后,就会生成一个新
窗口。
import java.awt.*;
import java.awt.event.*;
public class java3 extends Frame{
java3(){
super("java3");
addNotify();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
Insets insets=getInsets();
setSize(insets.left+insets.right+150,
insets.top+insets.bottom+150);
this.addMouseListener(MouseEventHandler());
}
class MouseEventHandler implements MouseAda-
pter{
public void mousePresse(MouseEvent evt){
Rectangle bounds=getBounds();
int x=evt.getX()+bounds.x;
int y=evt.getY()十bounds.y;
java3 m=newjava3();
m.setLocation(x,y);
m.show();
}
}
static public void main(String[]args){
(new java3()).show();
}
}
请在此输入您的答案
12[简答题]
本题的功能是用文本框来设定表盘中指针的位置。窗口中有一个画板和两个文本框,画板中绘制了一个表盘和时针、分针,通过文本框分别设定“时”和“分”,表盘中的时针和分针就会指到对应的位置上。
import java.awt.*;
import java.awt.event*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.event.*;
public class java3
{
public static void main(String[]args)
{
TextTestFrame frame=new TextTestFrame():
frame.setDefauhCloseOperation(JFrame.EXIT_
0N_CLOSE);
frame.show();
}
}
class TextTestFrame extends JFrame
{
public TextTestFrame()
{
setTitle("java3"):
setSize(DEFAULT_WIDTH,DEFAULT_
HElGHT);
Container contentPane=getContentPane();
DocumentListener listener=new DoeumentListen-
er();
JPanel panel=new JPanel();
hourField=new JTextField("12",3);
panel.add(hourField);
hourField.getDocument().addDocumentListener
(this);
minuteField=new JTextField("00",3):
panel.add(minuteField);
minuteField.getDocument().addDocumentListener
(listener);
contentPane.add(panel,BorderLayout.S()UTH);
clock=new ClockPanel();
contentPane.add(clock,BorderLayout.CEN-
TER);
}
public void setClock()
{
try
{
int hours
=Integer.parseInt(hourField.getText().trim
()):
int minutes
=Integer.parseInt(minuteField.getText().trim
());
clock.setTime(hours,minutes);
}
catch(NumberFormatExcepfion e){}
}
public static final int DEFAULT_WIDTH=300;
public static final int DEFAULT_HEIGHT
=300;
private J TextField hourField;
private JTextField minuteField;
private ClockPanel clock;
private class clockFieldListener extends Docu-
mentListener
{
public void insertUpdate(DocumentEvent e){ set-
Clock();}
public void removeUpdate(DocumentEvent e){
setClock();}
public void changedUpdate(DocumentEvent e){}
}
}
class ClockPanel extends JPanel
{
pubhc void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphies2D g2=(Graphics2D)g;
Ellipse2D circle
=new Ellipse2D.Double(0,0,2* RADIUS,2
*RADIUS);
g2.draw(circle);
double hourAngle
=Math.toRadians(90-360*minutes/(12
*60));
drawHand(92,hourAngle,HOUR_HAND_
LENGTH);
double minuteAngle
=Math.toRadians(90-360*minutes/60);
drawHand(g2,minuteAngle,MINUTE_HAND_
LENGTH):
}
punic void drawHand(Graphics2D g2,
double angle,double handLength)
{
Point2D end=new Point2D.Double(
RADIUS+handLength*Math.cos(angle),
RADIUS-handLength*Math.sin(angle));
Point2D center=new Point2D.Double(RADIUS,
RADIUS):
g2.draw(new Line2D.Double(center,end));
}
public void setTime(int h,int m)
{
minutes=h*60+m;
repaint();
}
private double minutes=0;
private double RADIUS=100;
private double MINUTE_HAND_LENGTH=
0.8*RADIUS;
private double HOUR_HAND_LENGTH=0.6
*RADIUS:
}
正在阅读:
2017年全国计算机等级考试《二级JAVA》冲刺试题:综合应用07-16
高中生新春日记500字:桂林春节习俗04-15
那山那人作文800字07-22
我心中的太阳作文500字12-04
2022年江苏南通高级经济师准考证打印时间:6月10日-6月18日06-01
2019年广西房地产估价师报名时间及报名入口【8月9日-19日】04-12
初中生周记400字:元旦假期03-28
日本留学大概生活费参考01-27
小学一年级父亲节日记200字左右06-07