Java中的ActionListener实现

时间:2013-04-23 00:03:18

标签: java swing actionlistener jtextfield

你能帮我在我的代码中使用ActionListener correclty吗?代码编译并且GUI正确显示,但没有按钮工作!如果要测试代码,请注意您需要将图像放在与创建的项目文件相同的文件夹中,并更改“ImageIcon myImageIcon = new ImageIcon(”rodeo.jpg“);”根据你的照片名称。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ImageApplication extends JFrame implements ActionListener
{
public Image myImage;
public JLabel myImageLabel;
public ImageIcon myImageIcon;
public JFrame frame;
public JTextField txtWidth, txtHeight;
public int origWidth, origHeight;


public static void main(String[] args)
{
    int origWidth, origHeight;
    ImageApplication ia = new ImageApplication();
    ia.setVisible(true);
    JFrame frame = new JFrame();
    ImageIcon myImageIcon = new ImageIcon("rodeo.jpg");
    JLabel myImageLabel = new JLabel(myImageIcon, JLabel.CENTER);
    Image myImage = myImageIcon.getImage();


    origWidth = myImageIcon.getIconWidth();
    origHeight = myImageIcon.getIconHeight();

    JMenuBar myMenuBar = new JMenuBar();
    JMenu myMenu = new JMenu("Options");
    JMenuItem myMenuItem1 = new JMenuItem("Double");
    JMenuItem myMenuItem2 = new JMenuItem("Reset");
    myMenu.add(myMenuItem1);
    myMenu.add(myMenuItem2);
    myMenuBar.add(myMenu);
    ia.setJMenuBar(myMenuBar);

    JButton bAL = new JButton("Align Left");
    JButton bAC = new JButton("Align Center");
    JButton bAR = new JButton("Align Right");
    JButton bResize = new JButton ("Resize");
    bAL.setFocusPainted(false);
    bAC.setFocusPainted(false);
    bAR.setFocusPainted(false);
    bResize.setFocusPainted(false);

    JLabel lWidth = new JLabel("Width:");
    JLabel lHeight = new JLabel("Height:");
    JTextField txtWidth = new JTextField(Integer.toString(origWidth));
    JTextField txtHeight = new JTextField(Integer.toString(origHeight));

    JPanel GRID = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1f;
    c.weighty = 0f;

    c.gridx = 0;
    c.gridy = 0;
    GRID.add(bAL, c);
    c.gridx++;
    GRID.add(bAC, c);
    c.gridx++;
    GRID.add(bAR, c);
    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 3;
    GRID.add(myImageLabel, c);
    c.gridwidth = 1;

    c.gridx = 0;
    c.gridy = 2;
    GRID.add(lWidth, c);
    c.gridx++;
    c.gridwidth = 2;
    GRID.add(txtWidth, c);
    c.gridwidth = 1;

    c.gridx = 0;
    c.gridy = 3;
    GRID.add(lHeight, c);
    c.gridx++;
    c.gridwidth = 2;
    GRID.add(txtHeight, c);
    c.gridwidth = 1;

    c.gridx = 0;
    c.gridy = 4;
    GRID.add(bResize, c);

    ia.add(GRID, BorderLayout.CENTER);
    ia.setSize(origWidth + 150, origHeight + 150);

    myMenuItem1.addActionListener(ia);
    myMenuItem1.setActionCommand("double");
    myMenuItem2.addActionListener(ia);
    myMenuItem2.setActionCommand("reset");
    bAL.addActionListener(ia);
    bAL.setActionCommand("left");
    bAC.addActionListener(ia);
    bAC.setActionCommand("center");
    bAR.addActionListener(ia);
    bAR.setActionCommand("right");
    bResize.addActionListener(ia);
    bResize.setActionCommand("resize");
}



private void ResizeImage(int Width, int Height)
{
    myImage = myImage.getScaledInstance(Width, Height, Image.SCALE_SMOOTH);
    myImageIcon.setImage(myImage);
    myImageLabel.setIcon(myImageIcon);

    txtWidth.setText(Integer.toString(Width));
    txtHeight.setText(Integer.toString(Height));

    setSize(Width + 150, Height + 150);
}

public void actionPerformed(ActionEvent e)
{
    String command = e.getActionCommand();

    if(command == "left") myImageLabel.setHorizontalAlignment(JLabel.LEFT);
    else if(command == "center") myImageLabel.setHorizontalAlignment(JLabel.CENTER);
    else if(command == "right") myImageLabel.setHorizontalAlignment(JLabel.RIGHT);
    else if(command == "resize") ResizeImage(Integer.parseInt(txtWidth.getText()),       
    Integer.parseInt(txtHeight.getText()));
    else if(command == "double") ResizeImage(myImageIcon.getIconWidth() * 2,  
    myImageIcon.getIconHeight() * 2);
    else if(command == "reset") ResizeImage(origWidth, origHeight);
}
}

4 个答案:

答案 0 :(得分:4)

使用String#equals来比较String内容。您正在使用比较对象引用的==运算符。

但是,由于按钮具有不同的功能,因此每个按钮的个人ActionListener更好。这可以使用匿名ActionListener实例来完成。

方面问题:未分配类成员变量myImageLabel。而是另一个变量 在static main方法中初始化具有相同名称的名称。您需要将main方法中实例化的所有组件移动到实例方法中,并删除JLabel本地类声明。

移动代码后:

JLabel myImageLabel = new JLabel(myImageIcon, JLabel.CENTER);

应该是

myImageLabel = new JLabel(myImageIcon, JLabel.CENTER);

答案 1 :(得分:2)

试试这个方法: 它的作用:当点击要执行的方法时,它会添加到按钮本身:

buttonName.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                //do what ever
            }
        });
        //set bunds for the button itself, if not done otherwise, but for your layout.

答案 2 :(得分:1)

这些实例变量:

public JTextField txtWidth, txtHeight;

永远不会初始化,但会在您的侦听器代码中引用。您具有要实例化的同名的局部变量。改变这个:

JTextField txtWidth = new JTextField(Integer.toString(origWidth));
JTextField txtHeight = new JTextField(Integer.toString(origHeight));

到此:

txtWidth = new JTextField(Integer.toString(origWidth));
txtHeight = new JTextField(Integer.toString(origHeight));

和您的其他实例变量类似。

答案 3 :(得分:0)

使用以下代码获取图像

ImageIcon myImageIcon = new ImageIcon(“rodeo.jpg”)。getImage();