如何使用复选框更改java中的背景颜色?

时间:2014-04-14 18:22:57

标签: java swing actionlistener jcheckbox

我想通过复选框更改java程序的颜色。我一直在研究这个程序,这些只是成功的最后一步。我不想添加更多细节.dfftwe5trtwtrewghdsgrteegytehyedtnhtfdbeghd。

这是我目前所拥有的:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.JCheckBox;


import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
 * Write a description of class GUI here.
 * 
 * @author Ibrahim Y. Hmood 
 * @version 04.02.14
 */
public class GUI extends JFrame implements ActionListener {
    /**
     * Identify the components for the window
     */
    JPanel contentPane;
    JLabel lblLastName;
    JLabel lblFirstName;
    JLabel lblStudentID;
    JTextField txtLastName;
    JTextField txtFirstName;
    JTextField txtStudentID;
    JButton    btnSubmit;
    JLabel lblStoreFirstName;
    JLabel lblStoreLastName;
    JLabel lblStoreStudentID;
    JTextField txtStorestudentID;
    JTextArea txaAll;
    JCheckBox chkBox1;
    CheckBoxListener myListener = null;
    JCheckBox chkBox2;
    JCheckBox chkBox3;
    /**
     * Constructor for objects of class GUI
     */
public GUI()
{
    /**
     * Create the panel, its components and add them to the panel
     */
    contentPane = new JPanel (null);
    lblLastName = new JLabel ("Enter Last Name");
    lblFirstName = new JLabel ("Enter First Name");
    lblStudentID = new JLabel ("Enter Student ID");
    txtLastName = new JTextField ();
    txtFirstName = new JTextField();
    txtStudentID = new JTextField();
    btnSubmit = new JButton ("Submit Info");
    lblStoreFirstName = new JLabel ();
    lblStoreLastName = new JLabel ();
    lblStoreStudentID = new JLabel ();
    txaAll= new JTextArea ();
    chkBox1 =  new JCheckBox ("Check box one");
    chkBox1.setBounds(200,180,150,20);
    chkBox2 = new JCheckBox ("Check box 2");
    chkBox2.setBounds(200,200,150,20);
    chkBox3 = new JCheckBox ("Check box 3");
    chkBox3.setBounds(200,220,150,20);
    JRadioButton Label  = new JRadioButton ("Check this Button");
    Label.setBounds(20,180,150,20);
    JRadioButton Label1 = new JRadioButton ("Check this button, too");
    Label1.setBounds (20,200,150,20);
    JRadioButton Label2 = new JRadioButton ("Don't forget to check this button,too");
    Label2.setBounds (20,220,150,20);
    JRadioButton Label3 = new JRadioButton ("Color change");
    Label3.setBounds(20,240,150,20);


    lblLastName.setBounds(25, 25, 590, 20);
    lblFirstName.setBounds (25, 65,590, 20);
    lblStudentID.setBounds (25, 105,590, 20);


    txtLastName.setBounds(140,25,150,20);
    txtFirstName.setBounds(140,65,150,20);
    txtStudentID.setBounds(140,105,150,20);
    txaAll.setBounds(140,345,250,250); 


    btnSubmit.setBounds(100, 130, 250 ,20);

   lblStoreFirstName.setBounds(200,200,150,20);
    lblStoreLastName.setBounds(200, 240, 150, 20);
     lblStoreStudentID.setBounds(200,260, 150, 20);

    contentPane.add(lblLastName);
    contentPane.add(lblFirstName);
    contentPane.add(lblStudentID);

    contentPane.add(txtLastName);
    contentPane.add(txtFirstName);
    contentPane.add(txtStudentID);
    contentPane.add(lblStoreFirstName);
    contentPane.add(lblStoreLastName);
    contentPane.add(lblStoreStudentID);
    contentPane.add(txaAll);
    contentPane.add(Label);
    contentPane.add(Label1);
    contentPane.add(Label2);
    contentPane.add(Label3);
    contentPane.add(chkBox2);
    contentPane.add(chkBox3);

    myListener = new CheckBoxListener();
    chkBox1.addItemListener(new CheckBoxListener());
    contentPane.add(chkBox1);


    chkBox1.setActionCommand("checkbox1");
    chkBox1.addActionListener(this);
     contentPane.add(chkBox1);

     chkBox2.setActionCommand ("");

    btnSubmit.setActionCommand("Submit");
    btnSubmit.addActionListener(this);
    contentPane.add(btnSubmit);


    setTitle("GUI");
    setContentPane(contentPane);
    setLocation(0,0);
    setSize(600,600);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
    private class CheckBoxListener implements ItemListener{
        public void itemStateChanged (ItemEvent e)
        {

            Object source = e.getSource(); {
            if (source == chkBox1 ) {
                if (e.getStateChange() == ItemEvent.SELECTED) 
                    txaAll.setBackground(Color.green);


                }
            }
        }
    }







    @Override 
    public void actionPerformed(ActionEvent e) { 
        if ("Submit".equals(e.getActionCommand())) {
            btnSubmit.setBackground(Color.red);
            btnSubmit.setBackground(Color.cyan);
            String lastname = txtLastName.getText();
            String firstname = txtFirstName.getText();
            String studentID = txtStudentID.getText();
            String str = txaAll.getText();
            str+= firstname + "\t" + lastname + "\t" + studentID + "\n";
            lblStoreLastName.setText(lastname);
            lblStoreFirstName.setText (firstname);
            lblStoreStudentID.setText (studentID);
            txaAll.setText(str);      
        }
    }
}

1 个答案:

答案 0 :(得分:2)

如果您想在程序颜色时更改JFrame颜色,则可以执行以下操作:

if (source == chkBox1) {
    if (e.getStateChange() == ItemEvent.SELECTED) {
        txaAll.getParent().setBackground(Color.blue);
    }
} else if(source == chkBox2) {
    //set the color you want
}