我很难在一行中解释我的问题,所以我决定解释我的计划的目标。基本上做的程序是用不同的颜色改变背景颜色。第一个是红色,第二个是绿色,第三个是蓝色,第四个是惊喜,第五个是白色,也称为默认。
也可以使用键盘快捷键调用所有这些来源。首先,我创建我的JFrame
类(The Marc),然后创建包含JPanel
s的JButton
类(lamina或panel)。但在创建它之前,有一些代码可以创建一个随机颜色,它是使用惊讶JButton
的颜色,这是我的问题。
在程序中,当我按下运行按钮,然后单击惊喜JButton
或使用键盘时,它会用随机颜色绘制背景(正如我们想要的那样,现在一切都是正确的)。
如果我第二次按下按钮,颜色与我们之前获得的颜色相同。那么,我怎么能在下次按它时让JButton(惊喜)显示出一种新的不同颜色?换句话说,如何在不重新打开程序窗口的情况下重置值?
下面是我的程序代码和程序外观的图像。我知道这些是很多文字,但我不知道如何描述这个问题。为了使问题更清楚,我加入了另一个具有我想要的目标的程序,但它只有一个按钮(随机颜色的按钮)。我希望惊喜按钮像第二个程序的随机按钮一样工作。
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Shortcut {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Open the Window when execute the program.
shortMarc marc1=new shortMarc();
}
}
//Create the Window.
class shortMarc extends JFrame{
public shortMarc() {
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit myscreen=Toolkit.getDefaultToolkit();
Dimension screenSize=myscreen.getScreenSize();
int heightScreen=screenSize.height;
int widthScreen=screenSize.width;
setSize(widthScreen/2, heightScreen/2);
setLocation(widthScreen/4, heightScreen/4);
setTitle("Hello My World!");
shortLamina laminax=new shortLamina();
add(laminax);
Image icon1=myscreen.getImage("blackcircle.png");
setIconImage(icon1);
}
}
//Create a layer above the marc.
class shortLamina extends JPanel{
public shortLamina() {
//It creates an object that contain a random color to use it later on the program.
int extra=10;
int valorDado1=(int) ((Math.random()*256)+extra);
int valorDado2=(int) ((Math.random()*256)+extra);
int valorDado3=(int) ((Math.random()*256)+extra);
if(valorDado1>255) {
valorDado1=valorDado1-extra;
}
if(valorDado2>255) {
valorDado2=valorDado2-extra;
}
if(valorDado3>255) {
valorDado3=valorDado3-extra;
}
Color randomcolor=new Color(valorDado1, valorDado2, valorDado3);
// Create the user tangible objects. In this case the buttons.
ActionColor actionGreen=new ActionColor("Green", new ImageIcon("green.png"), Color.GREEN, "It makes the background green (Ctrl G)");
ActionColor actionBlue=new ActionColor("Blue", new ImageIcon("blue.png"), Color.BLUE, "It makes the background blue (Ctrl B)");
ActionColor actionRed=new ActionColor("Red", new ImageIcon("red.png"), Color.RED, "It makes the background red (Ctrl R");
ActionColor actionDefault=new ActionColor("Default", Color.WHITE, "It makes the background white, the default color (Ctrl D)");
ActionColor actionRandom=new ActionColor("Sorprise", randomcolor, "It paint the background with a random color (Ctrl S)");
JButton button1=new JButton(actionGreen);
JButton button2=new JButton(actionBlue);
JButton button3=new JButton(actionRed);
JButton button4=new JButton(actionDefault);
JButton button5=new JButton(actionRandom);
add(button3);
add(button1);
add(button2);
add(button5);
add(button4);
//Assign the key combinations.
InputMap inputmap=getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);//Step 1. Create a InputMap. It indicates wherre is the source object.
KeyStroke green_key=KeyStroke.getKeyStroke("ctrl G");// Step 2. Create a Key combination.
KeyStroke blue_key=KeyStroke.getKeyStroke("ctrl B");
KeyStroke red_key=KeyStroke.getKeyStroke("ctrl R");
KeyStroke default_key=KeyStroke.getKeyStroke("ctrl D");
KeyStroke random_key=KeyStroke.getKeyStroke("ctrl S");
inputmap.put(green_key, "green_background"); //Step 3. Asign the key to the place of the sorce object.
inputmap.put(blue_key, "blue_background");
inputmap.put(red_key, "red_background");
inputmap.put(default_key, "default_background");
inputmap.put(random_key, "random_background");
ActionMap actionmap=getActionMap();//Step 4. Create an object of type "action map" (instanciar) in spanish, to use it methods.
actionmap.put("green_background", actionGreen);//Step 5. Asign the object of the key combination to the action created before using the method put of the actionmap object.
actionmap.put("blue_background", actionBlue);
actionmap.put("red_background", actionRed);
actionmap.put("default_background", actionDefault);
actionmap.put("random_background", actionRandom);
}
//Create the class that asign the constructor methods to the buttons.
private class ActionColor extends AbstractAction{
//Constructor method 1.
public ActionColor(String nam, Icon icon, Color colorButton, String des) {
putValue(Action.NAME, nam);
putValue(Action.SMALL_ICON, icon);
putValue(Action.SHORT_DESCRIPTION, des);
putValue("background color", colorButton);
}
//Constructor method 2.
public ActionColor(String nam, Color colorButton, String des) {
putValue(Action.NAME, nam);
putValue(Action.SHORT_DESCRIPTION, des);
putValue("background color", colorButton);
}
//The listener class. It recive the instructions.
public void actionPerformed(ActionEvent e) {
Color c=(Color) getValue("background color");
setBackground(c);
}
}
}
这是我的第二个程序,它按我的意思工作,但它只有一个按钮。我想将此按钮合并到我的其他程序中,但我不知道如何。我希望它能使我的问题更清楚。
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class ChangingColors {
public static void main(String[] args) {
// TODO Auto-generated method stub
colorMarc marc1=new colorMarc();
}
}
class colorMarc extends JFrame{
public colorMarc() {
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit myscreen=Toolkit.getDefaultToolkit();
Dimension screenSize=myscreen.getScreenSize();
int heightScreen=screenSize.height;
int widthScreen=screenSize.width;
setSize(736, 524);
setLocation(widthScreen/4, heightScreen/4);
setTitle("Hello Color!!!");
//Lamina part.
lamina4 mylamina=new lamina4();
add(mylamina);
}
}
//Lamina.
class lamina4 extends JPanel implements ActionListener{
JButton buttonToChange=new JButton("Change color");
//int valorDado = (int)Math.random()*255+1;
public lamina4() {
add(buttonToChange);
buttonToChange.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
int extra=10;
int valorDado1=(int) ((Math.random()*256)+extra);
int valorDado2=(int) ((Math.random()*256)+extra);
int valorDado3=(int) ((Math.random()*256)+extra);
if(valorDado1>255) {
valorDado1=valorDado1-extra;
}
if(valorDado2>255) {
valorDado2=valorDado2-extra;
}
if(valorDado3>255) {
valorDado3=valorDado3-extra;
}
Color randomcolor=new Color(valorDado1, valorDado2, valorDado3);
/*System.out.println(valorDado1);
System.out.println(valorDado2);
System.out.println(valorDado3);
System.out.println();*/
setBackground(randomcolor);
}
}
答案 0 :(得分:0)
生成随机颜色的当前代码是:
int extra=10;
int valorDado1=(int) ((Math.random()*256)+extra);
int valorDado2=(int) ((Math.random()*256)+extra);
int valorDado3=(int) ((Math.random()*256)+extra);
if(valorDado1>255) {
valorDado1=valorDado1-extra;
}
if(valorDado2>255) {
valorDado2=valorDado2-extra;
}
if(valorDado3>255) {
valorDado3=valorDado3-extra;
}
Color randomcolor=new Color(valorDado1, valorDado2, valorDado3);
现在让我们关注这些代码行:
int valorDado1=(int) ((Math.random()*256)+extra);
int valorDado2=(int) ((Math.random()*256)+extra);
int valorDado3=(int) ((Math.random()*256)+extra);
......更具体地说:
((Math.random()*256)+extra);
以上所做的就是选择10到266之间的数字。因此,valorDado1
,valorDado2
和valorDado3
最有可能小于255,您需要更改
if(valorDado1>255) {
valorDado1=valorDado1-extra;
}
if(valorDado2>255) {
valorDado2=valorDado2-extra;
}
if(valorDado3>255) {
valorDado3=valorDado3-extra;
}
到
if(valorDado1<=255) {
valorDado1=valorDado1-extra;
}
if(valorDado2<=255) {
valorDado2=valorDado2-extra;
}
if(valorDado3<=255) {
valorDado3=valorDado3-extra;
}
(注意比较运算符已更改)
答案 1 :(得分:0)
我找到了答案。为此,我需要在薄层的构造方法之外建立随机颜色。如果你不这样做,并在构造函数中设置随机颜色,那么至少你关闭并重新打开程序的coloralways是相同的。解决方案是创建另一个抽象操作类。
最终的代码是:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Shortcut {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Open the Window when execute the program.
shortMarc marc1=new shortMarc();
}
}
//Create the Window.
class shortMarc extends JFrame{
public shortMarc() {
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
shortLamina laminax=new shortLamina();
add(laminax);
Toolkit myscreen=Toolkit.getDefaultToolkit();
Dimension screenSize=myscreen.getScreenSize();
int heightScreen=screenSize.height;
int widthScreen=screenSize.width;
setSize(widthScreen/2, heightScreen/2);
setLocation(widthScreen/4, heightScreen/4);
setTitle("Changing colors game!");
Image icon1=myscreen.getImage("blackcircle.png");
setIconImage(icon1);
}
}
//Create a layer above the marc.
class shortLamina extends JPanel{
public shortLamina() {
//It creates an object that contain a random color to use it later on the program.
int extra=10;
int valorDado1=(int) ((Math.random()*256)+extra);
int valorDado2=(int) ((Math.random()*256)+extra);
int valorDado3=(int) ((Math.random()*256)+extra);
if(valorDado1>255) {
valorDado1=valorDado1-extra;
}
if(valorDado2>255) {
valorDado2=valorDado2-extra;
}
if(valorDado3>255) {
valorDado3=valorDado3-extra;
}
Color randomcolor=new Color(valorDado1, valorDado2, valorDado3);
// Create the user tangible objects. In this case the buttons.
ActionColor actionGreen=new ActionColor("Green", new ImageIcon("green.png"), Color.GREEN, "It makes the background green (Ctrl G)");
ActionColor actionBlue=new ActionColor("Blue", new ImageIcon("blue.png"), Color.BLUE, "It makes the background blue (Ctrl B)");
ActionColor actionRed=new ActionColor("Red", new ImageIcon("red.png"), Color.RED, "It makes the background red (Ctrl R");
ActionColor actionDefault=new ActionColor("Default", Color.WHITE, "It makes the background white, the default color (Ctrl D)");
ActionRandom actionRandom=new ActionRandom("Random", randomcolor, "It paint the background with a random color (Ctrl X, Ctrl S)");
JButton button1=new JButton(actionGreen);
JButton button2=new JButton(actionBlue);
JButton button3=new JButton(actionRed);
JButton button4=new JButton(actionDefault);
JButton button5=new JButton(actionRandom);
add(button3);
add(button1);
add(button2);
add(button5);
add(button4);
//Assign the key combinations.
InputMap inputmap=getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);//Step 1. Create a InputMap. It indicates wherre is the source object.
KeyStroke green_key=KeyStroke.getKeyStroke("ctrl G");// Step 2. Create a Key combination.
KeyStroke blue_key=KeyStroke.getKeyStroke("ctrl B");
KeyStroke red_key=KeyStroke.getKeyStroke("ctrl R");
KeyStroke default_key=KeyStroke.getKeyStroke("ctrl D");
KeyStroke random_key=KeyStroke.getKeyStroke("ctrl X");
KeyStroke random_key2=KeyStroke.getKeyStroke("ctrl S");
inputmap.put(green_key, "green_background"); //Step 3. Asign the key to the place of the sorce object.
inputmap.put(blue_key, "blue_background");
inputmap.put(red_key, "red_background");
inputmap.put(default_key, "default_background");
inputmap.put(random_key, "random_background");
inputmap.put(random_key2, "random_background");
ActionMap actionmap=getActionMap();//Step 4. Create an object of type "action map" (instanciar) in spanish, to use it methods.
actionmap.put("green_background", actionGreen);//Step 5. Asign the object of the key combination to the action created before using the method put of the actionmap object.
actionmap.put("blue_background", actionBlue);
actionmap.put("red_background", actionRed);
actionmap.put("default_background", actionDefault);
actionmap.put("random_background", actionRandom);
}
//Create the class that asign the constructor methods to the buttons.
private class ActionColor extends AbstractAction{
//Constructor method 1.
public ActionColor(String nam, Icon icon, Color colorButton, String des) {
putValue(Action.NAME, nam);
putValue(Action.SMALL_ICON, icon);
putValue(Action.SHORT_DESCRIPTION, des);
putValue("background color", colorButton);
}
//Constructor method 2.
public ActionColor(String nam, Color colorButton, String des) {
putValue(Action.NAME, nam);
putValue(Action.SHORT_DESCRIPTION, des);
putValue("background color", colorButton);
}
//The listener class. It recive the instructions.
public void actionPerformed(ActionEvent e) {
Color c=(Color) getValue("background color");
setBackground(c);
}
}
private class ActionRandom extends AbstractAction{
public ActionRandom(String nam, Color colorButton, String des) {
putValue(Action.NAME, nam);
putValue(Action.SHORT_DESCRIPTION, des);
putValue("background color", colorButton);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int extra=10;
int valorDado1=(int) ((Math.random()*256)+extra);
int valorDado2=(int) ((Math.random()*256)+extra);
int valorDado3=(int) ((Math.random()*256)+extra);
if(valorDado1>255) {
valorDado1=valorDado1-extra;
}
if(valorDado2>255) {
valorDado2=valorDado2-extra;
}
if(valorDado3>255) {
valorDado3=valorDado3-extra;
}
Color randomcolor=new Color(valorDado1, valorDado2, valorDado3);
/*System.out.println(valorDado1);
System.out.println(valorDado2);
System.out.println(valorDado3);
System.out.println();*/
setBackground(randomcolor);
}
}
}