-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJFrameYesNoButtons.java
More file actions
49 lines (41 loc) · 1.28 KB
/
Copy pathJFrameYesNoButtons.java
File metadata and controls
49 lines (41 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import javax.swing.*;
import java.awt.event.*;
public class ButtonFrame extends JFrame
{
private JLabel label;
private JButton yesButton;
private JButton noButton;
public ButtonFrame()
{
// Set the title and layout of the frame
setTitle("Button Frame");
setLayout(new FlowLayout());
// Create the label
label = new JLabel("");
add(label);
// Create the buttons and add action listeners
yesButton = new JButton("Yes");
yesButton.addActionListener(new ButtonListener());
add(yesButton);
noButton = new JButton("No");
noButton.addActionListener(new ButtonListener());
add(noButton);
// Set the size and location of the frame
setSize(200, 100);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// Set the label's text to the text of the button being pressed
label.setText(((JButton)e.getSource()).getText());
}
}
public static void main(String[] args)
{
ButtonFrame frame = new ButtonFrame();
frame.setVisible(true);
}
}