Note that there are some explanatory texts on larger screens.

plurals
  1. POActionListener implementation in Java
    text
    copied!<p>Could you please help me use the ActionListener correclty in my code? The code compiles and the GUI is displayed correctly, but no button works!! If you want to test the code, note that you need to put the image in the same folder as the project file created and change the line "ImageIcon myImageIcon = new ImageIcon("rodeo.jpg");" according to the name of your photo.</p> <pre><code>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); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload