Design a frame in java with three buttons named RED, BLUE and GREEN. Write a code such that when RED button is clicked, text red is displayed and so on for other two

From Events, Handling Events and AWT/Swing chapter in PU/ Programming in Java

Asked on 21 Dec, 2022

Like 0 Dislike 211 Views

Add a comment

5 Answers

Anonymous on 6 Jun, 2023 Like 0 Dislike

Jie Yu said, making Gu Min Alpha Xr Shark Tank male enhancement pills kuwait a little unclear does priligy work

Add a comment

Anonymous on 30 May, 2023 Like 0 Dislike

t comfortable with all the attention levitra definition We tested the hypothesis that fructose directly stimulates TAL dependent NaCl reabsorption by enhancing NKCC2 activity

Add a comment

Anonymous on 24 Apr, 2023 Like 0 Dislike

levitra economico com 20 E2 AD 90 20Generic 20Viagra 20Injection 20 20Jual 20Viagra 20Di 20Pekanbaru jual viagra di pekanbaru Customers at the 30 Costa Coffee outlets in Abu Dhabi, Dubai and Ras Al Khaimah can now ask baristas to use camel milk in their coffees

Add a comment

Anonymous on 23 Dec, 2022 Like 0 Dislike

123456

Add a comment

arjun adhikari on 21 Dec, 2022 Like 0 Dislike

Here is an example of how you can create a frame with three buttons in Java and display text when the buttons are clicked:

 

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

 

public class ButtonFrame extends JFrame {
  private JButton redButton;
  private JButton blueButton;
  private JButton greenButton;
  private JLabel label;

 

  public ButtonFrame() {
    // Set the frame title and layout
    setTitle("Button Test");
    setLayout(new FlowLayout());

 

    // Create the buttons
    redButton = new JButton("RED");
    blueButton = new JButton("BLUE");
    greenButton = new JButton("GREEN");

 

    // Add action listeners to the buttons
    redButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        label.setText("RED");
      }
    });
    blueButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        label.setText("BLUE");
      }
    });
    greenButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        label.setText("GREEN");
      }
    });

 

    // Create the label
    label = new JLabel("Press a button");

 

    // Add the buttons and label to the frame
    add(redButton);
    add(blueButton);
    add(greenButton);
    add(label);
  }
}

To run this code, you will need to create an instance of ButtonFrame and make it visible. Here is an example of how to do that:

ButtonFrame frame = new ButtonFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 100); frame.setVisible(true);

Add a comment


Add your answer