如何编写代码以显示每个JPanel中的每个字母,以及如何旋转. (JFrame,NetBeans) [英] How do i write a code to display every letter in every JPanel, and how do I rotate. (JFrame, NetBeans)
问题描述
我是Java的新手,我在课堂上被分配来开发以下问题的代码,我只能进行设计,此后,我不知道如何继续向每个对象添加动作.按钮.
I'm new to Java, and I was assigned in my class to develop a code for the following question, I was only able to do the design, after that I didn't know how to continue adding the actions to every button.
这是问题:
If anyone has ever solved it, please share it. Thanks for help in advance!
推荐答案
由于这是家庭作业,因此我没有提供完整的代码.我将提供摘要.
Since this is homework, I'm not providing the entire code. I will provide snippets.
这是我创建的GUI.我希望可以将其显示为动画GIF.
Here's the GUI I created. I wish I could show it as an animated GIF.
我添加了一个停止按钮来停止单词旋转.
I added a stop button to stop the word rotation.
我通过将问题分解为越来越小的步骤来编写代码,然后对每个步骤进行编码.在完成GUI之前,我对GUI进行了许多测试.有些测试失败了,我不得不修改代码.
I wrote the code by breaking the problem down into smaller and smaller steps, then coding each of the steps. I ran many tests of the GUI before I finished it. Some of the tests failed, and I had to revise the code.
我写了6堂课.主类在底部创建了JFrame
,字母面板组和控制面板.我编写了一个LetterPanel
类来创建一个字母面板.我编写了3个actionListener
类,一个用于JComboBox
,一个用于旋转按钮,一个用于停止按钮.我写了一个Animation
类,该类每秒旋转一次字母.
I wrote 6 classes. The main class created the JFrame
, the letter panel group, and the control panel on the bottom. I wrote a LetterPanel
class to create one letter panel. I wrote 3 actionListener
classes, one for the JComboBox
, one for the rotate button, and one for the stop button. I wrote an Animation
class that rotates the letters every second.
这是我用来获得4种绿色阴影的颜色.
Here are the colors I used to get the 4 shades of green.
Color[] colors = { new Color(50, 117, 1),
new Color(65, 159, 0), new Color(88, 201, 5),
new Color(107, 242, 2)
};
设置主JPanel
来容纳4个LetterPanel
对象有点棘手.这是我的方法.
Setting up the main JPanel
to hold the 4 LetterPanel
objects was a bit tricky. Here's how I did it.
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
LetterPanel
类扩展了JPanel
,并覆盖了paintComponent
方法.首先,我调用了super.paintComponent
方法.始终首先调用super.paintComponent
方法.然后,我绘制了背景色.然后,我画了封信.
The LetterPanel
class extended a JPanel
and overrode the paintComponent
method. First, I called the super.paintComponent
method. Always call the super.paintComponent
method first. Then, I painted the background color. Then, I painted the letter.
要在每个LetterPanel
中绘制字母,我使用了以下代码.
To paint the letter in each LetterPanel
, I used the following code.
/**
* Draw a String centered in the middle of the panel.
*
* @param g2d The Graphics2D instance.
* @param text The String to draw.
* @param font The Font to draw with.
*/
public void drawCenteredString(Graphics2D g2d,
String text, Font font) {
FontMetrics metrics = g2d.getFontMetrics(font);
int x = (getWidth() - metrics.stringWidth(text)) / 2;
int y = ((getHeight() - metrics.getHeight()) / 2) +
metrics.getAscent();
g2d.setFont(font);
g2d.drawString(text, x, y);
}
JComboBox actionListener从JComboBox获取选定的单词. Oracle教程如何使用组合框,告诉您正是我设置JComboBox这个词的方式.
The JComboBox actionListener gets the selected word from the JComboBox. The Oracle tutorial, How to Use Combo Boxes, tells you exactly how I set up the word JComboBox.
旋转按钮actionListener
检查是否同时选中了两个JCheckBox
字段.然后,它检查是否未选中JCheckBox
字段.最后,它启动一个Animation
线程.
The rotate button actionListener
checks if both JCheckBox
fields are checked. Then it checks if neither JCheckBox
field is checked. Finally, it starts an Animation
thread.
停止"按钮停止Animation
线程.
Animation
线程旋转单词并暂停1秒钟,以便您查看旋转情况.
The Animation
thread rotates the word and pauses 1 second to allow you to see the rotation.
这是运行循环.
@Override
public void run() {
while (running) {
updatePanel();
sleep(1000L);
if (leftSelected) {
word = rotateLeft(word);
} else {
word = rotateRight(word);
}
}
}
这是我的轮换方法.
private String rotateLeft(String word) {
return word.substring(1) + word.substring(0, 1);
}
private String rotateRight(String word) {
return word.substring(word.length() - 1) +
word.substring(0, word.length() - 1);
}
这篇关于如何编写代码以显示每个JPanel中的每个字母,以及如何旋转. (JFrame,NetBeans)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!