資源描述:
《圖形用戶界面設(shè)計(jì)表格組件示例》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫(kù)。
1、程序運(yùn)行結(jié)果截圖:程序清單如下:importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassExample10_16{publicstaticvoidmain(Stringarts[]){Winwin=newWin();}}classWinextendsJFrameimplementsActionListener{JTabletable;Objecta[][];Objectname[]={"商品名稱","單價(jià)","銷(xiāo)售量","銷(xiāo)售額"};JButtoncomputerRows,computerColums;J
2、TextFieldinputRowsNumber;intinitRows=1;JPanelpSouth,pNorth;intcount=0,rowsNumber=0;Win(){computerRows=newJButton("每件商品銷(xiāo)售額");computerColums=newJButton("總銷(xiāo)售額");inputRowsNumber=newJTextField(10);computerRows.addActionListener(this);computerColums.addActionListener(this);inputRowsNumber.addActionListe
3、ner(this);pSouth=newJPanel();pNorth=newJPanel();pNorth.add(newJLabel("輸入表格行數(shù),回車(chē)確認(rèn)"));pNorth.add(inputRowsNumber);pSouth.add(computerRows);pSouth.add(computerColums);add(pSouth,BorderLayout.SOUTH);add(pNorth,BorderLayout.NORTH);add(newJScrollPane(table),BorderLayout.CENTER);setBounds(100,100,370,25
4、0);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}publicvoidactionPerformed(ActionEvente){if(e.getSource()==inputRowsNumber){count=0;initRows=Integer.parseInt(inputRowsNumber.getText());a=newObject[initRows][4];for(inti=0;i5、Table(a,name);table.setRowHeight(20);getContentPane().removeAll();add(newJScrollPane(table),BorderLayout.CENTER);add(pSouth,BorderLayout.SOUTH);add(pNorth,BorderLayout.NORTH);validate();}elseif(e.getSource()==computerRows){introws=table.getRowCount();//獲取現(xiàn)有表格行數(shù)for(inti=0;i6、oleanboo=true;for(intj=1;j<=2;j++){try{sum=sum*Double.parseDouble(a[i][j].toString());}catch(Exceptionee){boo=false;table.repaint();//表格更新顯示}if(boo==true){a[i][3]=""+sum;//修改數(shù)組中的數(shù)據(jù)table.repaint();}}}}elseif(e.getSource()==computerColums){if(count==0){rowsNumber=table.getRowCount();//獲取表格的目前的行數(shù)coun
7、t++;}else{rowsNumber=table.getRowCount();//獲取表格的目前的行數(shù)rowsNumber=rowsNumber-1;//不要最后一行}doubletotalSum=0;for(intj=0;j