Question: Can you help me modify my program so that when I press enter key it has the same effect as I click the buttons? Thanks

Can you help me modify my program so that when I press enter key it has the same effect as I click the buttons? Thanks for your help!

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Panel02 extends JPanel

{

private JButton northButton, southButton, westButton, eastButton, centerButton;

private ImageIcon picture1, picture2;

private JTextField southText;

public Panel02()

{

setLayout(new BorderLayout());

northButton = new JButton(" First day of learning Java (maybe other languages) ");

add(northButton, BorderLayout.NORTH);

northButton.setForeground(Color.black);

northButton.setBackground(Color.red);

northButton.setFont(new Font("Helvetica", Font.ITALIC, 24));

ActionListener listener1 = new MyActionListener();

northButton.addActionListener(listener1);

southButton = new JButton("Luckily, it finally works correctly, but you do not understand what bugs did you make. (to be continue)");

add(southButton, BorderLayout.SOUTH);

southButton.setForeground(Color.gray);

southButton.setBackground(Color.orange.brighter());

southButton.setFont(new Font("Serif", Font.BOLD, 24));

ActionListener listener2 = new MyActionListener();

southButton.addActionListener(listener2);

westButton = new JButton("Your program does not work as expected.");

add(westButton, BorderLayout.WEST);

westButton.setForeground(Color.yellow);

westButton.setBackground(Color.blue);

westButton.setFont(new Font("TimesRoman", Font.PLAIN, 36));

ActionListener listener3 = new MyActionListener();

westButton.addActionListener(listener3);

eastButton = new JButton("You check your code mutiple times...");

add(eastButton, BorderLayout.EAST);

eastButton.setForeground(Color.pink);

eastButton.setBackground(Color.magenta.darker());

eastButton.setFont(new Font("TimesRoman", Font.BOLD, 36));

ActionListener listener4 = new MyActionListener();

eastButton.addActionListener(listener4);

picture1 = new ImageIcon("a.jpg");

centerButton = new JButton(picture1);

add(centerButton, BorderLayout.CENTER);

ActionListener listener5 = new MyActionListener();

centerButton.addActionListener(listener5);

southText = new JTextField(240);

southButton.add(southText);

ActionListener listener6 = new MyActionListener();

southText.addActionListener(listener6);

}

class MyActionListener implements ActionListener

{

public MyActionListener()

{

}

public void actionPerformed(ActionEvent event)

{

if(event.getSource() == northButton ||event.getSource() == southButton || event.getSource() == westButton || event.getSource() == eastButton ||event.getSource() == centerButton)

{

northButton.setText("A year later");

westButton.setText("Thanks for teacher's help");

eastButton.setText("As well as your hardwork");

southButton.setText("Now, you can make prefect programs!");

picture2 = new ImageIcon("b.jpg");

northButton.setBackground(Color.white);

westButton.setBackground(Color.white);

eastButton.setBackground(Color.white);

centerButton = new JButton(picture2);

add(centerButton, BorderLayout.CENTER);

}

}

}

}

class Driver02

{

public static void main(String[] args)

{

JFrame frame = new JFrame("GUI mini-program #2");

frame.setSize(400, 350);

frame.setLocation(200, 200);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setContentPane(new Panel02());

frame.setVisible(true);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!