- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我最近才接触 Java,而且还是个菜鸟。我使用 for 循环创建了一个 9x9 的 JPanel 面板来添加按钮。如何创建一个 Action 监听器,让我可以选择不同的颜色,单击时将显示为 JButton 的背景颜色?我正在尝试制作一个微型像素艺术程序。
最佳答案
它看起来像这样。在此示例中,[row]
和 [col]
指网格的行值和列值。请务必创建计数器 (private static int counter = 0;
)。否则你会得到一堆错误。这是代码:
JBut[row][col].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
typeOfButton button = (typeOfButton) e.getSource();
int row = button.getRow();
int col = button.getCol();
counter++;
if (counter == 1) {
JBut[row][col].setBackground(Color.RED);
}
else if (counter == 2) {
JBut[row][col].setBackground(Color.ORANGE);
}
else if (counter == 3) {
JBut[row][col].setBackground(Color.YELLOW);
}
else if (counter == 4) {
JBut[row][col].setBackground(Color.GREEN);
}
else if (counter == 5){
JBut[row][col].setBackground(Color.BLUE);
}
else if (counter == 6){
JBut[row][col].setBackground(Color.MAGENTA);
}
else {
JBut[row][col].setBackground(Color.BLACK);
counter = 0; //makes color cycle repeat, starting with red
} //end else
} //end actionPerformed
}); //end ActionListener
显然,执行此操作的简单方法是将巨大的 if
语句放入名为 defineColor()
或类似方法的新方法中。
关于java - 使用 JButtons 的微型像素艺术程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43700319/
无论是谁读到这篇文章,我都感谢您花时间阅读。 本质上,我试图通过调用 x 个方法来生成这个精确的 ASCII 艺术,我个人考虑了 3 个: * * ** ** ***
这太琐碎了,听起来很傻,但请多多包涵。 我以前会画ASCII艺术笑脸similar to this post通过做类似的事情: Console.Write((char)1); 当我在 Windows
这样的图像转换算法是如何工作的? 我想将位图转换为 ASCII 艺术。谁能帮我看看我应该使用哪种算法? . W ,
我正在尝试为 android 制作游戏。我目前已将所有美术资源加载到 drawables 文件夹中,但我的问题是如何实际引用特定资源来渲染它? 我知道每个文件都有一个唯一的@id,我可能必须在onDr
我试图垂直反射(reflect)这门艺术。 示例: 但这是我最终得到的: 代码如下: String reverse; for(int i=1;i=0;j--) // probably the wors
我正在尝试使用 JavaScript 函数在我的网站上获取一些 ASCII 艺术作品,但结果并不是我现在想要的...... 它应该是这样的: 这是我用来尝试实现该目标的代码: function log
我正在尝试创建一个脚本,该脚本将接受用户输入(仅字母)并向用户输出 ASCII 艺术。两种输出方法是水平或垂直。我遇到的问题是如何水平打印字典值。 这是我到目前为止所拥有的: def convert(
下面的代码(用 patorjk.com Text to ASCII Art Generator 生成)给出了预期的结果(“TEST”ASCII 艺术文本): Windows:Firefox、Chrom
我正在尝试像这样打印 ascii 艺术: print(("""\ ._ o o
所以..这是(无论如何对我来说)这个程序最重要的功能。我需要这个才能工作。请不要笑..(好吧你可以笑)但是当我的程序出错时,我希望它显示这个: _ _,---._ ,
我正在尝试输入 ASCII art在 C++ 程序中,并通过手动打印每一行来实现,但结果与 ASCII 艺术完全不同。就像: 出现这种情况是因为提示无法识别字符还是我没有正确操作? 这是我正在尝试做的
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 11 年前。 Improve thi
我有一个很长的多行 ascii 艺术字符串,我想使用 Python curses 模块将其呈现给用户。我对此有点困惑,因为在 curses 中打印字符串的唯一方法是 addstr(y,x,string
目前我正在尝试读取可能包含 ASCII 艺术的 .txt 文件并使用 JQuery 在我的 HTML 文档中显示它们。简单的文本显示正确,但我有艺术格式问题。 我该如何解决这个问题? $(docume
我正在用 optparse 模块制作一个 shell 脚本,只是为了好玩,所以我想打印一个漂亮的 ascii 图来代替描述。 原来这段代码: parser = optparse.OptionParse
我是 python 的新手,把它作为一种兴趣爱好,通过一些搜索,我自己找到了一堆来自“计算实践”的练习,其中一个是关于写作的一个 ASCII 数字,如下所示。 这似乎是一个足够简单的练习,但我似乎无法
这个问题在这里已经有了答案: Render a string in HTML and preserve spaces and linebreaks (7 个答案) 关闭 9 个月前。 我该怎么做才能
我曾经为一项工作做过编程测试,其中涉及用 C# 制作 ASCii 艺术。我在这方面做得并不好,因为我对在 C#(或任何编程知识)中这样做的想法或经验知之甚少。 .NET 中是否有任何值得了解/练习的资
我正在编写一个小程序,想知道是否有办法在 R 中包含 ASCII 艺术。我在 python 中寻找等效的三个引号( """ 或 ''' )。 我尝试使用 cat或 print没有成功。 最佳答案 不幸
因此,对于我的 Java 入门类(class),我想用 ASCII 创建一个 Gingerbread 人。这是第一个作业,所以到目前为止,该类(class)仅涵盖了 println 语句。我在 OSX
我是一名优秀的程序员,十分优秀!