現在Java初心者です。学校でJavaの勉強をしています。今回もプログラムをていしゅつ使用としたのですがクラスYPanel,XPanelが重複していますというエラーが出てしまいました。これに対してどのようにすれば解決できるのかご指導いただければ幸いです。少しコードが汚いかもしれません。あらかじめお詫びいたします。
Java
1 import java.awt.*; 2 import java.awt.event.*; 3 4 class YPanel extends XPanel { 5 Panel innerPanel3; 6 Panel innerPanel4; 7 8 public YPanel() { 9 // setLayout(null); 10 innerPanel3 = new Panel(); 11 add(innerPanel3); 12 innerPanel3.setBackground(Color.black); 13 14 innerPanel4 = new Panel(); 15 add(innerPanel4); 16 innerPanel4.setBackground(Color.black); 17 } 18 public void setBounds(int x, int y, int width, int height) { 19 super.setBounds(x,y,width,height); 20 innerPanel3.setBounds(4,height-8,width-8,8); 21 innerPanel4.setBounds(width-8,4,8,height-8); 22 } 23 } 24 25 class XPanel extends Panel { 26 Panel innerPanel1; 27 Panel innerPanel2; 28 29 public XPanel() { 30 setLayout(null); 31 innerPanel1 = new Panel(); 32 add(innerPanel1); 33 innerPanel1.setBackground(Color.white); 34 35 innerPanel2 = new Panel(); 36 add(innerPanel2); 37 innerPanel2.setBackground(Color.white); 38 } 39 public void setBounds(int x, int y, int width, int height) { 40 super.setBounds(x,y,width,height); 41 innerPanel1.setBounds(0,0,width-8,8); 42 innerPanel2.setBounds(0,0,8,height-8); 43 } 44 } 45 46 class ex7_4 { 47 static Frame myframe; 48 static XPanel mypanel1; 49 static YPanel mypanel2; 50 static YPanel mypanel3; 51 static YPanel mypanelline3[]; 52 static int score = 0; 53 54 public static void main(String args[]) { 55 int i; 56 57 myframe = new Frame(); 58 mypanel1 = new XPanel(); 59 mypanel2 = new YPanel(); 60 mypanel3 = new YPanel(); 61 62 myframe.setLayout(null); // does not use layout manager 63 myframe.setSize(400,600); // window size : width = 400, height = 600 64 myframe.setVisible(true); // make the window visible 65 66 myframe.add(mypanel1); 67 mypanel1.setBounds(30,40,100,100); 68 mypanel1.setBackground(Color.green); 69 70 myframe.add(mypanel2); 71 mypanel2.setBounds(150,40,100,100); 72 mypanel2.setBackground(Color.yellow); 73 74 myframe.add(mypanel3); 75 mypanel3.setBounds(270,40,100,100); 76 mypanel3.setBackground(Color.red); 77 78 mypanelline3 = new YPanel[5]; 79 for( i = 0; i < 5; i++ ) { 80 mypanelline3[i] = new YPanel(); 81 int w = 50/(i+1); 82 mypanelline3[i].setBounds((150+50-w/2)+((270-150)*((i-5)*(i-5))/25), 83 (130+250-w/2)-(10*((i-5)*(i-5))),w,w); 84 mypanelline3[i].setBackground(Color.gray); 85 mypanelline3[i].setVisible(false); 86 } 87 88 89 // o ma ji na i ! 90 mypanel3.addMouseListener(new MouseAdapter() { 91 public void mouseReleased(MouseEvent e) { 92 mypanel3MouseReleased(e); 93 } 94 } ); 95 96 while(true) { 97 sleep(300); 98 if( mypanelline3[0].isVisible() ) { 99 score -= 10; 100 System.out.print("SCORE:" + score + "\n"); 101 mypanel3.setBackground(Color.gray); 102 } else { 103 mypanel3.setBackground(Color.red); 104 } 105 for( i = 0; i < 4; i++ ) { 106 boolean v = mypanelline3[i+1].isVisible(); 107 mypanelline3[i].setVisible(v); 108 } 109 mypanelline3[4].setVisible(Math.random() > 0.7); 110 } 111 112 } 113 public static void mypanel3MouseReleased(MouseEvent e) { 114 if( mypanelline3[0].isVisible() == true ) { 115 mypanel3.setBackground(Color.white); 116 mypanelline3[0].setVisible(false); 117 score += 30; 118 System.out.print("SCORE:" + score + "\n"); 119 } else { 120 mypanel3.setBackground(Color.gray); 121 score -= 5; 122 System.out.print("SCORE:" + score + "\n"); 123 } 124 125 } 126 static void sleep(long msec) { 127 try{ 128 Thread.sleep(msec); 129 }catch(InterruptedException ie) { 130 } 131 } 132 }
(
(実行結果)
赤、緑、黄色の3つの四角および黒い線が表示されます。実行結果が文字で表すとこんなかんじになってしまいます。
SCORE:-10
SCORE:-20
SCORE:-30
SCORE:-40
SCORE:-50
SCORE:-60
SCORE:-70
SCORE:-80
SCORE:-90
SCORE:-100
SCORE:-110
(上がコマンドプロンプトでの実行結果)
(以下チェッカが導き出したエラーがこれです。)java ex7_4 4行目 で YPanelクラスが重複しています。
java ex7_4 25行目 でXPanelクラスが重複しています。
この2つがエラーとして表示されました。
回答1件
あなたの回答
tips
プレビュー