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.

When the button is clicked, it should change the text of a label.

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

Solution

Here’s a simple Java program that creates a GUI application with a button. When the button is clicked, it changes the text of a label.

Java GUI Program

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

public class SimpleGuiApp {
    public static void main(String[] args) {
        // Create a new frame
        JFrame frame = new JFrame("Simple GUI Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        
        // Create a label
        JLabel label = new JLabel("Initial Text");
        label.setHorizontalAlignment(SwingConstants.CENTER);
        
        // Create a button
        JButton button = new JButton("Click Me");
        
        // Add action listener to the button
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                label.setText("Text Changed!");
            }
        });

        // Add components to the frame
        frame.getContentPane().add(label, "North");
        frame.getContentPane().add(button, "South");
        
        // Set the frame visibility to true
        frame.setVisible(true);
    }
}

How to Run the Program

  1. Make sure you have Java Development Kit (JDK) installed on your computer.
  2. Copy the code into a text editor or IDE (like Eclipse or IntelliJ IDEA) and save it as SimpleGuiApp.java.
  3. Open a command prompt or terminal, navigate to the directory where the file is saved, and compile the program using:
    javac SimpleGuiApp.java
    
  4. Run the compiled program with:
    java SimpleGuiApp
    

Operation

When you run the program, a window will open displaying a button and a label. Clicking the button will change the text of the label from "Initial Text" to "Text Changed!"

This problem has been solved

Similar Questions

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

a set of extensible GUI Components to ease the developer's life to create JAVA based Front End/GUI Applications*1 pointSwing APISwingAPIJava Swing

Which of the following is NOT a component of a GUI-based application?a.Compilerb.TextFieldc.Buttond.ComboBox

Which Java library is commonly used for advanced graphical features in GUI programming?Question 5Answera.JDBCb.JavaFXc.Java AWTd.Swing

What is the main interface used for user input and output in Task 1?a.JTextAreab.JFramec.JTextFieldd.JLabel

1/1

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.