JPanel j2の、Count(i)の結果を、j1のLabelに反映させたい。
java
1import javax.swing.*; 2import java.awt.*; 3import java.awt.event.*; 4 5public class ScreenToucher extends JFrame { 6 7 int i=0; 8 static int width=500; 9 static int height=500; 10 11 public static void main(String args[]) { 12 ScreenToucher frame = new ScreenToucher("タイトル"); 13 frame.setVisible(true); 14 } 15 16 ScreenToucher(String title){ 17 setTitle(title); 18 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 19 setBounds(100, 100, width, height); 20 21 MyPanel1 p1 = new MyPanel1(); 22 MyPanel2 p2 = new MyPanel2(); 23 24 Container contentPane = getContentPane(); 25 contentPane.add(p1, BorderLayout.NORTH); 26 contentPane.add(p2, BorderLayout.CENTER); 27 } 28 29 } 30 31class MyPanel1 extends JPanel{ 32 33 int i; 34 35 MyPanel1(){ 36 JPanel jp1 = new JPanel(); 37 JLabel jl1 = new JLabel(); 38 jp1.setBackground(Color.green); 39 Integer j = new Integer(i); 40 String text = j.toString(); 41 jl1.setText(text); 42 jp1.add(jl1); 43 add(jp1); 44 45 }; 46 47 } 48 49class MyPanel2 extends JPanel { 50 51 static int width=500; 52 static int height=500; 53 static int i=0; 54 static int r = 60; 55 static int x; 56 static int y; 57 58 final static Color bc = Color.black; 59 final static Color dc = Color.green; 60 61 MyPanel2(){ 62 repaint(); 63 MouseListener(); 64 } 65 66 void MouseListener () { 67 addMouseListener(new MouseAdapter() { 68 public void mousePressed(MouseEvent e) { 69 double mouseX=e.getX(); 70 double mouseY=e.getY(); 71 if(mouseX>x&&mouseX<x+2*r) { 72 if(mouseY>y&&mouseY<y+2*r) { 73 74 repaint(); 75 Count(i); 76 } 77 } 78 } 79 }); 80 } 81 82 protected void paintComponent(Graphics g) { 83// super.paintComponent(g); 84 x = (int) (Math.random()*width); 85 y = (int) (Math.random()*height)+30; 86 if((x<width-2*r)&&(y<height-2*r)) { 87 g.setColor(bc); 88 g.fillRect(0, 0, width, height); 89 g.setColor(dc); 90 g.fillOval(x, y, r, r); 91 }else { 92 repaint(); 93 } 94 } 95 96 int Count(int i){ 97 i=i*100; 98 return i; 99 } 100 }
https://teratail.com/help/question-tips#questionTips3-4-1
回答1件
あなたの回答
tips
プレビュー