亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

java:設置了buttonPanel按鈕后,怎么在事件監聽器中加入要顯示的圖案

java:設置了buttonPanel按鈕后,怎么在事件監聽器中加入要顯示的圖案

慕瓜6658770 2017-11-10 21:10:37
學習java中,,作業要求按不同的按鈕顯示不同的圖案,但我加入按鈕后,不知道在怎么實現圖案,actionPerformed方法中直接用Frame中定義buttonpanel對象嗎package?showFace; import?java.awt.*; import?java.awt.event.*; import?java.awt.geom.*; import?javax.swing.*; import?java.applet.*; class?drawsmile?extends?JPanel{???//畫笑臉 public?void?paintComponent(Graphics?g)?{ super.paintComponent(g); Graphics2D?g2=(Graphics2D)g; int?centerx=0; int?centery=0; int?minx=-200; int?miny=-200; int?maxx=200; int?maxy=200; g2.translate(800,5);?? g2.draw(new?Line2D.Double(minx,centery,maxx,centery));?? } } class?drawcry?extends?JPanel{????//流淚臉 public?void?paintComponent(Graphics?g)?{ super.paintComponent(g); Graphics2D?g2=(Graphics2D)g; int?centerx=0; int?centery=0; int?minx=-200; int?miny=-200; int?maxx=200; int?maxy=200; g2.translate(800,5);?? g2.draw(new?Line2D.Double(centerx,?miny,?centerx,?maxy)); } } class??DrawFrame?extends?JFrame{ JPanel?buttonPanel; public?DrawFrame(){ setTitle("顯示表情");????//窗口標題 setSize(500,500);???????//窗口大小 //創建按鈕 JButton?smileButton=new?JButton("微笑");????//創建按鈕“微笑” JButton?cryButton=new?JButton("流淚"); //創建繪制面板buttonPanel JPanel?buttonPanel=new?JPanel(); //圖形繪制中加入按鈕 buttonPanel.add(smileButton);? buttonPanel.add(cryButton); //按鈕加入面板 add(buttonPanel); //創建按鈕事件 FaceAction?smileAction=new?FaceAction("smile"); //將事件與按鈕連接起來 smileButton.addActionListener(smileAction); } //內部類? class?FaceAction?extends?JPanel?implements?ActionListener{???//編寫函數對應不同的類 private?String?a;??? Panel?panel=new?Panel(); public?FaceAction?(String?a){?//構造函數 ???if(a=="smile")?{ drawsmile?panel=new?drawsmile();??? ???} ? ???if(a=="cry")?{} ????drawcry?panel?=new?drawcry(); ???????} ???public?void?actionPerformed(ActionEvent?event)?{ ???buttonPanel.add(panel); ???} ?? } } public?class?showface?{//主函數showface為測試函數 public?static?void?main(String[]?args)?{ //?TODO?Auto-generated?method?stub ?????????DrawFrame?a=new?DrawFrame();??????//框架定義 ?????????a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ?????????a.setVisible(true); } }
查看完整描述

1 回答

  • 1 回答
  • 1 關注
  • 2544 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號