我是根据书上关于MVC结构的示例改写成商品结账系统的,书上的示例运行后的窗口如下图。
(接下来,我放的东西有点多,额。。算是一个仿写代码的过程吧,看了以后可以防雷吧)
书上的示例代码如下:
Example10_15.java public class Example10_15 { public static void main(String args[]){ WindowTriangle win = new WindowTriangle(); win.setTitle("使用MVC结构"); win.setBounds(100,100,420,260); } } WindowTriangle.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class WindowTriangle extends JFrame implements ActionListener { Triangle triangle; //模型 JTextField textA,textB,textC; //视图 JTextArea showArea; //视图 JButton controlButton; //控制器 WindowTriangle() { init(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void init() { triangle = new Triangle(); textA = new JTextField(5); textB = new JTextField(5); textC = new JTextField(5); showArea = new JTextArea(); controlButton=new JButton("计算面积"); JPanel pNorth=new JPanel(); pNorth.add(new JLabel("边A:")); pNorth.add(textA); pNorth.add(new JLabel("边B:")); pNorth.add(textB); pNorth.add(new JLabel("边C")); pNorth.add(textC); pNorth.add(controlButton); controlButton.addActionListener(this); add(pNorth,BorderLayout.NORTH); add(new JScrollPane(showArea),BorderLayout.CENTER); } public void actionPerformed(ActionEvent e) { try{ double a = Double.parseDouble(textA.getText().trim()); double b = Double.parseDouble(textB.getText().trim()); double c = Double.parseDouble(textC.getText().trim()); triangle.setA(a) ; //更新数据 triangle.setB(b); triangle.setC(c); String area = triangle.getArea(); showArea.append("三角形"+a+","+b+","+c+"的面积:"); showArea.append(area+"\n"); //更新视图 } catch(Exception ex) { showArea.append("\n"+ex+"\n"); } } } Triangle.java public class Triangle { double sideA,sideB,sideC,area; boolean isTriange; public String getArea() { if(isTriange) { double p = (sideA+sideB+sideC)/2.0; area = Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC)) ; return String.valueOf(area); } else { return "无法计算面积"; } } public void setA(double a) { sideA = a; if(sideA+sideB>sideC&&sideA+sideC>sideB&&sideC+sideB>sideA) isTriange = true; else isTriange = false; } public void setB(double b) { sideB = b; if(sideA+sideB>sideC&&sideA+sideC>sideB&&sideC+sideB>sideA) isTriange = true; else isTriange = false; } public void setC(double c) { sideC = c; if(sideA+sideB>sideC&&sideA+sideC>sideB&&sideC+sideB>sideA) isTriange = true; else isTriange = false; } }
我自己改编的代码如下:
//Example10_15.java public class Example10_15 { public static void main(String args[]) { WindowStringBuffer win = new WindowStringBuffer(); win.setTitle("使用MVC结构"); win.setBounds(100,100,420,260); } } //WindowStringBuffer.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class WindowStringBuffer extends JFrame implements ActionListener { StringBuffer stringBuffer; //模型 JTextField textA,textB,textC,textD,textF; //视图 JTextArea showArea ; //视图 JButton controlButton; //控制器 WindowStringBuffer() { init(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void init() { stringBuffer = new StringBuffer(); textA = new JTextField(5); textB = new JTextField(5); textC = new JTextField(5); textD = new JTextField(5); textF = new JTextField(5); showArea = new JTextArea(); controlButton=new JButton("录入商品信息"); JPanel pNorth=new JPanel(); pNorth.add(new JLabel("商品编号:")); pNorth.add(textA); pNorth.add(new JLabel("商品名称:")); pNorth.add(textB); pNorth.add(new JLabel("商品产地:")); pNorth.add(textC); pNorth.add(new JLabel("商品单价:")); pNorth.add(textD); pNorth.add(new JLabel("商品数量:")); pNorth.add(textF); pNorth.add(controlButton); controlButton.addActionListener(this); //设置监视器 add(pNorth,BorderLayout.NORTH); add(new JScrollPane(showArea),BorderLayout.CENTER); } public void actionPerformed(ActionEvent e) //动作执行 { try{ int a = int.parseInt(textA.getText().trim()); String b = String.parseString(textB.getText().trim()); String c = String.parseString(textC.getText().trim()); double d = Double.parseDouble(textD.getText().trim()); int f = int.parseInt(textF.getText().trim()); stringBuffer.setGoodNum(a); //更新数据 stringBuffer.setGoodName(b); stringBuffer.setGoodArea(c); stringBuffer.setGoodValue(d); stringBuffer.setGoodSum(f); String information = stringBuffer.showInformation(); showArea.append("商品编号:"+a+","+"商品名称:"+b+","+"商品产地:"+c+","+"商品价格:"+d+","+"商品数量:"+f); showArea.append(information+"\n"); //更新视图 } catch(Exception ex) { showInformation.append("\n"+ex+"\n"); } } } //StringBuffer.java public class StringBuffer { int goodNum,goodSum; double goodValue; String goodName,goodArea; public String getInformation() { stringBuffer.showInformation(); public setGoodNum(int a) { goodNum = a; return a; } public setGoodName(string b) { goodName = b; return b; } public setGoodArea(string c) { goodArea= c; return c; } public setGoodValue(double d) { goodValue = d; return d; } public setGoodSum(int f) { goodSum = f; return f; } }
心痛2017....
我不知道我改的代码错在哪。。。难道显示商品属性不能用StringBuffer类做吗?!!
请大神指教也欢迎小伙伴前来讨论!
看风景非凡
文在内也
qq_迷宫的十字路口_2
我小时候可帅了
AUNO
相关分类