Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions