首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java编程练习(七)

2012-12-27 
Java编程练习题(七)1. 设计一程序,创建一个用户界面,设计一块显示图片的空间及“开始”与“停止”两个按钮,当

Java编程练习题(七)

1. 设计一程序,创建一个用户界面,设计一块显示图片的空间及“开始”与“停止”两个按钮,当你按下“开始”按钮时连续显示10次图片,同时有一个进度条显示进度,显示的速度可以通过滑杆调节。

import javax.swing.*;

import javax.swing.border.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.event.*;

?

?public class MyProgressMonitor implements ActionListener,ChangeListener {

?

??? JFrame f = null;? //创建全局的类对象和变量,先赋空值,以备在各个方法中使用;

??? Timer timer = null;//计时器

??? ImageIcon[] icons = null;

?

??? JSlider slider = null;//滑动杆

??? JLabel label = null;

??? int index=0;//图片数组的索引

??? int total=0;//图片的总数

?

??? ProgressMonitor? pMonitor;

?

??? public MyProgressMonitor() {?? //写该类的构造方法:

??????? f = new JFrame("进度条例题");

??????? Container contentPane = f.getContentPane();

?

??????? icons = new ImageIcon[10];

??????? for (int i=0 ; i<10 ; i++)

??????????? icons[i] = new ImageIcon((i+1)+".gif");

??????? label = new JLabel(icons[0]);

??????? //实例化滑动杆对象:为水平放置,最小值0,最大值100,初始设置为50;

??????? slider = new JSlider(JSlider.HORIZONTAL,0, 100,50);

??????? slider.setPaintTicks(true);? //进度条刻度显示方式为True,下面的两个语句才起作用

??????? slider.setMajorTickSpacing(20);

??????? slider.setMinorTickSpacing(5);

??????? slider.setPaintLabels(true);

??????? slider.addChangeListener(this);

??????? JPanel panel = new JPanel();

??????? panel.setLayout(new GridLayout(1,2));

??????? JButton b1 = new JButton("Start");

??????? b1.addActionListener(new ButtonListener());

??????? panel.add(b1);

??????? JButton b2 = new JButton("Stop");

??????? b2.addActionListener(new ButtonListener());

??????? panel.add(b2);

??????? panel.setPreferredSize(new Dimension(200,30));

??????? timer = new Timer(slider.getValue()*10,this);

??????? contentPane.add(slider,BorderLayout.NORTH);

??????? contentPane.add(label,BorderLayout.CENTER);

? ??????contentPane.add(panel,BorderLayout.SOUTH);

??????? f.pack();

??????? f.setVisible(true);

??? }

?

??? public static void main(String[] args){

?????? new MyProgressMonitor();

??? }

?

??? public void actionPerformed(ActionEvent e){

??????? if (pMonitor.isCanceled()){

??????????? timer.stop();index = 0;total = 0;

??????? }

??????? else {

??????????? pMonitor.setProgress(total*10);

??????? }

??????? if (total < 10){

??????????? if (index == 5)

??????????? index = 0;

??????????? label.setIcon(icons[index]);

??????????? label.repaint();

??????????? index++;

??????????? total++;

??????? }

??????? else

?????????? timer.stop();

??? }

?

??? public void stateChanged(ChangeEvent e1){

???????? timer.setDelay(slider.getValue()*10);

??? }

?

??? class ButtonListener implements ActionListener{

??????? public void actionPerformed(ActionEvent e){

??????????? if (e.getActionCommand().equals("Start")){

??????????????? pMonitor = new ProgressMonitor(f,"Showing Progress Monitor","",0,100);

??????????????? pMonitor.setNote("Changing Photo....");

??????????????? pMonitor.setMillisToDecideToPopup(0);

??????????????? pMonitor.setMillisToPopup(1000);

??????????????? pMonitor.setProgress(0);

??????????????? index = 0;total = 0;

??????????????? timer.start();

??????????? }

??????????? if (e.getActionCommand().equals("Stop")){

??????????????? timer.stop();

?????????? }

??????? }

??? }

}

?

?

2编写一个商品类别类:属性有:类别好id,类别名name。

? package myshop;

? /**

?? * 商品类别信息类

?? */

? public class Category {

??? /** 商品类别ID */

??? private int id;

?

?

???? /** 商品类别名称 */

???? private String name;

?

?

???? /** 空构造方法 */

???? public Category() {

???? }

?

?

???? /**

????? * 构造方法

????? * @param id int 商品类别ID

????? * @param name String 商品类别名称

????? */

???? public Category(int id, String name) {

?????? this.id = id;

?????? this.name = name;

???? }

?

?

???? public int getId() {

?????? return id;

???? }

?

???? public String getName() {

?????? return name;

???? }

?

???? public void setId(int id) {

?????? this.id = id;

???? }

?

???? public void setName(String name) {

?????? this.name = name;

???? }

?? }

热点排行