从 Panel Array 发出对 JButton 的设置约束

Issue setting constraints on a JButton from a Panel Array

我正在创建一个日历并为每一天创建一个单独的 JPanel。所有这些面板都放入一个 ArrayList 中。但是,当我尝试使用 SpringLayout 向 JButton 添加约束时,它似乎无法从 ArrayList 调用。

我确实调用了 validate();和重绘();

然而我还是没有运气。

    private void addEvents(int currentMonth) {
    for (int i = 0; i < eventArrayList.size(); i++) {
        JButton eventButton = new JButton();
        eventButtonArray.add(eventButton);
        String[] eventListSplit = eventArrayList.get(i).split(", ");
        if (Integer.parseInt(eventListSplit[2]) == currentMonth && eventListSplit[4].equalsIgnoreCase(initialFrame.getInitialPanel().getRightPanel().getNorthPanel().getYear().getText())) {
            eventButton.setText(eventListSplit[0]);
            eventButton.setBackground(Color.decode("#FF9D00"));
            eventButton.setFocusPainted(false);
            eventButton.setFont(new Font("Times New Roman", Font.BOLD, screenSize.width / 100));
            initialFrame.getInitialPanel().getRightPanel().getCenterPanel().getDayPanelArray().get(Integer.parseInt(eventListSplit[3]) + dateHashMap.get(currentMonth - 1).get(0) - 2).add(eventButton);
            initialFrame.getInitialPanel().getRightPanel().getCenterPanel().getDayPanel().getLayout().putConstraint(SpringLayout.HORIZONTAL_CENTER, eventButton, 0, SpringLayout.HORIZONTAL_CENTER, (DayPanel)initialFrame.getInitialPanel().getRightPanel().getCenterPanel().getDayPanelArray().get(Integer.parseInt(eventListSplit[3]) + dateHashMap.get(currentMonth - 1).get(0) - 2));
            initialFrame.getInitialPanel().getRightPanel().getCenterPanel().getDayPanel().getLayout().putConstraint(SpringLayout.VERTICAL_CENTER, eventButton, 0, SpringLayout.VERTICAL_CENTER, initialFrame.getInitialPanel().getRightPanel().getCenterPanel().getDayPanelArray().get(Integer.parseInt(eventListSplit[3]) + dateHashMap.get(currentMonth - 1).get(0) - 2));
            eventButton.addActionListener((ActionEvent e) -> {
                try {
                    newEvent = new NewEvent(screenSize);
                } catch (ParseException ex) {
                    System.out.println(ex.getMessage());
                }

                for (int k = 0; k < eventArrayList.size(); k++) {
                    String[] eventListSplit2 = eventArrayList.get(k).split(", ");
                    if (eventButton.getText().equalsIgnoreCase(eventListSplit2[0])) {
                        newEvent.getNewEventInitialPanel().getEventName().setText(eventListSplit2[0]);
                        newEvent.getNewEventInitialPanel().getLocationBox().setText(eventListSplit2[1]);
                        newEvent.getNewEventInitialPanel().getStartDate().setText(eventListSplit2[2] + "/" + eventListSplit2[3] + "/" + eventListSplit2[4]);
                        newEvent.getNewEventInitialPanel().getStartTime().setText(eventListSplit2[5] + ":" + eventListSplit2[6] + ":" + eventListSplit2[7]);
                        newEvent.getNewEventInitialPanel().getEndDate().setText(eventListSplit2[8] + "/" + eventListSplit2[9] + "/" + eventListSplit2[10]);
                        newEvent.getNewEventInitialPanel().getEndTime().setText(eventListSplit2[11] + ":" + eventListSplit2[12] + ":" + eventListSplit2[13]);
                        if (eventListSplit2[14].equalsIgnoreCase("true")) {
                            newEvent.getNewEventInitialPanel().getAllDay().setSelected(true);
                        }
                        newEvent.getNewEventInitialPanel().getDescription().setText(eventListSplit2[15]);
                    }
                }

                newEvent.setSize(screenSize.height / 2, screenSize.width / 2);
                newEvent.setLocationRelativeTo(null);
                newEvent.setVisible(true);

                newEvent.getNewEventInitialPanel().getSave().addActionListener((ActionEvent e1) -> {
                    JOptionPane.showMessageDialog(newEvent.getNewEventInitialPanel(), "Option not yet supported!");
                });

                newEvent.getNewEventInitialPanel().getDelete().addActionListener((ActionEvent e1) -> {
                    JOptionPane.showMessageDialog(newEvent.getNewEventInitialPanel(), "Option not yet supported!");
                });
            });
        }
    }
    initialFrame.validate();
    initialFrame.repaint();
}    

虽然我进行了测试以确保获得正确的面板,但我仍然无法移动 JButton。它应该根据代码居中。

感谢您的帮助!

根据 Java 的 SpringLayout 引用 (https://docs.oracle.com/javase/tutorial/uiswing/layout/spring.html)

看来您 可能 缺少 west/east 和 north/south 约束。

Unlike many layout managers, SpringLayout does not automatically set the location of the components it manages. If you hand-code a GUI that uses SpringLayout, remember to initialize component locations by constraining the west/east and north/south locations. Depending on the constraints you use, you may also need to set the size of the container explicitly.

如何在 SpringDemo2.java 和 SpringDemo3.java 中设置 West/East 和 North/South 位置的示例(链接在该页面上)。