问答题

本程序是一个Applet,功能是监听对于文本域中文本的选择。页面中有一个文本域、一个“复制”按钮和一个文本框,选中文本域中部分文字后,单击“复制”按钮,所选文字将显示在文本框中,如图所示。


import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class exam_13 extends Applet implements ActionListener
{
TextArea ta=new TextArea(5,30);
TextField tf=new TextField(30);
Button button=new Button("复制");
String text="AWT提供基本的GUI组件, \n"+"具有可以扩展的超类, \n"+"它们的属性是继承
的。\n";
public void init()
{
setLayout(new
FlowLayout(FlowLayout.left));
ta.setText(text);
ta.setEditable(true);
add (ta);
add (button);
add (tf);
ta. addActionListener (this);
}
public void actionPerformed(ActionEvent e)
{
String s;
s=ta.qetSelectText();
if (e.getSource () ==button)
tf.setText(s);
}
}
exam_13.html:
<html>
<head><title>exam_13</title></head>
<body>
<applet code=exam_13.class width=500 height=500></applet>
</body>
</html>

【参考答案】

第1处:setLayout(new FlowLayout(FlowLayout.LEFT))
第2处:butt......

(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)
热门 试题

问答题
本程序是一个Applet,功能是监听鼠标在面板上的移动。页面上有三个不同颜色的面板,当鼠标移到其中一个面板上时,该面板颜色发生改变,当鼠标移开后,该面板颜色又恢复原色,如图所示。import java.applet.*;import java.awt.*;import java.awt.event.*;public class exam_12 extends Applet{private ChapllPanel pl=new ChapllPanel( Have A Try ,Color.yellow,newColor(150,150,150));private ChapllPanel p2=new ChapllPanel( You, The Best ,Color.orange,newColor(130,130,130));private ChapllPanel p3=new ChapllPanel( You Can Do It ,Color.yellow, newColor(150,150,150));private Label blankLabel=new Label( );private Label msg=new Label( Roll over the panel. );public void Init(){setLayout (newGridLayout (2,3));add (p1);add (p2);add (p3);add (blankLabel);add (msg); }}class ChapllPanel extends Panel implements MouseActionListener{int x, y;String message=new String();Color color;Color clr2;public ChapllPanel(String msg, Color clr,Color clr2){message=msg;this.clr2=clr2;color=clr;setBackground(clr2);addMouselistener(this);}public void mousePressed(MouseEvent e){}public void mouseClicked(MouseEvent e){}public void mouseEntered(MouseEvent e){setBackground(color);}public void mouseExited(MouseEvent e){setBackground(clr2);}public void mouseReleased(MouseEvent e){}public void paint(Graphics g){g.setFont(new Font( Arial , Font.BOLD, 16));g.drawString (message, 10,30);}}exam_12.html:<html><head><title>exam_12< title>< head><body><applet code=exam_12.class width=500 height=100>< applet>< body>< html>
问答题
本程序的功能是用按钮来控制文本框中文本的颜色。窗口中有两个带有文字标题的“Sample text”和“Text color control”面板,窗ISl的底部还有一个“Disable changes”复选框。在“Sample text”面板中有一个带有字符串的文本框,而在“Text color control”面板中有“Black”、“Red”和“Green”三个按钮,并且每个按钮上都有一个对应颜色的圆。单击任意按钮,文本框中的文本变成对应的颜色,如果选中“Disable changes”复选框,则三个颜色按钮变为不可用,如果取消选中复选框,则三个按钮变为可用,如图所示。import javax.swing.*;import java.awt.*;import java.awt.event.*;public class exam_11 extends JFrame {private JPanel upper, middle, lower;private JTextField text;private JButton black, red, green;private JCheckBox disable;public exam_11( String titleText ) {super( titleText );addWindowListener( new WindowAdapter() {public voidwindowClosing( WindowEvent e ) {System.exit(0);}});upper=new JPanel();upper.setBorder(BorderFactory.createTitledBorder( 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.createTitledBorder( Text colorcontrol ) );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( Color.red ) );red.addActionListener(new ButtonListener( Color.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( lower, 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 paintIcon( 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 getIconHeight()return DIAMETER;}public int getIconWidth()return DIAMETER;}}public static void main( String[] args ) {new exam_11( exam_11 );}}