import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CheckBoxDemonstration extends JFrame implements ItemListener{
JLabel label = new JLabel ("What would you like to drink?");
JLabel information = new JLabel ("");
ButtonGroup bGroup = new ButtonGroup();
JCheckBox coffee = new JCheckBox("Coffee", false);
JCheckBox cola = new JCheckBox("Cola", false);
JCheckBox milk = new JCheckBox("Milk", false);
JCheckBox water = new JCheckBox("Water", false);
public CheckBoxDemonstration(){
super("Checkbox Demonstration");
setLayout(new FlowLayout());
label.setFont(new Font("Arial", Font.ITALIC, 22));
information.setFont(new Font("Arial", Font.ITALIC, 22));
coffee.addItemListener(this);
cola.addItemListener(this);
milk.addItemListener(this);
water.addItemListener(this);
bGroup.add(coffee);
bGroup.add(cola);
bGroup.add(milk);
bGroup.add(water);
add(label);
add(coffee);
add(cola);
add(milk);
add(water);
add(information);
}
public void itemStateChanged(ItemEvent e){
Object source = e.getItem();
if (source == coffee){
int select = e.getStateChange();
if(select == ItemEvent.SELECTED){
information.setText("Enoy your coffee!");
}
else{
information.setText("");
}
}
if (source == cola){
int select = e.getStateChange();
if(select == ItemEvent.SELECTED){
information.setText("Enoy your cola!");
}
else{
information.setText("");
}
}
if (source == milk){
int select = e.getStateChange();
if(select == ItemEvent.SELECTED){
information.setText("Enoy your milk!");
}
else{
information.setText("");
}
}
if (source == water){
int select = e.getStateChange();
if(select == ItemEvent.SELECTED){
information.setText("Enoy your water!");
}
else{
information.setText("");
}
}
}
public static void main (String[] args) {
final int FRAME_WIDTH = 350;
final int FRAME_HEIGHT = 150;
CheckBoxDemonstration frame = new CheckBoxDemonstration();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setVisible(true);
}
}
What I have tried:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Shiro_Anime extends JFrame implements ItemListener{
JLabel label = new JLabel ("PICK THE ANIME YOU LIKE");
JLabel information = new JLabel ("\n");
ButtonGroup bGroup = new ButtonGroup();
JCheckBox BlackBullet = new JCheckBox("Black Bullet", false);
JCheckBox HighSchoolDxD = new JCheckBox("High School DxD", false);
JCheckBox OREIMO = new JCheckBox("OREIMO", false);
JCheckBox GuiltyCrown = new JCheckBox("Guilty Crown", false);
JCheckBox Sakurasou = new JCheckBox("Sakurasou", false);
JCheckBox AngelBeats = new JCheckBox("Angel Beats", false);
JCheckBox AccelWorld = new JCheckBox("Accel World", false);
JCheckBox Nisekoi = new JCheckBox("Nisekoi", false);
JCheckBox NoGameNoLife = new JCheckBox("No Game No Life", false);
public Shiro_Anime(){
super("MY FAVORITE ANIME");
setLayout(new FlowLayout());
label.setFont(new Font("Arial", Font.ITALIC, 22));
information.setFont(new Font("Arial", Font.ITALIC, 16));
BlackBullet.addItemListener(this);
HighSchoolDxD.addItemListener(this);
OREIMO.addItemListener(this);
GuiltyCrown.addItemListener(this);
Sakurasou.addItemListener(this);
AngelBeats.addItemListener(this);
AccelWorld.addItemListener(this);
Nisekoi.addItemListener(this);
NoGameNoLife.addItemListener(this);
bGroup.add(BlackBullet);
bGroup.add(HighSchoolDxD);
bGroup.add(OREIMO);
bGroup.add(GuiltyCrown);
bGroup.add(Sakurasou);
bGroup.add(AngelBeats);
bGroup.add(AccelWorld);
bGroup.add(Nisekoi);
bGroup.add(NoGameNoLife);
add(label);
add(BlackBullet);
add(HighSchoolDxD);
add(OREIMO);
add(GuiltyCrown);
add(Sakurasou);
add(AngelBeats);
add(AccelWorld);
add(Nisekoi);
add(NoGameNoLife);
add(information);
}
public void itemStateChanged(ItemEvent e){
Object source = e.getItem();
if (source == BlackBullet){
int select = e.getStateChange();
if(select == ItemEvent.SELECTED){
information.setText("You are now watching Black Bullet");
}
else{
information.setText("");
}
}
if (source == HighSchoolDxD){
int select = e.getStateChange();
if(select == ItemEvent.SELECTED){
information.setText("You are now watching High School DxD");
}
else{
information.setText("");
}
}
if (source == OREIMO){
int select = e.getStateChange();
if(select == ItemEvent.SELECTED){
information.setText("You are now watching OREIMO");
}
else{
information.setText("");
}
}
if (source == GuiltyCrown){
int select = e.getStateChange();
if(select == ItemEvent.SELECTED){
information.setText("You are now watching Guilty Crown");
}
else{
information.setText("");
}
}
if (source == Sakurasou){
int select = e.getStateChange();
if(select == ItemEvent.SELECTED){
information.setText("You are now watching Sakurasou");
}
else{
information.setText("");
}
}
if (source == AngelBeats){
int select = e.getStateChange();
if(select == ItemEvent.SELECTED){
information.setText("You are now watching Angels Beats");
}
else{
information.setText("");
}
}
if (source ==AccelWorld){
int select = e.getStateChange();
if(select == ItemEvent.SELECTED){
information.setText("You are now watching Accel World");
}
else{
information.setText("");
}
}
if (source ==Nisekoi){
int select = e.getStateChange();
if(select == ItemEvent.SELECTED){
information.setText("You are now watching Nisekoi");
}
else{
information.setText("");
}
}
if (source ==NoGameNoLife){
int select = e.getStateChange();
if(select == ItemEvent.SELECTED){
information.setText("You are now watching No Game No Life");
}
else{
information.setText("");
}
}
}
public static void main (String[] args) {
final int FRAME_WIDTH = 350;
final int FRAME_HEIGHT = 200;
Shiro_Anime frame = new Shiro_Anime();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setVisible(true);
}
}