Java初学之代码篇<二>

来源:互联网 发布:mplayerx for mac破解 编辑:程序博客网 时间:2024/06/16 19:01

4 编写程序ColorPane.java,实现下面的界面布局效果,并对每个按钮加载监听器,使得当按下一个按钮时,这个按钮的表面显现出与其上面所写的名字相同的颜色

package test;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class ColorPane extends JFrame implements ActionListener {     private JButton buttons[];    private String names[]={"blue","cyan","green","magenta","orange","pink","red","white","yellow"};    private boolean toggle=true;    private Container container;    private GridLayout grid;    public FileEditor(){        super("ColorPane");        grid=new GridLayout(3,3,5,5);        container=getContentPane();        container.setLayout(grid);        buttons=new JButton[names.length];        for(int count=0;count<names.length;count++){    //加按钮            buttons[count]=new JButton(names[count]);            buttons[count].addActionListener(this);            container.add(buttons[count]);        }        setSize(300,150);        setVisible(true);    }    public void actionPerformed(ActionEvent e){        //加监听器        if(e.getSource()==buttons[0]){            buttons[0].setBackground(Color.BLUE);            buttons[0].updateUI();        }        if(e.getSource()==buttons[1]){            buttons[1].setBackground(Color.CYAN);            buttons[1].updateUI();        }        if(e.getSource()==buttons[2]){            buttons[2].setBackground(Color.GREEN);            buttons[2].updateUI();        }        if(e.getSource()==buttons[3]){            buttons[3].setBackground(Color.MAGENTA);            buttons[3].updateUI();        }        if(e.getSource()==buttons[4]){            buttons[4].setBackground(Color.ORANGE);            buttons[4].updateUI();        }        if(e.getSource()==buttons[5]){            buttons[5].setBackground(Color.PINK);            buttons[5].updateUI();        }        if(e.getSource()==buttons[6]){            buttons[6].setBackground(Color.RED);            buttons[6].updateUI();        }        if(e.getSource()==buttons[7]){            buttons[7].setBackground(Color.WHITE);            buttons[7].updateUI();        }if(e.getSource()==buttons[8]){            buttons[8].setBackground(Color.YELLOW);            buttons[8].updateUI();        }    }    public static void main(String[] args){        ColorPane application=new ColorPane();        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }}               

5 用HashMap模拟一个网上购物车。要求:从键盘输入5本书的名称、单价、购买数量,将这些信息存入一个HashMap,然后将该HashMap作为参数调用方法getSum(HashMap books),该方法用于计算书的总价并返回。【说明:键盘输入可以使用Scanner类】

import java.util.HashMap;import java.util.Scanner;public class Books {    String name;    double price;    int number;}public class Shop {    public static double getSum(HashMap books){   //计算书的总价        double sum=0;        for(int i=0;i<books.size();i++){            Books b=(Books)books.get(i);            sum+=b.price*b.number;        }        return sum;    }    public static void main(String[] args){        HashMap m=new HashMap();        System.out.println("请输入五本书的名称、单价、购买数量:");        for(int i=0;i<5;i++){            Books b=new Books();            Scanner a=new Scanner(System.in);            b.name=a.nextLine();            b.price=a.nextDouble();            b.number=a.nextInt();            m.put(i, b);        }        double sum=getSum(m);        System.out.println("书的总价为:"+sum);    }}

6 使用Java的GUI实现文本文件的读写,并满足以下要求:
(1)界面中包含两个按钮和一个文本显示区。
(2)其中一个按钮的功能为打开文件,并在文本显示区显示文本内容;另一个按钮的功能为将文本显示区的内容写入文件。
PS:支持txt,不支持docx

import java.io.*;import java.awt.*;import java.awt.event.*;public class jtxtfm{public static void main(String args[]){    jtxtfrm fm=new jtxtfrm();   }}class jtxtfrm extends Frame implements ActionListener{    FileDialog op,sv;    Button btn1,btn2;    TextArea tarea;    jtxtfrm(){        super("文件读写");        setLayout(null);        setBackground(Color.gray);        setSize(600,300);        setVisible(true);        btn1=new Button("打开");        btn2=new Button("保存");        tarea=new TextArea("");        add(btn1);        add(btn2);        add(tarea);        tarea.setBounds(30,50,460,220);        btn1.setBounds(520,60,50,30);        btn2.setBounds(520,120,50,30);        op=new FileDialog(this,"打开",FileDialog.LOAD);        sv=new FileDialog(this,"保存",FileDialog.SAVE);        btn1.addActionListener(this);        btn2.addActionListener(this);        addWindowListener(                new WindowAdapter(){                    public void windowClosing(WindowEvent e){                        setVisible(false);                        System.exit(0);                    }                }                );    }    public void actionPerformed(ActionEvent e){  //加事件监听器        if(e.getSource()==btn1){   //打开文件            String str;            op.setVisible(true);            try{                File f1=new File(op.getDirectory(),op.getFile());                FileReader fr=new FileReader(f1);                BufferedReader br=new BufferedReader(fr);                tarea.setText("");                while((str=br.readLine())!=null)tarea.append(str+'\n');                fr.close();            }            catch(Exception e1)            {}        }        if(e.getSource()==btn2){        //保存文件            sv.setVisible(true);            try{                File f1=new File(sv.getDirectory(),sv.getFile());                FileWriter fw=new FileWriter(f1);                BufferedWriter bw=new BufferedWriter(fw);                String gt=tarea.getText();                bw.write(gt,0,gt.length());                bw.flush();                fw.close();            }            catch ( Exception e2)            {}        }    }}

7 设计一个多线程程序如下:设计一个火车售票模拟程序。假如火车站要有100张火车票要卖出,现在有5个售票点同时售票,用5个线程模拟这5个售票点的售票情况

import java.util.Random;public class SaleTicket implements Runnable {  //用Runnable接口        public int total;        public int count;        public SaleTicket() {            total = 100;            count = 0;        }        public void run() {   //重写run()方法而构造出线程实例            while (total > 0) {                synchronized (this) {                    if(total > 0) {                        try {                                Thread.sleep(new Random().nextInt(1000));                        } catch (InterruptedException e) {                            e.printStackTrace();                        }                        count++;                        total--;                        System.out.println(Thread.currentThread().getName() + "\t当前票号:" + count);                    }                }            }        }        public static void main(String[] args) {            SaleTicket st = new SaleTicket();            for(int i=1; i<=5; i++) {                new Thread(st, "售票点" + i).start();            }        }    }
原创粉丝点击