三、簡單應(yīng)用題(共24分)
本題使用下拉菜單來控制字體,窗口中有一個標(biāo)簽和一個下拉菜單,當(dāng)選中下拉菜單中的任一項(xiàng)字體時,標(biāo)簽上字符串的字體就隨之改變。
import java.a(chǎn)wt.*;
import java.a(chǎn)wt.event.*;
import javax.swing.*;
class ComboBoxFrame extends JFrame {
public ComboBoxFrame(){
setTitle("java2");
setSize(300,200);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
style=new JComboBox():
style.setEditable(true);
style.a(chǎn)ddhem("Serif");
style.a(chǎn)ddItem("SansSerif");
style.a(chǎn)ddhem("Monospaced");
style.a(chǎn)ddhem("Dialog");
style.a(chǎn)ddhem("Dialoglnput");
style.a(chǎn)ddActionListener(this);
JPanel p=new JPanel();
P.a(chǎn)dd(style);
getContentPane().a(chǎn)dd(p,"South");
panel=new ComboBoxTestPanel();
getContentPane().a(chǎn)dd(panel,"Center");
}
public void actionPerformed(ActionEvent evt){
JComboBox source=(JComboBox) ;
String item=(String)source.getSelectedhem():
panel.setStyle(item);
}
private ComboBoxTestPanel panel;
private JComboBox style;
}
class ComboBoxTestPanel extends JPanel{
public ComboBoxTestPanel(){
setStyle("Serif");
}
public void setStyle(String s){
setFont(new Font(S,F(xiàn)ont.PLAIN,12));
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
9.drawString("Welcome to China!",0,50);
}
}
public class java2{
public static void main(String[]args){
JFrame frame=new ComboBoxFrame();
frame.show();
}
}
四、綜合應(yīng)用題(共18分) 本題是一個Applet,功能是監(jiān)聽用對于文本域中文本的選擇。頁面中有一個文本域、一個“復(fù)制”按鈕和一個文本框,選中文本域中部分文字后,單擊按鈕“復(fù)制”,所選文字將顯示在文本框中。 import java.a(chǎn)pplet.Applet; import java.a(chǎn)wt.*; import java.a(chǎn)wt.event.*; public class java3 extends Applet implements ActionL- istener { TextArea ta=new TextArea(5,30); TextField tf=new TextField(30); Button button=new Button("復(fù)制"); String text="AWT提供基本的GUl組件,\n"+" 具有可以擴(kuò)展的超類,\n"+"它們的屬性是繼承的。\ n": public void init() { setLayout(new FlowLayout(FlowLayout.left)); ta.setText(text); ta.setEditable(true); add(ta); add(button); add(tf); ta.a(chǎn)ddActionListener(this); } public void actionPerformed(ActionEvent e) { String S; s=ta.getSelectText(); if(e.getSource()= =button) tf.setText(s); } }
三、簡單應(yīng)用題 第1處:implements ActionListener 第2處:evt.getSource() 【解析】第1處是實(shí)現(xiàn)ActionListener接口,程序中有窗口監(jiān)聽器的注冊;第2處返回ActionEvent動作事件的最初發(fā)生對象。 四、綜合應(yīng)用題 第1處:setLayout(new FlowLayout(FlowLayout.LEFT)) 第2處:button.a(chǎn)ddActionListener(this) 一第3處:s=ta.getSelectedText() 【解析】第1處是設(shè)置構(gòu)件的對齊方式為左對齊的且縱橫間隔都是5個像素的布局管理器;第2處是為按鈕注冊監(jiān)聽器;第3處是在文本域ta中得到選中文本,將其賦給String類型的s。
四、綜合應(yīng)用題(共18分) 本題是一個Applet,功能是監(jiān)聽用對于文本域中文本的選擇。頁面中有一個文本域、一個“復(fù)制”按鈕和一個文本框,選中文本域中部分文字后,單擊按鈕“復(fù)制”,所選文字將顯示在文本框中。 import java.a(chǎn)pplet.Applet; import java.a(chǎn)wt.*; import java.a(chǎn)wt.event.*; public class java3 extends Applet implements ActionL- istener { TextArea ta=new TextArea(5,30); TextField tf=new TextField(30); Button button=new Button("復(fù)制"); String text="AWT提供基本的GUl組件,\n"+" 具有可以擴(kuò)展的超類,\n"+"它們的屬性是繼承的。\ n": public void init() { setLayout(new FlowLayout(FlowLayout.left)); ta.setText(text); ta.setEditable(true); add(ta); add(button); add(tf); ta.a(chǎn)ddActionListener(this); } public void actionPerformed(ActionEvent e) { String S; s=ta.getSelectText(); if(e.getSource()= =button) tf.setText(s); } }
三、簡單應(yīng)用題 第1處:implements ActionListener 第2處:evt.getSource() 【解析】第1處是實(shí)現(xiàn)ActionListener接口,程序中有窗口監(jiān)聽器的注冊;第2處返回ActionEvent動作事件的最初發(fā)生對象。 四、綜合應(yīng)用題 第1處:setLayout(new FlowLayout(FlowLayout.LEFT)) 第2處:button.a(chǎn)ddActionListener(this) 一第3處:s=ta.getSelectedText() 【解析】第1處是設(shè)置構(gòu)件的對齊方式為左對齊的且縱橫間隔都是5個像素的布局管理器;第2處是為按鈕注冊監(jiān)聽器;第3處是在文本域ta中得到選中文本,將其賦給String類型的s。