Gui-2

Posted by depojones on Sat, 04 Jul 2020 17:59:22 +0200

Write a JFrame window, the requirements are as follows:
1. Place a JPanel panel in the NORTH area of the window.
2. JPanel panel places the following components:
(1) JLable label, the label text is "interest", then on the right are three JCheckBox multi-choice buttons, the options are "badminton", "table tennis", "singing". You can choose more than one.
(2) JLabel label, the label text is "gender", the right is followed by two jradiobuttons, the options are "male" and "female". Set to radio button, prompt: use the ButtonGroup class.
(3) Interest tags and buttons are placed in the first line, and gender labels and buttons are placed in the second line. Two row Box containers are used to arrange the positions of the two line components. The two row Box containers are placed in the JPanel panel. To align the two lines of components, you can set the GridLayout layout of two rows and one column on the JPanel panel.
3. Place a jscrollpan container in the CENTER area of the window, and place a JTextArea text field in the container.

4. When clicking the JCheckBox multiple selection button and JRadioButton button, if it is a selected operation, the text of the selected item will be displayed in the JTextArea text field, and one option will be displayed in each line. You can click repeatedly, and each click displays the selected item.

import java.awt.*;  
import java.awt.event.*;  
  
import javax.swing.*;  
  
  
public class Winframee extends JFrame implements ActionListener{  
        //JPanel panel  
        JPanel panel;  
        //Interest, gender JLabel label  
        JLabel label1,label2;  
        //Badminton, table tennis, singing three multi-choice buttons  
        JCheckBox checkbox1,checkbox2,checkbox3;  
        //Male and female buttons  
        JRadioButton button1,button2;  
        ButtonGroup group;  
          
        //Text area  
        JTextArea area=new JTextArea(20,30);  
        //Two box containers  
        Box box1,box2;  
  
        public Winframee() {  
            init();  
            setVisible(true);  
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        }  
        void init(){  
            //Create panel  
            panel=new JPanel();  
            panel.setLayout(new GridLayout(2,1));  
            //Create interest Tags  
            label1=new JLabel("interest");  
            //Create multi select button  
            checkbox1=new JCheckBox("badminton");  
            checkbox2=new JCheckBox("Table Tennis");  
            checkbox3=new JCheckBox("sing");  
            //Create gender label  
            label2=new JLabel("Gender");  
            //Radio Button   
            button1=new JRadioButton("male");  
            button2=new JRadioButton("female");  
            group=new ButtonGroup();  
            group.add(button1);  
            group.add(button2);  
            //Create two row box layouts  
            box1=Box.createHorizontalBox();  
            box2=Box.createHorizontalBox();  
            //Add the interest tag and the interest multiple selection button to box1  
            box1.add(Box.createHorizontalStrut(5));  
            box1.add(label1);  
            box1.add(checkbox1);  
            box1.add(checkbox2);  
            box1.add(checkbox3);  
            //Add a gender tag and a gender radio button to box2  
            box2.add(Box.createHorizontalStrut(5));  
            box2.add(label2);  
            box2.add(button1);  
            box2.add(button2);  
            //Add box1 and box2 to the panel panel  
            panel.add(box1);  
            panel.add(box2);  
            //Add a panel to the window NORTH area  
            add(panel,BorderLayout.NORTH);  
            //Adds a scrolling form to the CENTER area of the window  
            JScrollPane scroll=new JScrollPane(area);  
            add(scroll,BorderLayout.CENTER);  
            checkbox1.addActionListener(this);  
            checkbox2.addActionListener(this);  
            checkbox3.addActionListener(this);  
            button1.addActionListener(this);  
            button2.addActionListener(this);  
        }  
  
    public void actionPerformed (ActionEvent e) {  
        if(e.getSource()==checkbox1 && checkbox1.isSelected()==true) {  
            area.append("badminton"+"\n");  
        }  
        if(e.getSource()==checkbox2 && checkbox2.isSelected()==true) {  
            area.append("Table Tennis"+"\n");  
        }  
       if(e.getSource()==checkbox3 && checkbox3.isSelected()==true) {  
            area.append("sing"+"\n");  
        }  
      if(e.getSource()==button1 && button1.isSelected()==true) {  
            area.append("male"+"\n");  
        }else if(e.getSource()==button2 && button2.isSelected()==true) {  
            area.append("female"+"\n");  
        }  
          
        }  
}  

public class win {  
  
    public static void main(String[] args) {  
         Winframee win=new Winframee();  
         win.setBounds(100, 100, 400, 400);  
  
    }  
  
}  

Topics: Java