作者qrtt1 (null)
看板java
标题Re: [问题] 关於Windowing
时间Sat Feb 20 16:28:47 2010
※ 引述《BlackMatrix (BlackMatrix)》之铭言:
: 最近教授有教怎麽做出简单&上面有按钮的程序
: 那...教授叫我们做出一个程序
: 有八个按钮, 然後一开始程序就会设定每种按钮的颜色, 然後点击按钮的时候会变换
: 那个被点击的按钮的框框会自动改变随机的颜色.
: /////////想像//////// START
: 以下是想像的程序码
: int x = 0;
: if x = 1 那就是我第一个按钮
: 就是设定一个Counter, 像我的程序码一样, 然後每一个数字代表一个JButton
: 就是如果counter ++, 那麽counter在2的时候就是会自动的等於我的JButton
: 再利用X.setBackground(XXXX颜色), 可是因为我知道教授给我的数目是八个
: 所以我就利用JButton one, two, three four到eight
: 万一教授没有指定框框数目可是又要求我们把每一个框框随机不同颜色,点击不同
: 的颜色怎麽办?
: 简单来说, 要怎麽设定N个框框, 随机颜色很简单, 可是还有另一个重点就是
: ActiveListener的GetSource要怎麽把他设定成只要是我刚刚设定的N个框框要是被点击
: 就会自动改变颜色?
: /////////想像//////// END
: 可是问题是我不会让X = 1 等於一个JButton
: 所以我就写出以下的程序, 可以完成老师要求的事, 可是我不知道有什麽更简单的方法
: 可以做出相同的事情
: Frame:
: http://nopaste.csie.org/211d0
: Main:
: http://nopaste.csie.org/50a20
======================================================================
if (e.getSource() == oneOneButton)
{
Random randomGeneratorThree = new Random();
int randomInt = randomGeneratorThree.nextInt(13);
if (randomInt == 0)
{
oneOneButton.setBackground(Color.black);
}
else if (randomInt == 1)
{
========================================================================
何必在乎 e.getSource() 是那一个 reference 呢?
重点应该是确保它是一个 JButton,
而只要是 JButton 就会有 .setBackground() 方法可以用。
Swing 并不会你按的是 oneOneButton 却把 oneTwoButton
作为 event source 传递给你。所以应该改成:
if(e.getSource() instanceof JButton){
JButton btn = (JButton) e.getSource();
btn.setBackground(..........................);
}
=========================================================================
if (randomInt == 0)
{
oneOneButton.setBackground(Color.black);
}
else if (randomInt == 1)
{
oneOneButton.setBackground(Color.blue);
}
else if (randomInt == 2)
{
oneOneButton.setBackground(Color.cyan);
}
else if (randomInt == 3)
{
oneOneButton.setBackground(Color.darkGray);
}
else if (randomInt == 4)
{
oneOneButton.setBackground(Color.gray);
}
else if (randomInt == 5)
{
oneOneButton.setBackground(Color.green);
}
else if (randomInt == 6)
{
oneOneButton.setBackground(Color.lightGray);
}
else if (randomInt == 7)
{
oneOneButton.setBackground(Color.magenta);
}
else if (randomInt == 8)
{
oneOneButton.setBackground(Color.orange);
}
else if (randomInt == 9)
{
oneOneButton.setBackground(Color.pink);
}
else if (randomInt == 10)
{
oneOneButton.setBackground(Color.red);
}
else if (randomInt == 11)
{
oneOneButton.setBackground(Color.white);
}
else if (randomInt == 12)
{
oneOneButton.setBackground(Color.yellow);
}
}
===========================================================================
这很明显是简单地数字与 Color 物件的对应关系,
12 => yellow
11 => white
10 => red
.......
刚好能简单化为阵列:
Color[] COLORS = new Color[] { Color.BLACK, Color.BLUE, Color.CYAN,
Color.DARK_GRAY, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY,
Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE,
Color.YELLOW };
那麽一切就变得很容易了:
btn.setBackground(COLORS[randomGenerator.nextInt(13)]);
PS. 不要执着於 reference,应针对 reference 代表的物件/介面去操作。
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.168.161
※ 编辑: qrtt1 来自: 140.112.168.161 (02/20 16:32)
※ 编辑: qrtt1 来自: 140.112.168.161 (02/20 17:07)
1F:推 BlackMatrix:谢谢qrtt1的提示, 我正在努力修改我的Code 02/20 17:17
2F:推 BlackMatrix:谢谢qrtt1的提示, 我已经把Code做完了, 感谢 02/20 17:52