通过选中所有必需的复选框启用按钮

时间:2014-08-01 01:20:54

标签: java swing jbutton jcheckbox

我正在尝试创建一个程序,只有在JButton所有JCheckBoxes都已选中时才会启用JButton,但我在启用特定JCheckBoxes方面遇到问题,因为一次我检查了所有JButton,启用的唯一public class Practice extends JFrame implements ActionListener { JPanel main=new JPanel(); JPanel gui=new JPanel(); JButton btnadd=new JButton("Add Timer"); JPanel order=new JPanel(); JPanel chkPanel=new JPanel(); JButton remove=new JButton("Remove"); public Practice() { main.setLayout(new BorderLayout()); gui.setLayout(new FlowLayout()); main.add(btnadd, BorderLayout.NORTH); main.add(gui,BorderLayout.CENTER); add(main); btnadd.addActionListener(new ActionListener() { int ctr=0; @Override public void actionPerformed(ActionEvent e) { ctr+=1; //addPanel(); //System.out.print(x); addPanel(ctr); revalidate(); repaint(); } }); main.add(gui); } public void addPanel(int ctr) { Border blackline=BorderFactory.createLineBorder(Color.BLACK); order=new JPanel(); order.setPreferredSize(new Dimension(200,300)); order.setLayout(new BorderLayout()); TitledBorder title=BorderFactory.createTitledBorder(blackline); title.setTitleJustification(TitledBorder.LEFT); order.setBorder(title); addCheckPanel(ctr); addBtn(); gui.add(order); } public void addCheckPanel(int ctr) { chkPanel=new JPanel(); chkPanel.setLayout(new BoxLayout(chkPanel, BoxLayout.Y_AXIS)); for(int i=0;i<ctr;i++) addCheck(ctr); order.add(chkPanel); } int y; public void addCheck(int ctr) { JCheckBox check=new JCheckBox("Check Me"); chkPanel.add(check); y=ctr; check.addActionListener(listener); public void addBtn() { remove=new JButton("Remove"); order.add(remove, BorderLayout.SOUTH); remove.setEnabled(false); remove.addActionListener(listener); } public static void main(String args[]) { TimerPractice p=new TimerPractice(); p.setSize(1000,800); p.setVisible(true); p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); p.setLocationRelativeTo(null); } ActionListener listener=new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source instanceof Component) { Component comp = (Component)source; gui.remove(comp.getParent()); revalidate(); repaint(); } } }; @Override public void actionPerformed(ActionEvent e) { } } 是最后创建的

这是我的代码:

{{1}}

1 个答案:

答案 0 :(得分:2)

您需要隔离和分离您的工作单元。例如,您的order面板具有控件和功能,可以更好地分隔到它自己的类中,它可以独立于其自身的其他实例来维护它自己的状态,例如...

您可以提取主类的大部分代码并创建一个单独的类,例如OrderPane ...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

public class OrderPane extends JPanel {

    private JPanel chkPanel;
    private int selectedCount = 0;
    private int checkCount = 0;

    private JButton removeButton;

    public OrderPane(int checkCount) {
        setLayout(new BorderLayout());

        Border blackline = BorderFactory.createLineBorder(Color.BLACK);
        TitledBorder title = BorderFactory.createTitledBorder(blackline);
        title.setTitleJustification(TitledBorder.LEFT);
        setBorder(title);

        this.checkCount = checkCount;

        addCheckPanel();
        addBtn();

    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 300);
    }

    public void addCheckPanel() {
        chkPanel = new JPanel();
        chkPanel.setLayout(new BoxLayout(chkPanel, BoxLayout.Y_AXIS));
        for (int i = 0; i < checkCount; i++) {
            addCheck();
        }
        add(chkPanel);
    }

    public void addCheck() {
        JCheckBox check = new JCheckBox("Check Me");
        chkPanel.add(check);
        check.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JCheckBox cb = (JCheckBox) e.getSource();
                if (cb.isSelected()) {
                    selectedCount++;
                } else {
                    selectedCount--;
                }
                if (selectedCount == checkCount) {
                    removeButton.setEnabled(true);
                }
            }
        });
    }

    public void addBtn() {
        removeButton = new JButton("Remove");
        add(removeButton, BorderLayout.SOUTH);
        removeButton.setEnabled(false);
        removeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object source = e.getSource();
                if (source instanceof Component) {
                    Component comp = (Component) source;
                    Component myParent = comp.getParent();
                    Container yourParent = myParent.getParent();
                    yourParent.remove(myParent);
                    yourParent.revalidate();
                    yourParent.repaint();
                }
            }
        });
    }

}

您可以在需要时创建此类的新实例,并根据需要将其添加到UI中,例如......

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Practice extends JFrame {

    JPanel main = new JPanel();
    JPanel gui = new JPanel();
    JButton btnadd = new JButton("Add Timer");

    public Practice() {

        main.setLayout(new BorderLayout());
        gui.setLayout(new FlowLayout());
        main.add(btnadd, BorderLayout.NORTH);
        main.add(gui, BorderLayout.CENTER);
        add(main);

        final Random rnd = new Random();
        btnadd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                OrderPane orderPane = new OrderPane(rnd.nextInt(10));
                gui.add(orderPane);
                gui.revalidate();
                gui.repaint();
            }
        });

        main.add(gui);
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                Practice p = new Practice();
                p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                p.setSize(800, 600);
                p.setLocationRelativeTo(null);
                p.setVisible(true);
            }
        });
    }
}

你可能会发现Object-Oriented Programming ConceptsInheritance使用的一些信息而且没有冒犯,我会在处理像UI这样复杂的东西之前理解这些主题;)

附注

通常,我会将某种“控制器”interface传递给OrderPane,这将提供从主UI中删除组件的功能以及OrderPane的其他功能可能需要提供反馈。

这些反馈中的一些可以通过观察者/监听器模式提供,例如当计时器超时时,最好将某种事件提供给已注册的监听器,然后可能在“控制器”上调用方法 - 恕我直言

...但我们一次只处理一个概念......