Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class ColorSelectionGUI extends JFrame{
- private JCheckBox northCB, southCB, eastCB, westCB;
- private JRadioButton redRB, greenRB, blueRB, cyanRB;
- private ButtonGroup ColorSelectGroup;
- private JPanel checkboxPanel, radioPanel, myButtons, northJP, southJP, eastJP, westJP;
- private JLabel northJL, southJL, eastJL, westJL;
- private BorderLayout borderLayoutMgr;
- private final int WINDOW_WIDTH = 500;
- private final int WINDOW_HEIGHT = 250;
- public ColorSelectionGUI() {
- setTitle("Color Selection GUI");
- setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- buildGUI();
- setVisible(true);
- }
- public void buildGUI(){
- Color red = new Color(255,0,0);
- Color blue = new Color(0,0,255);
- Color green = new Color(0,255,0);
- Color cyan = new Color(0,255,255);
- borderLayoutMgr = new BorderLayout();
- Container pane = getContentPane();
- northCB = new JCheckBox("North");
- southCB = new JCheckBox("South");
- eastCB = new JCheckBox("East");
- westCB = new JCheckBox("West");
- redRB = new JRadioButton("Red");
- redRB.setForeground(red);
- greenRB = new JRadioButton("Green");
- greenRB.setForeground(green);
- blueRB = new JRadioButton("Blue");
- blueRB.setForeground(blue);
- cyanRB = new JRadioButton("Cyan");
- cyanRB.setForeground(cyan);
- northJL = new JLabel("North");
- northJL.setHorizontalAlignment(JLabel.CENTER);
- northJL.setVerticalAlignment(JLabel.CENTER);
- southJL = new JLabel("South");
- southJL.setHorizontalAlignment(JLabel.CENTER);
- southJL.setVerticalAlignment(JLabel.CENTER);
- eastJL = new JLabel("East");
- eastJL.setHorizontalAlignment(JLabel.CENTER);
- eastJL.setVerticalAlignment(JLabel.CENTER);
- westJL = new JLabel("West");
- westJL.setHorizontalAlignment(JLabel.CENTER);
- westJL.setVerticalAlignment(JLabel.CENTER);
- northJP = new JPanel();
- northJP.add(northJL);
- southJP = new JPanel();
- southJP.add(southJL);
- eastJP = new JPanel();
- eastJP.add(eastJL);
- westJP = new JPanel();
- westJP.add(westJL);
- checkboxPanel = new JPanel();
- checkboxPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Locations"),BorderFactory.createBevelBorder(1)));
- checkboxPanel.add(northCB);
- checkboxPanel.add(southCB);
- checkboxPanel.add(eastCB);
- checkboxPanel.add(westCB);
- checkboxPanel.setLayout(new GridLayout(4,1));
- radioPanel = new JPanel();
- radioPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Bg Color"), BorderFactory.createBevelBorder(1)));
- radioPanel.add(redRB);
- radioPanel.add(greenRB);
- radioPanel.add(blueRB);
- radioPanel.add(cyanRB);
- radioPanel.setLayout(new GridLayout(4,1));
- ColorSelectGroup = new ButtonGroup();
- ColorSelectGroup.add(redRB);
- ColorSelectGroup.add(greenRB);
- ColorSelectGroup.add(blueRB);
- ColorSelectGroup.add(cyanRB);
- cyanRB.setSelected(true);
- myButtons = new JPanel();
- myButtons.setLayout(new GridLayout(1,2));
- myButtons.add(checkboxPanel);
- myButtons.add(radioPanel);
- pane.add(northJP, BorderLayout.NORTH);
- pane.add(westJP, BorderLayout.WEST);
- pane.add(myButtons, BorderLayout.CENTER);
- pane.add(eastJP, BorderLayout.EAST);
- pane.add(southJP, BorderLayout.SOUTH);
- redRB.addActionListener(new RadioButtonListener());
- greenRB.addActionListener(new RadioButtonListener());
- blueRB.addActionListener(new RadioButtonListener());
- cyanRB.addActionListener(new RadioButtonListener());
- }
- class RadioButtonListener implements ActionListener {
- public void actionPerformed(ActionEvent arg0) {
- Color color = null;
- if(redRB.isSelected()){
- color = Color.red;
- }
- if(greenRB.isSelected()){
- color = Color.green;
- }
- if(blueRB.isSelected()){
- color = Color.blue;
- }
- if(cyanRB.isSelected()){
- color = Color.cyan;
- }
- if (northCB.isSelected()){
- northJP.setBackground(color);
- }
- if (eastCB.isSelected()){
- eastJP.setBackground(color);
- }
- if (southCB.isSelected()){
- southJP.setBackground(color);
- }
- if (westCB.isSelected()){
- westJP.setBackground(color);
- }
- }
- }
- public static void main(String[] args) {
- ColorSelectionGUI flow = new ColorSelectionGUI();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement