Home > Mobile >  Java Checkboxes and İmage
Java Checkboxes and İmage

Time:11-05

Can someone help me with checkboxes and image

i need the do a college homework with java

i want to create logic gates with gui

example

and gate

2 checkbox

and one image

if both checked image turn green

if 1 or 0 checked image turn red Can Someone help me to do that


import java.awt.event.*;
import java.awt.*;
import javax.swing.\*;
class solve extends JFrame {

    // frame
    static JFrame f;
    static JLabel l;
    Label label; 
    
    // main class
    public static void main(String[] args)
    {
        // create a new frame
        f = new JFrame("frame");
    
        // set layout of frame
        f.setLayout(new FlowLayout());
    
        // create checkbox
        JCheckBox c1 = new JCheckBox("checkbox 1");
        JCheckBox c2 = new JCheckBox("checkbox 2");
    
        // create a new panel
        JPanel p = new JPanel();    
    
        // add checkbox to panel
        p.add(c1);
        p.add(c2);
    
        // add panel to frame
        f.add(p);
    
        // set the size of frame
        f.setSize(300, 300);
    
        f.show();
    
    
    }

}

i dont know how to check checkboxes checked and place image they dont teach me on college but they give me a project i cant find a docs or something to do

CodePudding user response:

You do not need to create a JFrame although it would work. I created a JPanel which you may use many times within one JFrame if you so like.

public class AndGate extends JPanel implements ChangeListener {
    private JCheckBox in1;
    private JCheckBox in2;
    private JLabel result;

    public AndGate() {
        super();
        setLayout(new FlowLayout());
        
        in1 = new JCheckBox();
        in1.addChangeListener(this);
        add(in1);
        in2 = new JCheckBox();
        in2.addChangeListener(this);
        add(in2);
        result = new JLabel("green");
        add(result);
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        System.out.println("stateChanged "   e);
        if (in1.isSelected() && in2.isSelected()) {
            result.setText("green");
        } else {
            result.setText("red");
        }
    }
}

Using that class may then look like this:

public class NewJFrame extends javax.swing.JFrame {

    public NewJFrame() {
        initComponents();
        setLayout(new BorderLayout());
        add(new AndGate(), BorderLayout.CENTER);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                NewJFrame f = new NewJFrame();
                f.pack();
                f.setVisible(true);
            }
        });
    }

}

For simplicity I just used the test red and green. If you want to replace that with icons or graphics with your desired color, just add the solution mentioned in https://stackoverflow.com/a/72310825/4222206

CodePudding user response:

  1. You have to add a third checkbox for output.
  2. Add action listeners to both inputs. The action listeners will trigger when users click the check boxes.
  3. When the action listener is triggered, check status of both inputs using JCheckBox#isSelected.
  4. If both inputs are selected, mark output checkbox as selected else mark it as not selected using JCheckBox#setSelected
  5. Refresh the panel so the change to output checkbox is visible on UI. JPanel#repaint
  6. Optional: Set output as read-only so the user can't edit it manually JCheckBox#setEnabled

Additionally,

  • Try to use meaningful variable names. That's a good practice
  • I see you have imported packages with wildcards. All modern IDEs help you with imports. No need to manually enter package names with wildcards.
  • Try to avoid using deprecated methods. e.g.: instead of f.show() use setVisible()

You can follow the steps I mentioned. If you need more help you can refer to the code below.

import java.awt.FlowLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

class AndDemo extends JFrame {

    JFrame frame = new JFrame("AND GATE");
    JCheckBox input1 = new JCheckBox("input 1");
    JCheckBox input2 = new JCheckBox("input 2");
    JCheckBox output = new JCheckBox("output");
    JPanel outputPanel = new JPanel();

    public AndDemo() {

        input1.addActionListener(actionEvent -> {
            updateOutputState();
        });


        input2.addActionListener(actionEvent -> {
            updateOutputState();
        });

        createFrame();
    }

    private void createFrame() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());

        JPanel inputPanel = new JPanel();
        inputPanel.add(input1);
        inputPanel.add(input2);
        frame.add(inputPanel);


        outputPanel.add(output);
        frame.add(outputPanel);


        frame.setSize(300, 300);
        frame.setVisible(true);
    }

    private void updateOutputState() {
        if(input1.isSelected() && input2.isSelected()) {
            output.setSelected(true);
        } else {
            output.setSelected(false);
        }
        outputPanel.repaint();
    }
}

class RunAndDemo {
    public static void main(String[] args) {
        new AndDemo();
    }
}
  • Related