From Events, Handling Events and AWT/Swing chapter in PU/ Programming in Java
Asked on 21 Dec, 2022
0
211 Views
Jie Yu said, making Gu Min Alpha Xr Shark Tank male enhancement pills kuwait a little unclear does priligy work
t comfortable with all the attention levitra definition We tested the hypothesis that fructose directly stimulates TAL dependent NaCl reabsorption by enhancing NKCC2 activity
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
123456
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 your answer