0

你能帮我在我的代码中正确使用 ActionListener 吗?代码编译,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

4 回答 4

4

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

但是,由于按钮具有不同的功能,最好每个按钮都有一个单独的ActionListener. 这可以使用匿名ActionListener实例来完成。

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

移动代码后:

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

应该

myImageLabel = new JLabel(myImageIcon, JLabel.CENTER);
于 2013-04-23T00:05:20.353 回答
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.
于 2013-04-23T00:14:43.040 回答
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));

对于您的其他实例变量也是如此。

于 2013-04-23T00:14:43.790 回答
0

使用以下代码获取图像

ImageIcon myImageIcon = new ImageIcon("rodeo.jpg").getImage();

于 2014-07-10T12:04:31.247 回答