1、 参考答案:
文件Person.java,该类定义完整20分
package p1;
public class Person {
String name;
public String getName() {
return name;
}
public Person()
{
this("0");
}
public Person(String name)
{
this.name=name;
}
}
文件Teacher.java,该类定义完整20分
pckage p1;
public class Teacher extends Person{
String ID;
public String getID() {
return ID;
}
public Teacher(String s1,String s2)
{
this.name=s1;
this.ID=s2;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Teacher t=new Teacher("黄牧","000001");
System.out.println(t.getName());
System.out.println(t.getID());
}
}
2、 参考答案:
package p2;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class num_2 extends JFrame implements ActionListener{
JRadioButton rbtSing,rbtDancing,rbtPlaygame;
JTextField text;
public num_2()
{
this.setSize(300,200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(1,3));
ButtonGroup g=new ButtonGroup();
rbtSing=new JRadioButton("唱歌");
rbtDancing=new JRadioButton("跳舞");
rbtPlaygame=new JRadioButton("玩游戏");
g.add(rbtSing);
g.add(rbtDancing);
g.add(rbtPlaygame);
panel.add(rbtSing);
panel.add(rbtDancing);
panel.add(rbtPlaygame);
this.getContentPane().add(panel);
text=new JTextField();
this.getContentPane().add(text,"South");
rbtSing.addActionListener(this);
rbtDancing.addActionListener(this);
rbtPlaygame.addActionListener(this);
this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new num_2();
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getSource()==rbtSing)
{
text.setText("唱歌被选中!");
}
if(arg0.getSource()==rbtDancing)
{
text.setText("跳舞被选中!");
}
if(arg0.getSource()==rbtPlaygame)
{
text.setText("玩游戏被选中!");
}
}
}
3、参考答案:
package p3;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
public class ClockJFrame extends JFrame implements Runnable{
JLabel lbl;
SimpleDateFormat sdf;
Thread thread;
public ClockJFrame()
{
super("数字时钟");
this.setBounds(0,0,300,200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel=new JPanel();
sdf=new SimpleDateFormat("hh:mm:ss");
Date date=new Date();
sdf.format(date);
lbl=new JLabel();
lbl.setText(sdf.format(new Date()));
lbl.setHorizontalAlignment(lbl.CENTER);
this.add(lbl);
this.setVisible(true);
thread=new Thread(this);
thread.start();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new ClockJFrame();
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true)
{
lbl.setText(sdf.format(new Date()));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
本文来源:https://www.2haoxitong.net/k/doc/e984255f76a20029bd642d97.html
文档为doc格式