-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path60_CheckBox
More file actions
55 lines (43 loc) · 1.31 KB
/
60_CheckBox
File metadata and controls
55 lines (43 loc) · 1.31 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
50
51
52
53
54
55
// JCheckBox = A GUI component that can be selected or deselected
<Main.java>
public class Main{
public static void main(String[] args){
new myFrame();
}
}
<myFrame.java>
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class myFrame extends JFrame implements ActionListener {
JButton button;
JCheckBox checkBox;
ImageIcon xIcon;
ImageIcon checkIcon;
myFrame(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
xIcon = new ImageIcon("cross.png");
checkIcon = new ImageIcon("check.png");
button = new JButton();
button.setText("Submit");
button.addActionListener(this);
checkBox = new JCheckBox();
checkBox.setText("I'm not a robot");
checkBox.setFocusable(false); //get rid of textbox
checkBox.setFont(new Font("Consolas",Font.PLAIN, 35));
checkBox.setIcon(xIcon);
checkBox.setSelectedIcon(checkIcon);
this.add(button);
this.add(checkBox);
this.pack();
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button){
System.out.println(checkBox.isSelected());
}
}
}