📄 addsoft.java
字号:
JTextField tF6=new JTextField();
JTextField tF7=new JTextField();
JTextField tF8=new JTextField();
addlaptop(JFrame f21){ //构造方法,从其调用方法中获得对话框的父窗口
dialog = new JDialog(f21,"add laptop",true); //产生一modal对话框
Container dialogPane = dialog.getContentPane();//接下来注意添加各个组件
dialogPane.setLayout(new GridLayout(9,2));
dialogPane.add(new JLabel("name(srting) : ",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("thickness(double)",SwingConstants.CENTER));
dialogPane.add(tF7);
dialogPane.add(new JLabel("weight(double)",SwingConstants.CENTER));
dialogPane.add(tF8);
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{
laptop lt=new laptop();
lt.product_name=tF1.getText();
lt.product_price=Double.parseDouble(tF2.getText());
lt.product_description=tF3.getText();
lt.ProcessorName=tF4.getText();
lt.ProcessorSpeed=Double.parseDouble(tF5.getText());
lt.Memory=Integer.parseInt(tF6.getText());
lt.thickness=Double.parseDouble(tF7.getText());
lt.weight=Double.parseDouble(tF8.getText());
ArrayList<laptop> lap= new ArrayList<laptop>(1000);
lap.add(lt);
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();
}
br.close();
fr.close();
FileWriter fw=new FileWriter("production.txt",true);
BufferedWriter bw=new BufferedWriter(fw);
bw.write("category: laptop");
bw.write(" ID: "+product.product_ID);
bw.write(" name:"+lt.product_name);
bw.write(" ¥"+(float) lt.product_price);
bw.write(" description:"+lt.product_description);
bw.write(" ProcessorName:"+lt.ProcessorName);
bw.write(" ProcessorSpeed:"+lt.ProcessorSpeed);
bw.write(" Memory:"+lt.Memory);
bw.write(" thickness:"+lt.thickness);
bw.write(" weight:"+lt.weight);
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 stereo
class addstereo implements ActionListener
{
static JFrame f22=null;
public addstereo(JFrame f1){
f22=new JFrame("add stereo");
Container contentPane=f22.getContentPane();
JPanel buttonPanel=new JPanel();
buttonPanel.setBorder(BorderFactory.createLineBorder(Color.blue,2));
buttonPanel.setLayout(new GridLayout(3,3));
JButton b1=new JButton("add hometheater");
b1.addActionListener(this);
buttonPanel.add(b1);
JButton b2=new JButton("add carstereo");
b2.addActionListener(this);
buttonPanel.add(b2);
JButton b3=new JButton("Quit");
b3.addActionListener(this);
buttonPanel.add(b3);
contentPane.add(buttonPanel,BorderLayout.CENTER);
f22.pack();
f22.setVisible(true);
f22.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
String cmd=e.getActionCommand();
if(cmd.equals("add hometheater")){
new addhometheater(f22);
}
else if(cmd.equals("add carstereo")){
new addcarstereo(f22);
}
else if(cmd.equals("Quit")){
System.exit(0);
}
}
}
//第四层窗口3----addhometheater
class addhometheater 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")
addhometheater(JFrame f22){ //构造方法,从其调用方法中获得对话框的父窗口
dialog = new JDialog(f22,"add hometheater",true); //产生一modal对话框
Container dialogPane = dialog.getContentPane();//接下来注意添加各个组件
dialogPane.setLayout(new GridLayout(7,2));
dialogPane.add(new JLabel("name : ",SwingConstants.CENTER));
dialogPane.add(tF1);
dialogPane.add(new JLabel("price : ",SwingConstants.CENTER));
dialogPane.add(tF2);
dialogPane.add(new JLabel("description ",SwingConstants.CENTER));
dialogPane.add(tF3);
dialogPane.add(new JLabel("speaker ",SwingConstants.CENTER));
dialogPane.add(tF4);
dialogPane.add(new JLabel("watts",SwingConstants.CENTER));
dialogPane.add(tF5);
dialogPane.add(new JLabel("channel",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{
hometheater ht=new hometheater();
ht.product_name=tF1.getText();
ht.product_price=Double.parseDouble(tF2.getText());
ht.product_description=tF3.getText();
ht.speaker=Integer.parseInt(tF4.getText());
ht.watts=Integer.parseInt(tF5.getText());
ht.channel=Integer.parseInt(tF6.getText());
ArrayList<hometheater> home= new ArrayList<hometheater>(1000);
home.add(ht);
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: hometheater");
bw.write(" ID: "+product.product_ID);
bw.write(" name:"+ht.product_name);
bw.write(" ¥"+(float)ht.product_price);
bw.write(" description:"+ht.product_description);
bw.write(" speaker:"+ht.speaker);
bw.write(" watts:"+ht.watts);
bw.write(" channel:"+ht.channel);
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();
}
}
}
//第四层窗口3----addcarstereo
class addcarstereo 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")
addcarstereo(JFrame f22){ //构造方法,从其调用方法中获得对话框的父窗口
dialog = new JDialog(f22,"add carstereo",true); //产生一modal对话框
Container dialogPane = dialog.getContentPane();//接下来注意添加各个组件
dialogPane.setLayout(new GridLayout(7,2));
dialogPane.add(new JLabel("name : ",SwingConstants.CENTER));
dialogPane.add(tF1);
dialogPane.add(new JLabel("price : ",SwingConstants.CENTER));
dialogPane.add(tF2);
dialogPane.add(new JLabel("description ",SwingConstants.CENTER));
dialogPane.add(tF3);
dialogPane.add(new JLabel("speaker ",SwingConstants.CENTER));
dialogPane.add(tF4);
dialogPane.add(new JLabel("watts",SwingConstants.CENTER));
dialogPane.add(tF5);
dialogPane.add(new JLabel("removable",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{
carstereo cs=new carstereo();
cs.product_name=tF1.getText();
cs.product_price=Double.parseDouble(tF2.getText());
cs.product_description=tF3.getText();
cs.speaker=Integer.parseInt(tF4.getText());
cs.watts=Integer.parseInt(tF5.getText());
cs.removable=Boolean.parseBoolean(tF6.getText());
ArrayList<carstereo> car= new ArrayList<carstereo>(1000);
car.add(cs);
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: caarstereo");
bw.write(" ID: "+product.product_ID);
bw.write(" name:"+cs.product_name);
bw.write(" ¥"+(float)cs.product_price);
bw.write(" description:"+cs.product_description);
bw.write(" speaker:"+cs.speaker);
bw.write(" watts:"+cs.watts);
bw.write(" removable:"+cs.removable);
bw.newLine();
bw.flush();
fw.close();
}
catch(Exception ex){}
System.out.println("success to add a new product!");
dialog.dispose();//to do something
}
else if(cmd.equals("no")){
dialog.dispose();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -