开关上的绳子/执行按钮动作 [英] Switching on a string/implementing button actions

查看:193
本文介绍了开关上的绳子/执行按钮动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部免责声明:我是一个CS的学生,这个问题涉及到面向对象的编程最近分配的Java程序。虽然我们已经做了一些东西控制台,这是我们第一次使用GUI和Swing或AWT的工作。我们得到了创建的窗口有一些文本和一个按钮,通过不同颜色的文本旋转,一些code。然后,我们被要求修改程序来创建代替,这也是为了给我们实践研究的API颜色单选按钮。我已经交给我的任务,并从我的导师获得了许可,张贴在这里我的code。

什么是Java实现按钮动作的最好方法?一些摸索之后,我创建这样的按钮:

 类HelloComponent3扩展JComponent的
    实现的MouseMotionListener,的ActionListener
{
    INT messageX = 75,messageY = 175;    串theMessage;
    字符串redString =红,blueString =蓝色,greenString =绿色;
    字符串magentaString =品红,blackString =黑,resetString =复位;    JButton的resetButton;
    一个JRadioButton redButton,blueButton,greenButton,magentaButton,blackButton;
    ButtonGroup中colorButtons;    公共HelloComponent3(字符串消息){    theMessage =消息;    // intialize复位按钮
    resetButton =的新的JButton(重置);
    resetButton.setActionCommand(resetString);
    resetButton.addActionListener(本);    // intialize我们的单选按钮,用行动和标签
    redButton =新一个JRadioButton(红楼梦);
    redButton.setActionCommand(redString);
    ...

和添加动作监听器...

  redButton.addActionListener(本);
blueButton.addActionListener(本);
...

一个存根已经为actionPerformed方法给我们如何使用它的想法创建的,但由于在模板中,只有一个按钮,尚不清楚如何实现多个按钮。我试图在一个字符串转换,但很快就意识到,既然字符串不是原始的类型,我不能用它switch语句。我可以用的if-else链已经即兴的,但是这是我想出了替代。它距离优雅似乎远,而且必须有一个更好的办法。如果有,是什么呢?有没有办法对字符串进行切换?或者选择在一个更可扩展的方式操作?

 公共无效的actionPerformed(ActionEvent的五){    如果(e.getActionCommand()。等于(resetString)){
    messageX = 75; messageY = 175;
    setForeground(Color.black);
    blackButton.setSelected(真);
    重绘();
    返回;
    }    如果(e.getActionCommand()。等于(redString)){
    setForeground(Color.red);
    重绘();
    返回;
    }    如果(e.getActionCommand()。等于(blueString)){
    setForeground(Color.blue);
    重绘();
    返回;
    }    如果(e.getActionCommand()。等于(greenString)){
    setForeground(Color.green);
    重绘();
    返回;
    }    如果(e.getActionCommand()。等于(magentaString)){
    setForeground(Color.magenta);
    重绘();
    返回;
    }    如果(e.getActionCommand()。等于(blackString)){
    setForeground(Color.black);
    重绘();
    返回;
    }
}


解决方案

而不是写这个的:

  resetButton.addActionListener(本);

您也可以这样写:

  resetButton.addActionListener(新的ActionListener(){
    公共无效的actionPerformed(ActionEvent的EVT){
        resetButtonActionPerformed(EVT);
    }
});

和,而不是对所有的行动谱写一个大的actionPerformed(),您可以(然后还要)写:

 公共无效resetButtonActionPerformed(ActionEvent的EVT){
    messageX = 75; messageY = 175;
    setForeground(Color.black);
    blackButton.setSelected(真);
    重绘();
}

我不知道这是最优雅的解决方案,但至少你不再有那么大,如果结构。

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;
    }
}

解决方案

Instead of writing this:

resetButton.addActionListener(this);

You could also write this:

resetButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        resetButtonActionPerformed(evt);
    }
});

And instead of writing one big actionPerformed() for all actions, you can (and then have to) write this:

public void resetButtonActionPerformed(ActionEvent evt) {
    messageX = 75; messageY = 175;
    setForeground(Color.black);
    blackButton.setSelected(true);
    repaint();
}

I don't know if this is the most elegant solution, but at least you no longer have that big if construct.

这篇关于开关上的绳子/执行按钮动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆