按钮会改变颜色,但我需要它们在我单击它们时停止
Buttons change color, but I need them to stop when I click them
这是我第一次在这里发帖提问。我是 java 的新手,目前正在学习这方面的课程。这是作业:"Modify your button GUI program so that the buttons change color about every one second unless they've been pressed." ...这就是我得到的所有说明。哈哈,没什么!
所以我现在知道,当单击按钮时,它会变成白色并停止变化。从技术上讲,这满足给出的说明,对吗?我不认为那是他们想要的……而且我只是改变不透明度,所以它仍在改变颜色,你只是看不到它,对吧?所以我想知道的是,是否有一种方法可以阻止按钮改变颜色,但保持它已经拥有的颜色,就像冻结它一样,而不是把它变成白色?我有一个静态的 JFrame jf,在 main 和所有正确的导入之外按下静态布尔值。我的 getColor() 函数只是 returns 一种随机颜色。感谢 help/advice!!
public static void main(String[] args) {
jf = new JFrame("Homework 2");//constructed
jf.setSize(400,400);//sets window size
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//closes program
jf.setLayout(new GridLayout(2,4));
ArrayList<JButton> buttons = new ArrayList<JButton>();//array of button
pressed = true;
for(int i=1; i <= 8; i++) { //creates 8 buttons
JButton jb = new JButton();
jb.setText("Button " + i);
jb.setOpaque(pressed);
jb.setBorderPainted(false);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton theButton = (JButton)e.getSource();
theButton.setOpaque(!pressed);//makes it white if it has been clicked
}
});
buttons.add(jb);//add the button to the array
jf.add(jb);//adding to frame
}
jf.setVisible(true);//makes the window appear
while(true) {
for (JButton button : buttons){
button.setBackground(getColor());//change colors
}
try {
Thread.sleep(1000);//unless
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
我会创建一个包含 8 个布尔值的数组来跟踪按钮。并在此处检查按钮是否应更改其颜色:
for (int i = 0; i < 8; ++i){
if(!pressedArr[i]){
button.setBackground(getColor());//change colors
}
}
还需要跟踪所有的八个值,当它们都是true
时,就跳出while
循环
要从进一步的颜色变化中删除(停止)按钮,请在执行操作时将其从 buttons
数组列表中删除。
这是我第一次在这里发帖提问。我是 java 的新手,目前正在学习这方面的课程。这是作业:"Modify your button GUI program so that the buttons change color about every one second unless they've been pressed." ...这就是我得到的所有说明。哈哈,没什么!
所以我现在知道,当单击按钮时,它会变成白色并停止变化。从技术上讲,这满足给出的说明,对吗?我不认为那是他们想要的……而且我只是改变不透明度,所以它仍在改变颜色,你只是看不到它,对吧?所以我想知道的是,是否有一种方法可以阻止按钮改变颜色,但保持它已经拥有的颜色,就像冻结它一样,而不是把它变成白色?我有一个静态的 JFrame jf,在 main 和所有正确的导入之外按下静态布尔值。我的 getColor() 函数只是 returns 一种随机颜色。感谢 help/advice!!
public static void main(String[] args) {
jf = new JFrame("Homework 2");//constructed
jf.setSize(400,400);//sets window size
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//closes program
jf.setLayout(new GridLayout(2,4));
ArrayList<JButton> buttons = new ArrayList<JButton>();//array of button
pressed = true;
for(int i=1; i <= 8; i++) { //creates 8 buttons
JButton jb = new JButton();
jb.setText("Button " + i);
jb.setOpaque(pressed);
jb.setBorderPainted(false);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton theButton = (JButton)e.getSource();
theButton.setOpaque(!pressed);//makes it white if it has been clicked
}
});
buttons.add(jb);//add the button to the array
jf.add(jb);//adding to frame
}
jf.setVisible(true);//makes the window appear
while(true) {
for (JButton button : buttons){
button.setBackground(getColor());//change colors
}
try {
Thread.sleep(1000);//unless
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
我会创建一个包含 8 个布尔值的数组来跟踪按钮。并在此处检查按钮是否应更改其颜色:
for (int i = 0; i < 8; ++i){
if(!pressedArr[i]){
button.setBackground(getColor());//change colors
}
}
还需要跟踪所有的八个值,当它们都是true
时,就跳出while
循环
要从进一步的颜色变化中删除(停止)按钮,请在执行操作时将其从 buttons
数组列表中删除。