Knowee
Questions
Features
Study Tools

Develop a Java program that creates a GUI application with a button. Whenthe button is clicked, it should change the text of a label.

Question

Develop a Java program that creates a GUI application with a button. Whenthe button is clicked, it should change the text of a label.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple Java program using Swing to create a GUI application with a button. When the button is clicked, it changes the text of a label.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GUIApplication {
    public static void main(String[] args) {
        JFrame frame = new JFrame("GUI Application");
        Container contentPane = frame.getContentPane();
        JLabel label = new JLabel("Click the button");
        JButton button = new JButton("Click me");

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                label.setText("Button clicked");
            }
        });

        contentPane.setLayout(new FlowLayout());
        contentPane.add(label);
        contentPane.add(button);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        frame.setVisible(true);
    }
}

Here's a step-by-step explanation of the code:

  1. Import the necessary packages for the Swing components and event handling.

  2. Define a public class GUIApplication.

  3. Inside the main method, create a JFrame which is the main window of the application.

  4. Get the content pane of the frame, which is where we'll add our components.

  5. Create a JLabel and a JButton with initial texts.

  6. Add an ActionListener to the button. Inside the actionPerformed method (which is called when the button is clicked), change the text of the label.

  7. Set the layout manager of the content pane to FlowLayout, which arranges components in a left-to-right flow.

  8. Add the label and button to the content pane.

  9. Set the default close operation of the frame to EXIT_ON_CLOSE, which means the application will be closed when the user closes the frame.

  10. Set the size of the frame and make it visible.

When you run this program, it will create a window with a label and a button. When you click the button, the text of the label changes to "Button clicked".

This problem has been solved

Similar Questions

Which class is used to display text in Java GUI?1 pointJPanelJButtonJLabelJFrame

Customization in GUI DevelopmentExplore the concept of customizing GUI components in Java applications. Discuss various techniques for customizing Swing, SWT, and JFace components, including subclassing, property modification, and styling. Provide examples of customized components and explain their significance in creating tailored user interfaces.

Which class is used to create a window in Java GUI?1 pointJPanelJLabelJButtonJFrame

Which class is used to create a container in Java GUI?1 pointJButtonJFrameJLabelJPanel

Which JavaFX class is used to display text and images?Question 5Answera.Textb.Labelc.Buttond.Image

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.