二、基本操作題
本題提示輸入年份,然后判斷該年份是否為閏年。
importjava.io.*;
public class javal{
public static void main(String[]args){
InputStreamReader ir;
BufferedReader in;
ir=new InputStreamReader(System.in);
in=new BufferedReader(ir);
int year=1900;
System.out.print("請(qǐng)輸入年份:");
try{
String s=in.readLine();
;
} (Exception e){
}
if( )
System.OUt.println(year+"是閏年");
else
System.out.println(year+"不是閏年");
}
}
三、簡(jiǎn)單應(yīng)用題
本題使用下拉菜單來(lái)控制字體,窗口中有一個(gè)標(biāo)簽和一個(gè)下拉菜單,當(dāng)選中下拉菜單中的任一項(xiàng)字體時(shí),標(biāo)簽上字符串的字體就隨之改變。
import java.awt.*;
import java.awt.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.addhem("Serif");
style.addItem("SansSerif");
style.addhem("Monospaced");
style.addhem("Dialog");
style.addhem("Dialoglnput");
style.addActionListener(this);
JPanel p=new JPanel();
P.add(style);
getContentPane().add(p,"South");
panel=new ComboBoxTestPanel();
getContentPane().add(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)用題
本題是一個(gè)Applet,功能是監(jiān)聽(tīng)用對(duì)于文本域中文本的選擇。頁(yè)面中有一個(gè)文本域、一個(gè)“復(fù)制”按鈕和一個(gè)文本框,選中文本域中部分文字后,單擊按鈕“復(fù)制”,所選文字將顯示在文本框中。
import java.applet.Applet;
import java.awt.*;
import java.awt.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ò)展的超類(lèi),\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.getSelectText();
if(e.getSource()= =button)
tf.setText(s);
}
}
本題提示輸入年份,然后判斷該年份是否為閏年。
importjava.io.*;
public class javal{
public static void main(String[]args){
InputStreamReader ir;
BufferedReader in;
ir=new InputStreamReader(System.in);
in=new BufferedReader(ir);
int year=1900;
System.out.print("請(qǐng)輸入年份:");
try{
String s=in.readLine();
;
} (Exception e){
}
if( )
System.OUt.println(year+"是閏年");
else
System.out.println(year+"不是閏年");
}
}
三、簡(jiǎn)單應(yīng)用題
本題使用下拉菜單來(lái)控制字體,窗口中有一個(gè)標(biāo)簽和一個(gè)下拉菜單,當(dāng)選中下拉菜單中的任一項(xiàng)字體時(shí),標(biāo)簽上字符串的字體就隨之改變。
import java.awt.*;
import java.awt.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.addhem("Serif");
style.addItem("SansSerif");
style.addhem("Monospaced");
style.addhem("Dialog");
style.addhem("Dialoglnput");
style.addActionListener(this);
JPanel p=new JPanel();
P.add(style);
getContentPane().add(p,"South");
panel=new ComboBoxTestPanel();
getContentPane().add(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)用題
本題是一個(gè)Applet,功能是監(jiān)聽(tīng)用對(duì)于文本域中文本的選擇。頁(yè)面中有一個(gè)文本域、一個(gè)“復(fù)制”按鈕和一個(gè)文本框,選中文本域中部分文字后,單擊按鈕“復(fù)制”,所選文字將顯示在文本框中。
import java.applet.Applet;
import java.awt.*;
import java.awt.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ò)展的超類(lèi),\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.getSelectText();
if(e.getSource()= =button)
tf.setText(s);
}
}