gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:48:16 25 4
gpt4 key购买 nike

我正在尝试创建一个程序,其中 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)
{

}
}

最佳答案

您需要隔离和分隔您的工作单元。例如,您的 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 Concepts 找到一些信息和 Inheritance有用且无意冒犯,在处理像 UI 这样复杂的事情之前我会先了解这些主题;)

附注

通常,我会将某种“ Controller ”接口(interface)传递给OrderPane,它将提供从主 UI 中删除组件的功能以及其他功能OrderPane 可能需要提供反馈。

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

...但是让我们一次处理一个概念...

关于java - 通过选中所有必需的复选框来启用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25071464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com