threadを利用し、1秒後に背景を変えたいのですが、変わりません。
swing
1package luna.sexydesign; 2 3import javax.swing.*; 4import java.awt.*; 5import java.awt.event.*; 6 7class Subthread extends Thread { 8 9 private MyPanel2 toucher; 10 11public void run(MyPanel2 toucher) { 12 try { 13 sleep(1000); 14 } catch (InterruptedException e) { 15 // TODO Auto-generated catch block 16 e.printStackTrace(); 17 } 18 this.toucher = toucher; 19 toucher.setBackground(Color.red);; 20 } 21} 22 23public class ScreenToucher extends JFrame { 24 25 int i = 0; 26 static int width = 500; 27 static int height = 500; 28 private MyPanel1 p1; 29 30 public static void main(String args[]) { 31 32 Subthread thread = new Subthread(); 33 thread.start(); 34 ScreenToucher frame = new ScreenToucher("タイトル"); 35 frame.setVisible(true); 36 } 37 38 ScreenToucher(String title) { 39 setTitle(title); 40 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 41 setBounds(100, 100, width, height); 42 43 p1 = new MyPanel1(); 44 MyPanel2 p2 = new MyPanel2(this); 45 46 Container contentPane = getContentPane(); 47 contentPane.add(p1, BorderLayout.NORTH); 48 contentPane.add(p2, BorderLayout.CENTER); 49} 50 51 public void setCount(int count) { 52 p1.setCount(count); 53 } 54} 55 56class MyPanel1 extends JPanel { 57 58 int i; 59 60 private JLabel jl1; 61 62 MyPanel1() { 63 JPanel jp1 = new JPanel(); 64 jl1 = new JLabel(); 65 jp1.setBackground(Color.green); 66 Integer j = new Integer(i); 67 String text = j.toString(); 68 jl1.setText(text); 69 jp1.add(jl1); 70 add(jp1); 71 } 72 73 public void setCount(int count) { 74 jl1.setText(Integer.toString(count)); 75 } 76 77} 78 79class MyPanel2 extends JPanel { 80 81 static int width = 500; 82 static int height = 500; 83 static int i = 0; 84 static int r = 60; 85 static int x; 86 static int y; 87 88 final static Color bc = Color.black; 89 final static Color dc = Color.green; 90 91 private ScreenToucher owner; 92 93 public MyPanel2(ScreenToucher owner) { 94 setBackground(Color.black); 95 this.owner = owner; 96 repaint(); 97 MouseListener(); 98 } 99 100 void MouseListener() { 101 addMouseListener(new MouseAdapter() { 102 public void mousePressed(MouseEvent e) { 103 double mouseX = e.getX(); 104 double mouseY = e.getY(); 105 if (mouseX > x && mouseX < x + 2 * r) { 106 if (mouseY > y && mouseY < y + 2 * r) { 107 repaint(); 108 owner.setCount(Count()); 109 } 110 } 111 } 112 }); 113 } 114 115 protected void paintComponent(Graphics g) { 116 super.paintComponent(g); 117 x = (int) (Math.random() * width); 118 y = (int) (Math.random() * height) + 30; 119 if ((x < width - 2 * r) && (y < height - 2 * r)) { 120 g.setColor(dc); 121 g.fillOval(x, y, r, r); 122 } else { 123 repaint(); 124 } 125 } 126 127 int Count() { 128 i += 100; 129 return i; 130 } 131}
クラスごとに分けよう
回答2件
あなたの回答
tips
プレビュー