繁体   English   中英

在面板内添加复选框列表并在单击确定按钮时打印选定的复选框

[英]Add List of checkboxes inside the panel and print the selected checkbox while clicking okay button

我是 JFrame 的新手,我正在尝试将复选框添加为面板“chkpanel”中的列表我在数组中提供了 4 个示例复选框,它在执行时没有设计。 我现在已经给出了 4 个复选框,但是如果我给出了 10,20 个复选框,它希望按顺序设置(例如:在具有滚动类型的网格视图中)。

我的代码

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollBar;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

public class Pdf extends JFrame {
  private JPanel contentPane;
  private JTextField textField;
  private JRadioButton yes;
  private JRadioButton no;
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

      public void run() {
        try {
          Pdf frame = new Pdf();
          frame.setVisible(true);
        }
        catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }
  String[] database = {"Activity Diagram","Sequence Diagam","Block Definition Diagram","Internal Block Diagram"};
  CheckboxCommand chk1 =  new CheckboxCommand("Activity Diagram", new JCheckBox(database[0]));
  CheckboxCommand chk2 =  new CheckboxCommand("Sequence Diagram", new JCheckBox(database[1]));
  CheckboxCommand chk3 =  new CheckboxCommand("Block definition Diagram", new JCheckBox(database[2]));
  CheckboxCommand chk4 =  new CheckboxCommand("Internal Block Diagram", new JCheckBox(database[3]));
  private final ButtonGroup buttonGroup = new ButtonGroup();
  public Pdf() {
    setTitle("PDF");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 515, 381);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    
    Border blackline = BorderFactory.createLineBorder(Color.black);
    JPanel panel = new JPanel();
    panel.setBounds(29, 5, 431, 61);
    panel.setBorder(new TitledBorder(null, "Specify your metaClass", TitledBorder.LEADING, TitledBorder.TOP, null, null));
    contentPane.add(panel);
    panel.setLayout(null);
    
    JPanel chkpanel = new JPanel();
    chkpanel.add(chk1.checkbox);
    chkpanel.add(chk2.checkbox);
    chkpanel.add(chk3.checkbox);
    chkpanel.add(chk4.checkbox);
 
    yes = new JRadioButton("YES",true);
    buttonGroup.add(yes);
    yes.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if(yes.isSelected())
        {
          no.setSelected(false);
          textField.setEnabled(true);
        }
      }
    });
    yes.setBounds(8, 27, 63, 25);
    panel.add(yes);
    
    no = new JRadioButton("NO");
    buttonGroup.add(no);
    no.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if(no.isSelected())
        {
          yes.setSelected(false);
          textField.setEnabled(false);
          chk1.setSelected(false);
          chk2.setSelected(false);
          chkpanel.setEnabled(false);
        }
      }
    });
    no.setBounds(75, 27, 63, 25);
    panel.add(no);
    
    Border blackline1 = BorderFactory.createLineBorder(Color.black);
    JPanel txtpanel = new JPanel();
    txtpanel.setBounds(29, 172, 431, 72);
    txtpanel.setBorder(new TitledBorder(null, "Enter your MetaClass", TitledBorder.LEADING, TitledBorder.TOP, null, null));
    contentPane.add(txtpanel);
    txtpanel.setLayout(null);
    
    textField = new JTextField();
    textField.setEnabled(true);
    textField.setBounds(12, 27, 391, 33);
    txtpanel.add(textField);
    textField.setColumns(10);
    
    JButton btnNewButton = new JButton("OK");
    btnNewButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        String selection=null;
        if(yes.isSelected())
        {
          selection="'Yes' option Selected";
        }
        if(no.isSelected())
        {
          selection="'No' option Selected";
        }
        System.out.println(selection);
        System.out.println(textField.getText());
      }
    });
    btnNewButton.setBounds(106, 274, 97, 25);
    contentPane.add(btnNewButton);
    
    JButton btnNewButton_1 = new JButton("CANCEL");
    btnNewButton_1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
         System.exit(0);
      }
    });
    btnNewButton_1.setBounds(263, 274, 97, 25);
    contentPane.add(btnNewButton_1);
    
    Border blackline2 = BorderFactory.createLineBorder(Color.black);
  //  JPanel chkpanel = new JPanel();
    chkpanel.setBounds(29, 79, 431, 80);
    chkpanel.setBorder(blackline);
    contentPane.add(chkpanel);
    GridBagLayout gbl_chkpanel = new GridBagLayout();
    gbl_chkpanel.columnWidths = new int[]{0};
    gbl_chkpanel.rowHeights = new int[]{0};
    gbl_chkpanel.columnWeights = new double[]{};
    gbl_chkpanel.rowWeights = new double[]{};
    chkpanel.setLayout(gbl_chkpanel);
   
   // chkpanel.setVisible(true);
    int numberCheckBox = 5;
    JCheckBox[] checkBoxList = new JCheckBox[numberCheckBox];
   
    chkpanel.setLayout(null);
    panel.setLayout(null);
    JScrollBar scrollBar = new JScrollBar();
    scrollBar.setBounds(439, 79, 21, 80);
    contentPane.add(scrollBar);
  }
  
  private class CheckboxCommand {
    private String cmdToRun;
    private boolean isSelected;
    private JCheckBox checkbox;

    public CheckboxCommand(String cmdToRun, JCheckBox checkbox) {
        this.cmdToRun = cmdToRun;
        this.checkbox = checkbox;
    }

    public String getCmdToRun() {
        return cmdToRun;
    }

    public void setCmdToRun(String cmdToRun) {
        this.cmdToRun = cmdToRun;
    }

    public boolean isSelected() {
        return this.checkbox.isSelected();
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
    }
}
}

  1. 我想在单击确定按钮时打印选定的复选框。

您正在为 chkpanel 使用GridBagLayout ,但忘记为每个复选框设置GridBagConstraints

看看这个教程: https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM