⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addsoft.java

📁 JAVA产品存储系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
/*
 * @author Amadan Gong
 * @2008.11.17
 * @增加各类商品
 */
//第二层窗口1 add production
class addpro implements ActionListener
{

	static JFrame f1=null;
	public addpro(JFrame f){
		f1=new JFrame("add production");
		Container contentPane=f1.getContentPane();
		JPanel buttonPanel=new JPanel();
		buttonPanel.setBorder(BorderFactory.createLineBorder(Color.blue,2));
		buttonPanel.setLayout(new GridLayout(4,3));
		JButton b1=new JButton("add software");
		b1.addActionListener(this);
		buttonPanel.add(b1);
		JButton b2=new JButton("add computer");
		b2.addActionListener(this);
		buttonPanel.add(b2);
		JButton b3=new JButton("add stereo");
		b3.addActionListener(this);
		buttonPanel.add(b3);
		JButton b4=new JButton("Quit");
		b4.addActionListener(this);
		buttonPanel.add(b4);
        contentPane.add(buttonPanel,BorderLayout.CENTER);
		f1.pack();
		f1.setVisible(true);
		f1.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
	}
	public void actionPerformed(ActionEvent e){
		String cmd=e.getActionCommand();
		if(cmd.equals("add software")){
			new addsoft(f1);	
		}
		else if(cmd.equals("add computer")){
			 new addcom(f1);
		}
		else if(cmd.equals("add stereo")){
			new addstereo(f1);
		}
		else if(cmd.equals("Quit")){
			System.exit(0);
		}

	}
}
//第三层窗口 1--add software
 class addsoft implements ActionListener
{

    JDialog dialog;
    JTextField tF1=new JTextField();
    JTextField tF2=new JTextField();
    JTextField tF3=new JTextField();
    JTextField tF4=new JTextField();
    JTextField tF5=new JTextField();
    JTextField tF6=new JTextField();

    @SuppressWarnings("deprecation")
	addsoft(JFrame f1){ //构造方法,从其调用方法中获得对话框的父窗口
        dialog = new JDialog(f1,"add software",true); //产生一modal对话框
        Container dialogPane = dialog.getContentPane();//接下来注意添加各个组件
        dialogPane.setLayout(new GridLayout(7,2));        
        dialogPane.add(new JLabel("name(string) : ",SwingConstants.CENTER)); 
        dialogPane.add(tF1);

        dialogPane.add(new JLabel("price(double) : ",SwingConstants.CENTER));
        dialogPane.add(tF2);

        dialogPane.add(new JLabel("description(string) ",SwingConstants.CENTER));
        dialogPane.add(tF3);

        dialogPane.add(new JLabel("reqSpace(int) ",SwingConstants.CENTER));
        dialogPane.add(tF4);

        dialogPane.add(new JLabel("os(string)",SwingConstants.CENTER));
        dialogPane.add(tF5);

        dialogPane.add(new JLabel("Softwaretype(string)",SwingConstants.CENTER));
        dialogPane.add(tF6);  

        JButton b1 = new JButton("ok");
        dialogPane.add(b1);
        JButton b2 = new JButton("no");
        dialogPane.add(b2);              
        b1.addActionListener(this);   //为两按钮增加事件监听器
        b2.addActionListener(this);
        dialog.setBounds(400,300,600,260);
        dialog.show();

    }

    public void actionPerformed(ActionEvent e){
    	
    	String cmd=e.getActionCommand();

    	if(cmd.equals("ok")){
            try{
                software sw=new software();
            	sw.product_name=tF1.getText();
            	sw.product_price=Double.parseDouble(tF2.getText());
            	sw.product_description=tF3.getText();
            	sw.reqSpace=Integer.parseInt(tF4.getText());
            	sw.os=tF5.getText();
            	sw.SoftType=tF6.getText();
            	ArrayList<software> soft = new ArrayList<software>(1000); 
            	soft.add(sw);
            	FileReader fr=new FileReader("production.txt");
                BufferedReader br=new BufferedReader(fr);
                String line=br.readLine();
                int i=1;
                while(line!=null){
                	i++;
                	line=br.readLine();
                	}
            	FileWriter fw=new FileWriter("production.txt",true);
            	BufferedWriter bw=new BufferedWriter(fw);
            	bw.write("category: software");
            	bw.write("    ID: "+i);
            	bw.write("    name:"+sw.product_name);
            	bw.write("    ¥"+(float) sw.product_price);
            	bw.write("    description:"+sw.product_description);
            	bw.write("    OS:"+sw.os);
            	bw.write("    SoftType:"+sw.SoftType);
            	bw.newLine();
            	bw.flush();
            	fw.close();
              }
              catch(Exception ex){}
    		System.out.println("success to add a new product!");
    		dialog.dispose();

    	}
    	else if(cmd.equals("no")){
    		dialog.dispose();
    	}
    }
}

//第三层窗口 2--add computer
 class addcom implements ActionListener
{
	static JFrame f21=null;
	public addcom(JFrame f1){
		f21=new JFrame("add computer");
		Container contentPane=f21.getContentPane();
		JPanel buttonPanel=new JPanel();
		buttonPanel.setBorder(BorderFactory.createLineBorder(Color.blue,2));
		buttonPanel.setLayout(new GridLayout(3,3));
		JButton b1=new JButton("add laptop");
		b1.addActionListener(this);
		buttonPanel.add(b1);
		JButton b2=new JButton("add desktop");
		b2.addActionListener(this);
		buttonPanel.add(b2);
		JButton b3=new JButton("Quit");
		b3.addActionListener(this);
		buttonPanel.add(b3);
        contentPane.add(buttonPanel,BorderLayout.CENTER);
		f21.pack();
		f21.setVisible(true);
		f21.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
	}
	public void actionPerformed(ActionEvent e){
		String cmd=e.getActionCommand();
		if(cmd.equals("add desktop")){
			new adddesktop(f21);	
		}
		else if(cmd.equals("add laptop")){
			new addlaptop(f21);
		}
		else if(cmd.equals("Quit")){
			System.exit(0);
		}

	}
}
//第四层窗口1----adddesktop
 class adddesktop implements ActionListener
{

    JDialog dialog;
    JTextField tF1=new JTextField();
    JTextField tF2=new JTextField();
    JTextField tF3=new JTextField();
    JTextField tF4=new JTextField();
    JTextField tF5=new JTextField();
    JTextField tF6=new JTextField();
    JTextField tF7=new JTextField();

    @SuppressWarnings("deprecation")
	adddesktop(JFrame f21){ //构造方法,从其调用方法中获得对话框的父窗口
        dialog = new JDialog(f21,"add desktop",true); //产生一modal对话框
        Container dialogPane = dialog.getContentPane();//接下来注意添加各个组件
        dialogPane.setLayout(new GridLayout(8,2));        
        dialogPane.add(new JLabel("name(string) : ",SwingConstants.CENTER)); 
        dialogPane.add(tF1);

        dialogPane.add(new JLabel("price(double) : ",SwingConstants.CENTER));
        dialogPane.add(tF2);

        dialogPane.add(new JLabel("description(string) ",SwingConstants.CENTER));
        dialogPane.add(tF3);

        dialogPane.add(new JLabel("ProcessorName(string) ",SwingConstants.CENTER));
        dialogPane.add(tF4);

        dialogPane.add(new JLabel("ProcessorSpeed(double)",SwingConstants.CENTER));
        dialogPane.add(tF5);

        dialogPane.add(new JLabel("Memory(int)",SwingConstants.CENTER));
        dialogPane.add(tF6);

        dialogPane.add(new JLabel("MonitorSize(int)",SwingConstants.CENTER));
        dialogPane.add(tF7);

        JButton b1 = new JButton("ok");
        dialogPane.add(b1);
        JButton b2 = new JButton("no");
        dialogPane.add(b2);              
        b1.addActionListener(this);   //为两按钮增加事件监听器
        b2.addActionListener(this);
        dialog.setBounds(400,300,600,260);
        dialog.show();
        

    }
    public void actionPerformed(ActionEvent e){
    	String cmd=e.getActionCommand();
    	if(cmd.equals("ok")){

            try{
                desktop dt=new desktop();
        		dt.product_name=tF1.getText();
        		dt.product_price=Double.parseDouble(tF2.getText());
           		dt.product_description=tF3.getText();
        		dt.ProcessorName=tF4.getText();
        		dt.ProcessorSpeed=Double.parseDouble(tF5.getText());
        		dt.Memory=Integer.parseInt(tF6.getText());
        		dt.MonitorSize=Integer.parseInt(tF7.getText());
            	ArrayList<desktop> desk= new ArrayList<desktop>(1000); 
            	desk.add(dt);
            	FileReader fr=new FileReader("production.txt");
                BufferedReader br=new BufferedReader(fr);
                String line=br.readLine();
                int i=0;
                while(line!=null){
                	i++;
                	line=br.readLine();
                	}  
            	FileWriter fw=new FileWriter("production.txt",true);
            	BufferedWriter bw=new BufferedWriter(fw);
            	bw.write("category: desktop");
            	bw.write("    ID: "+product.product_ID);
            	bw.write("    name:"+dt.product_name);
            	bw.write("    ¥"+(float) dt.product_price);
            	bw.write("    description:"+dt.product_description);
            	bw.write("    ProcessorName:"+dt.ProcessorName);
            	bw.write("    ProcessorSpeed:"+dt.ProcessorSpeed);
            	bw.write("    Memory:"+dt.Memory);
            	bw.write("    MonitorSize:"+dt.MonitorSize);
            	bw.newLine();
            	bw.flush();
            	fw.close();        
              }
              catch(Exception ex){}
    		System.out.println("success to add a new product!");
    		dialog.dispose();
    	}
    	else if(cmd.equals("no")){
    		dialog.dispose();
    	}
    }

}
//第四层窗口2----addlaptop
 class addlaptop implements ActionListener
{

    JDialog dialog;
    JTextField tF1=new JTextField();
    JTextField tF2=new JTextField();
    JTextField tF3=new JTextField();
    JTextField tF4=new JTextField();
    JTextField tF5=new JTextField();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -