ActionListener在Java中的实现

ActionListener在Java中的实现,java,swing,actionlistener,jtextfield,Java,Swing,Actionlistener,Jtextfield,你能帮我在代码中正确使用ActionListener吗?代码可以编译,GUI可以正确显示,但是没有按钮可以工作!!如果要测试代码,请注意,需要将图像与创建的项目文件放在同一文件夹中,并根据照片的名称更改行“ImageIcon myImageIcon=new ImageIcon(“rodeo.jpg”);” import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ImageApplication

你能帮我在代码中正确使用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);
}
}
使用
String#equals
比较
String
内容。您正在使用比较对象引用的
=
操作符

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

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

移动代码后:

应该是

myImageLabel = new JLabel(myImageIcon, JLabel.CENTER);
尝试以下方法: 功能:单击要执行的方法时,它会添加到按钮本身:

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.

这些实例变量:

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

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

使用以下代码获取图像


ImageIcon myImageIcon=newImageIcon(“rodeo.jpg”).getImage()

谢谢你的快速回答,但这并没有解决问题。当按下按钮时,我在ImageApplication.actionPerformed(ImageApplication.java:129)“检查行”myMenuItem1.addActionListener(ia);“ia是否有问题?”?此时我无法使用“this”,所以我使用了类应用程序的对象…您需要将
main
方法中实例化的所有组件移动到实例方法中,并删除
JLabel
本地类声明。最好是实例化类成员或实例变量。执行此操作的最佳位置是在实例方法中。许多Swing应用程序都有一个
initComponents
实例方法,例如Ok,但是如何调用实例方法呢?如果我在static main中只有ia对象创建和setvisible方法???
setBounds
有什么用?我在OP的代码里什么地方都没看到?考虑到他们不知道字符串比较,我怀疑开始向他们展示这一点是不是一个好主意
txtWidth = new JTextField(Integer.toString(origWidth));
txtHeight = new JTextField(Integer.toString(origHeight));