打开字符串/执行按钮操作
完整免责声明:我是一名计算机科学学生,这个问题与最近分配的面向对象编程的 Java 程序有关。 尽管我们已经完成了一些控制台方面的工作,但这是我们第一次使用 GUI 和 Swing 或 Awt。 我们得到了一些代码,这些代码创建了一个带有一些文本的窗口和一个可以旋转文本不同颜色的按钮。 然后我们被要求修改程序来创建颜色的单选按钮 - 这也是为了让我们练习研究 API。 我已经提交了作业并获得了导师的许可,可以在此处发布我的代码。
在 Java 中实现按钮操作的最佳方法是什么? 经过一番摆弄后,我创建了如下按钮:
class HelloComponent3 extends JComponent
implements MouseMotionListener, ActionListener
{
int messageX = 75, messageY= 175;
String theMessage;
String redString = "red", blueString = "blue", greenString = "green";
String magentaString = "magenta", blackString = "black", resetString = "reset";
JButton resetButton;
JRadioButton redButton, blueButton, greenButton, magentaButton, blackButton;
ButtonGroup colorButtons;
public HelloComponent3(String message) {
theMessage = message;
//intialize the reset button
resetButton = new JButton("Reset");
resetButton.setActionCommand(resetString);
resetButton.addActionListener(this);
//intialize our radio buttons with actions and labels
redButton = new JRadioButton("Red");
redButton.setActionCommand(redString);
...
并添加了操作侦听器...
redButton.addActionListener(this);
blueButton.addActionListener(this);
...
已经为 actionPerformed 方法创建了一个存根,让我们了解如何使用它,但由于模板中只有一个按钮,尚不清楚如何实现多个按钮。 我尝试切换字符串,但很快意识到,由于字符串不是原始类型,因此我无法将它用于 switch 语句。 我本可以即兴创作一个 if-else 链,但这是我想出来的。 这看起来很不优雅,一定有更好的方法。 如果有,那是什么? 有没有办法打开字符串? 或者以更具规模化的方式选择行动?
public void actionPerformed(ActionEvent e){
if (e.getActionCommand().equals(resetString)) {
messageX = 75; messageY = 175;
setForeground(Color.black);
blackButton.setSelected(true);
repaint();
return;
}
if ( e.getActionCommand().equals(redString) ) {
setForeground(Color.red);
repaint();
return;
}
if ( e.getActionCommand().equals(blueString) ) {
setForeground(Color.blue);
repaint();
return;
}
if ( e.getActionCommand().equals(greenString) ) {
setForeground(Color.green);
repaint();
return;
}
if ( e.getActionCommand().equals(magentaString) ) {
setForeground(Color.magenta);
repaint();
return;
}
if ( e.getActionCommand().equals(blackString) ) {
setForeground(Color.black);
repaint();
return;
}
}
Full disclaimer: I'm a CS student, and this question is related to a recently assigned Java program for Object-Oriented Programming. Although we've done some console stuff, this is the first time we've worked with a GUI and Swing or Awt. We were given some code that created a window with some text and a button that rotated through different colors for the text. We were then asked to modify the program to create radio buttons for the colors instead—this was also intended to give us practice researching an API. I've already handed in my assignment and received permission from my instructor to post my code here.
What's the best way to implement button actions in Java? After some fiddling around, I created the buttons like this:
class HelloComponent3 extends JComponent
implements MouseMotionListener, ActionListener
{
int messageX = 75, messageY= 175;
String theMessage;
String redString = "red", blueString = "blue", greenString = "green";
String magentaString = "magenta", blackString = "black", resetString = "reset";
JButton resetButton;
JRadioButton redButton, blueButton, greenButton, magentaButton, blackButton;
ButtonGroup colorButtons;
public HelloComponent3(String message) {
theMessage = message;
//intialize the reset button
resetButton = new JButton("Reset");
resetButton.setActionCommand(resetString);
resetButton.addActionListener(this);
//intialize our radio buttons with actions and labels
redButton = new JRadioButton("Red");
redButton.setActionCommand(redString);
...
And added action listeners...
redButton.addActionListener(this);
blueButton.addActionListener(this);
...
A stub was already created for the actionPerformed method to give us an idea on how to use it, but since there was only a single button in the template, it wasn't clear how to implement multiple buttons. I tried switching on a String, but quickly realized that, since a String isn't a primitive type, I couldn't use it for a switch statement. I could have improvised with an if-else chain, but this is what I came up with instead. It seems far from elegant, and there must be a better way. If there is, what is it? Is there a way to switch on a string? Or choose an action in a more scaleable fashion?
public void actionPerformed(ActionEvent e){
if (e.getActionCommand().equals(resetString)) {
messageX = 75; messageY = 175;
setForeground(Color.black);
blackButton.setSelected(true);
repaint();
return;
}
if ( e.getActionCommand().equals(redString) ) {
setForeground(Color.red);
repaint();
return;
}
if ( e.getActionCommand().equals(blueString) ) {
setForeground(Color.blue);
repaint();
return;
}
if ( e.getActionCommand().equals(greenString) ) {
setForeground(Color.green);
repaint();
return;
}
if ( e.getActionCommand().equals(magentaString) ) {
setForeground(Color.magenta);
repaint();
return;
}
if ( e.getActionCommand().equals(blackString) ) {
setForeground(Color.black);
repaint();
return;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这样写:
您也可以
您可以(然后必须)这样写:
我不知道这是否是最优雅的解决方案,但不是 为所有操作编写一个大的 actionPerformed()至少你不再有那么大的 if 结构。
Instead of writing this:
You could also write this:
And instead of writing one big actionPerformed() for all actions, you can (and then have to) write this:
I don't know if this is the most elegant solution, but at least you no longer have that big if construct.
两种替代方法:
Two alternate approaches:
一种足够好的方法是声明一个 其元素与您的字符串匹配的枚举 并打开 valueOf (str)(链接的示例展示了如何以相当的安全性做到这一点)。
避免匿名内部类的原因可能是因为该类还没有该构造,尽管这可能是最好的解决方案。
One decent enough approach is to declare an enum whose elements match your strings and switch on valueOf(str) (the linked example shows how to do this with a fair amount of safety).
The reason to avoid anonymous inner classes is probably because the class hasn't had that construct (yet), even though that might be the best solution.
正如已经建议的,您可以使用匿名内部类来实现 ActionListener 接口。 作为替代方案,您不必使用匿名内部类,但您可以使用一个简单的嵌套类来代替:
然后...
为什么这种方法 - 或任何涉及内部类的方法 - 比在外部实现 ActionListener 更好class 请参阅“设计模式”:
“优先考虑‘对象组合’而不是‘类继承’。” (Gang of Four 1995:20)
在匿名内部类和这些命名内部类之间进行选择很大程度上是风格问题,但我认为这个版本更容易理解,并且在有很多操作时更清晰。
As suggested already, you can use anonymous inner classes to implement the ActionListener interface. As an alternative, you don't have to use anonymous inner classes, but you can use a simple nested class instead:
and then...
For why this approach - or any approach involving inner classes - is better than implementing ActionListener in the outer class see "Design Patterns":
"Favor 'object composition' over 'class inheritance'." (Gang of Four 1995:20)
Choosing between anonymous inner classes and these named inner classes is a largely a matter of style, but I think this version is easier to understand, and clearer when there are lots of actions.
呃。 不要在一个大型类中实现大量不相关的接口。 相反,使用匿名内部类。 它们有点冗长,但正是您想要的。 每个事件使用一个,那么你就不需要大的 if-else 链。 我建议在内部类中保留足够的代码来解码事件并调用对目标对象有意义的方法。 此外,您可以参数化您的内部类。 您可能会发现不需要保留对周围实际小部件的引用。
在您的示例中,您似乎使用 JComponent 作为 JPanel。 没有太大区别,但使用 JPanel 来收集小部件块。 此外,不太可能需要对其进行子类化,所以不需要。
例如:
Ergh. Don't implement masses of unrelated interfaces in one mega class. Instead, use anoymous inner classes. They are a bit verbose, but are what you want. Use one for each event, then you wont need big if-else chain. I suggest keeping enough code within the inner class to decode the event and call methods that make sense to the target objects. Further, you can parameterise your inner classes. You will probably find you don't need to keep references to the actual widgets around.
In your example you seem to be using a JComponent as a JPanel. There's not much difference, but use JPanel for collecting a block of widgets. Further there is unlikely any need to subclass it, so don't.
So for instance: