十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
import java.awt.BorderLayout;
10年积累的成都网站建设、成都做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先做网站后付款的网站建设流程,更有鸡冠免费网站建设让你可以放心的选择与我们合作。
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login {
private JFrame frame = new JFrame("登录");
private Container c = frame.getContentPane();
private JTextField username = new JTextField();
private JPasswordField password = new JPasswordField();
private JButton ok = new JButton("确定");
private JButton cancel = new JButton("取消");
public Login(){
frame.setSize(300,200);
c.setLayout(new BorderLayout());
initFrame();
frame.setVisible(true);
}
private void initFrame() {
//顶部
JPanel titlePanel = new JPanel();
titlePanel.setLayout(new FlowLayout());
titlePanel.add(new JLabel("系统管理员登录"));
c.add(titlePanel,"North");
//中部表单
JPanel fieldPanel = new JPanel();
fieldPanel.setLayout(null);
JLabel l1 = new JLabel("用户名:");
l1.setBounds(50, 20, 50, 20);
JLabel l2 = new JLabel("密 码:");
l2.setBounds(50, 60, 50, 20);
fieldPanel.add(l1);
fieldPanel.add(l2);
username.setBounds(110,20,120,20);
password.setBounds(110,60,120,20);
fieldPanel.add(username);
fieldPanel.add(password);
c.add(fieldPanel,"Center");
//底部按钮
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(ok);
buttonPanel.add(cancel);
c.add(buttonPanel,"South");
}
public static void main(String[] args){
new Login();
}
}
上面 wuzhikun12同学写的不错,但我想还不能运行,并且还不太完善。我给个能运行的:(注意:文件名为:Test.java)
//要实现对象间的比较,就必须实现Comparable接口,它里面有个compareTo方法
//Comparable最好使用泛型,这样,无论是速度还是代码量都会减少
@SuppressWarnings("unchecked")
class Student implements ComparableStudent{
private String studentNo; //学号
private String studentName; //姓名
private double englishScore; //英语成绩
private double computerScore; //计算机成绩
private double mathScore; //数学成绩
private double totalScore; //总成绩
//空构造函数
public Student() {}
//构造函数
public Student(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
this.studentNo = studentNo;
this.studentName = studentName;
this.englishScore = englishSocre;
this.computerScore = computerScore;
this.mathScore = mathScore;
}
//计算总成绩
public double sum() {
this.totalScore = englishScore+computerScore+mathScore;
return totalScore;
}
//计算评测成绩
public double testScore() {
return sum()/3;
}
//实现compareTO方法
@Override
public int compareTo(Student student) {
double studentTotal = student.getTotalScore();
return totalScore==studentTotal?0:(totalScorestudentTotal?1:-1);
}
//重写toString方法
public String toString(){
return "学号:"+this.getStudentNo()+" 姓名:"+this.getStudentName()+" 英语成绩:"+this.getEnglishScore()+" 数学成绩:"+this.getMathScore()+" 计算机成绩:"+this.getComputerScore()+" 总成绩:"+this.getTotalScore();
}
//重写equals方法
public boolean equals(Object obj) {
if(obj == null){
return false;
}
if(!(obj instanceof Student)){
return false;
}
Student student = (Student)obj;
if(this.studentNo.equals(student.getStudentName())) { //照现实来说,比较是不是同一个学生,应该只是看他的学号是不是相同
return true;
} else {
return false;
}
}
/*以下为get和set方法,我个人认为,totalScore的set的方法没必要要,因为它是由其它成绩计算出来的
在set方法中,没设置一次值,调用一次sum方法,即重新计算总成绩
*/
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
sum();
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
sum();
}
public double getEnglishScore() {
return englishScore;
}
public void setEnglishScore(double englishScore) {
this.englishScore = englishScore;
sum();
}
public double getComputerScore() {
return computerScore;
}
public void setComputerScore(double computerScore) {
this.computerScore = computerScore;
sum();
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
sum();
}
public double getTotalScore() {
return totalScore;
}
}
//Student子类学习委员类的实现
class StudentXW extends Student {
//重写父类Student的testScore()方法
@Override
public double testScore() {
return sum()/3+3;
}
public StudentXW() {}
//StudentXW的构造函数
public StudentXW(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//Student子类班长类的实现
class StudentBZ extends Student {
//重写父类Student的testScore()方法
@Override
public double testScore() {
return sum()/3+5;
}
public StudentBZ() {}
//StudentXW的构造函数
public StudentBZ(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//测试类
public class Test {
public static void main(String[] args) {
//生成若干个student类、StudentXW类、StudentBZ类
Student student1 = new Student("s001","张三",70.5,50,88.5);
Student student2 = new Student("s002","李四",88,65,88.5);
Student student3 = new Student("s003","王五",67,77,90);
StudentXW student4 = new StudentXW("s004","李六",99,88,99.5);
StudentBZ student5 = new StudentBZ("s005","朱漆",56,65.6,43.5);
Student[] students = {student1,student2,student3,student4,student5};
for(int i = 0 ; istudents.length; i++){
double avgScore = students[i].testScore();
System.out.println(students[i].getStudentName()+"学生的评测成绩为:"+ avgScore+"分");
}
}
}
运行结果为:
张三学生的评测成绩为:69.66666666666667分
李四学生的评测成绩为:80.5分
王五学生的评测成绩为:78.0分
李六学生的评测成绩为:98.5分
朱漆学生的评测成绩为:60.03333333333333分
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.Timer;
public class PinBall
{
private final int TABLE_WIDTH = 300;//桌面宽度
private final int TABLE_HEIGHT = 400;//桌面高度
private final int RACKET_Y = 340;//球拍的垂直位置
private final int RACKET_HEIGHT = 20;//球拍高度
private final int RACKET_WIDTH = 60;//球拍宽度
private final int BALL_SIZE = 16;//球的大小
private Frame f = new Frame("弹球游戏");//实例化一个窗口
Random rand = new Random();//实例化一个随机数生成器
private int ySpeed = 10;//小球的纵向运动数度、
private double xyRate = rand.nextDouble() - 0.5;//返回一个-0.5到0.5之间的比率用控制小球运动方向
private int xSpeed = (int)(ySpeed*xyRate*2);//这个横向速度在-10到10之间,产生左右摆动运动效果
private int ballX = rand.nextInt(200)+20;//小球开始的横坐标位置,200表示产生0到100之间的随机数
private int ballY = rand.nextInt(10)+20;//小球开始的纵坐标位置
private int racketX = rand.nextInt(200);//球拍开始时的横坐标位置
private MyCanvas tableArea = new MyCanvas();//实力化一个画布工具,集成Canvas类
private String shape = "";//保存需要绘制图形的字符串属性
Timer timer;//声明一个时间变量
private boolean isLose = false;//表示游戏是否结束
public void init()
{
tableArea.setPreferredSize(new Dimension(TABLE_WIDTH,TABLE_HEIGHT));//定义画布大小
f.add(tableArea);//添加画布到窗口
KeyAdapter keyProcessor = new KeyAdapter()//实例化一个键盘监听事件适配器
{
public void keyPressed(KeyEvent ke)//重写适配器里面的按下某键盘方法
{
if(ke.getKeyCode()==KeyEvent.VK_LEFT)//按下键盘左键时
{
if(racketX 0)//球拍左边框不能出画布的左边框
racketX -=10;//按一左键次向左移动10个像素
}
if(ke.getKeyCode()==KeyEvent.VK_RIGHT)//按下键盘右键时
{
if(racketX TABLE_WIDTH - RACKET_WIDTH)//球拍右边框不能出画布的右边框
racketX +=10;//按一次右键移动向右移动10个像素
}
}
};
f.addKeyListener(keyProcessor);//给窗口添加键盘监听器
tableArea.addKeyListener(keyProcessor);//给画布添加键盘监听器
ActionListener taskPerformer = new ActionListener()//这里是实例化了一个监听接口,这个接口里面只有一个方法
{
public void actionPerformed(ActionEvent evt)//重写这个接口里面的方法,判断小球的位置
{
if(ballX=0 || ballX=TABLE_WIDTH-BALL_SIZE)//保证小球横向上在画布之内运动
{
xSpeed = -xSpeed;//触发反方向运动
}
if(ballY=RACKET_Y-BALL_SIZE(ballXracketX||ballXracketX+RACKET_WIDTH))//出了球拍的可击打范围
{
timer.stop();//停止对监听器的触发
isLose=true;//将标志isLose变量置为true
tableArea.repaint();//调用画布的重绘方法
}
else if(ballY=0||(ballY=RACKET_Y-BALL_SIZEballYracketXballX=racketX+RACKET_WIDTH))//小球在球拍之内,而其到达球拍的高度
{
ySpeed=-ySpeed;//上下方向改变,小球反弹
}
ballY+=ySpeed;//小球的坐标在纵向上增加
ballX+=xSpeed;//小球的坐标在横向上的增加
tableArea.repaint();//调用画布的重绘方法3
}
};
timer = new Timer(100,taskPerformer);//每隔0.1秒运行一次监听器
timer.start();//计时器开始运行
f.addWindowListener(new MyListener());//关闭窗口事件
f.pack();//设置窗口最佳大小
f.setVisible(true);//显示窗口
}
class MyListener extends WindowAdapter//关闭窗口的类
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
public static void main(String[] args)//程序入口
{
new PinBall().init();//调用PinBall类里面的init()方法
}
class MyCanvas extends Canvas//建一个集成Canvas类的类
{
public void paint(Graphics g)//重写父类的绘图方法
{
if(isLose)//如果isLose为真,则在画布里打印“游戏已结束”
{
g.setColor(new Color(255,0,0));//当前颜色
g.setFont(new Font("黑体",Font.BOLD,30));//字体名称,样式,大小
g.drawString("游戏已结束!",50,200);//按坐标绘制文字图形
}
else//负责
{
g.setColor(new Color(240,240,80));//当前颜色
g.fillOval(ballX,ballY,BALL_SIZE,BALL_SIZE);//填充颜色,根据坐标和长宽填充圆形
g.setColor(new Color(80,80,200));//当前颜色
g.fillRect(racketX,RACKET_Y,RACKET_WIDTH,RACKET_HEIGHT);//填充颜色,根据坐标和长宽填充矩形
}
}
}
}
class Student{
private String name;
private int age;
public Student(){
this.name = "nobody";
this.age =20;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
public boolean isSameAge(Student s){
return age == s.age;
}
}