首页 > 解决方案 > 无法在 Java Swing 中应用组合而不是继承

问题描述

我试图在创建 Swing 组件时采用组合而不是继承(这意味着我创建了一个不扩展任何东西的类,特别是不是 Swing 组件,例如JPanel),但我遇到了 Eclipse 的 Window Builder 插件的问题当我尝试切换到我的代码的设计视图时。

它抛出以下异常:

没有根方法

解析器找不到任何根方法(解析的入口点)。解析器从每个 GUI 工具包的已知方法开始解析。例如,对于 Swing,它从 JPanel 或 JFrame 的构造函数开始。如果当前解析的类不是 Swing 组件的子类,解析器将尝试查找 main(java.lang.String[]) 方法。如果它也不存在,则会显示此异常。通常这意味着您正在尝试解析 WindowBuilder 不支持的内容(例如,非 GUI 编译单元)。

请注意,即使 WindowBuilder 无法自动识别您的 Code Pattern,有时您仍然可以通过提供 @wbp.parser.entryPoint JavaDoc 提示来教 WindowBuilder 使用它。例如:

public class MyClass {
    /**
     * @wbp.parser.entryPoint
     */
    public void someNonStandardEntryPoint() {
        JFrame frame = new JFrame();
        ...;
    }
}

然后它建议我可以应用上述 JavaDoc 的方法,但是当我这样做时,它们都不会产生可见的设计。我没有创建一个main()方法,因为它在这种情况下没有任何意义。

非子类和支持JDialog类紧随其后。如果第一类子类,我可以看到设计JPanel,但我试图避免这种情况。

CustomerAddressesSection.java

package myapp.gui;

import java.awt.Color;
import java.awt.FlowLayout;
import java.util.List;
import java.util.Objects;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import myapp.models.CustomerAddress;

public class CustomerAddressesSection {
    private JPanel jPanelCustomerAddresses;

    private JPanel panelAbaixoDeCustomerAddresses;
    private JPanel panelTituloEnderecos;
    private JPanel panelContendoOsEnderecos;
    private JLabel lblEnderecos;

    private JScrollPane scrollPaneCustomerAddresses;
    private JList<CustomerAddress> jListCustomerAddresses;

    private final List<CustomerAddress> customerAddresses;

    public CustomerAddressesSection(List<CustomerAddress> customerAddresses) {
        this.customerAddresses = Objects.requireNonNull(customerAddresses);
    }
    
    public List<CustomerAddress> getCustomerAddresses() {
        return customerAddresses;
    }
    
    public void layoutCustomerAddressesSection(JPanel panel) {
        jPanelCustomerAddresses = new JPanel();
        
        jPanelCustomerAddresses.setBackground(new Color(255, 255, 255));
        jPanelCustomerAddresses.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0), 1, true), new EmptyBorder(10, 10, 10, 10)));
        jPanelCustomerAddresses.setLayout(new BoxLayout(jPanelCustomerAddresses, BoxLayout.Y_AXIS));
        
        panelAbaixoDeCustomerAddresses = new JPanel();
        panelAbaixoDeCustomerAddresses.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
        jPanelCustomerAddresses.add(panelAbaixoDeCustomerAddresses);
        panelAbaixoDeCustomerAddresses.setLayout(new BoxLayout(panelAbaixoDeCustomerAddresses, BoxLayout.Y_AXIS));
        
        panelTituloEnderecos = new JPanel();
        panelTituloEnderecos.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
        FlowLayout fl_panelTituloEnderecos = (FlowLayout) panelTituloEnderecos.getLayout();
        fl_panelTituloEnderecos.setAlignment(FlowLayout.LEFT);
        panelTituloEnderecos.setBackground(Color.LIGHT_GRAY);
        panelAbaixoDeCustomerAddresses.add(panelTituloEnderecos);
        
        lblEnderecos = new JLabel("Endereço");
        panelTituloEnderecos.add(lblEnderecos);
        
        panelContendoOsEnderecos = new JPanel();
        panelContendoOsEnderecos.setBorder(new EmptyBorder(0, 0, 0, 0));
        panelContendoOsEnderecos.setBackground(Color.WHITE);
        panelAbaixoDeCustomerAddresses.add(panelContendoOsEnderecos);
        panelContendoOsEnderecos.setLayout(new BoxLayout(panelContendoOsEnderecos, BoxLayout.Y_AXIS));
        
        panel.add(jPanelCustomerAddresses);
    }
    
    public void addCustomerAddresses() {
        
        DefaultListModel<CustomerAddress> listModel = new DefaultListModel<>();
        for (CustomerAddress customerAddress : customerAddresses) {
            listModel.addElement(customerAddress);
        }

        jListCustomerAddresses = new JList<>(listModel);
        scrollPaneCustomerAddresses = new JScrollPane(jListCustomerAddresses);
        panelContendoOsEnderecos.add(scrollPaneCustomerAddresses);
        
        jListCustomerAddresses.setCellRenderer(new PanelIndividualCustomerAddress());
        jListCustomerAddresses.setVisibleRowCount(1);
        //panelContendoOsEnderecos.add(jListCustomerAddresses);
    }

    public static void main(String [] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                MainGUI mainGui = new MainGUI(Collections.emptyList(), null, true);
                CustomerInfo customerInfo = new CustomerInfo("Nome", "Email", LocalDate.now(), "ddd", "phone", "observação");
                List<CustomerAddress> customerAddresses = new ArrayList<CustomerAddress>();
                JDViewCustomer jDViewCustomer = new JDViewCustomer(mainGui, true, mainGui, customerInfo, customerAddresses, Collections.emptyList(), null, 1, 3);
                jDViewCustomer.setVisible(true);
            }
        });
    }
}

JDViewCustomer.java

package myapp.gui;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.swing.BoxLayout;
import javax.swing.JPanel;

import myapp.HttpClient;
import myapp.LoadCustomerOrdersAndGenerateReportWorker;
import myapp.models.CustomerAddress;
import myapp.models.CustomerInfo;
import myapp.models.CustomerOrder;
import net.sf.jasperreports.engine.JRException;

public class JDViewCustomer extends javax.swing.JDialog {

    private static final long serialVersionUID = 1L;
    
    public static final int CUSTOMER_ORDERS_LIMIT = 10;
    public static final String CUSTOMER_ORDERS_SORT_BY = "created_at";
    public static final String CUSTOMER_ORDERS_SORT_ORDER = "desc";
    
    private final CustomerInfoSection customerInfoSection;
    private final CustomerAddressesSection customerAddressesSection;
    private final CustomerOrdersSection customerOrdersSection;

    private JPanel panelContainingTheOtherPanels;
    
    private final MainGUI mainGui;
    private final HttpClient httpClient;
    private final int customerId;

    public JDViewCustomer(java.awt.Frame parent, boolean modal, MainGUI mainGui, CustomerInfo customerInfo,
            List<CustomerAddress> customerAddresses, List<CustomerOrder> customerOrders, HttpClient httpClient,
            int customerId, int maxPage) {
        super(parent, modal);
        this.mainGui = Objects.requireNonNull(mainGui);
        this.customerInfoSection = new CustomerInfoSection(Objects.requireNonNull(customerInfo), this);
        this.customerAddressesSection = new CustomerAddressesSection(Objects.requireNonNull(customerAddresses));
        this.customerOrdersSection = new CustomerOrdersSection(
                (List<CustomerOrder>)(new ArrayList<CustomerOrder>(Objects.requireNonNull(customerOrders))),
                mainGui, httpClient, customerId, this, maxPage);
        this.httpClient = httpClient;
        this.customerId = customerId;
        
        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

        panelContainingTheOtherPanels = new JPanel();
        panelContainingTheOtherPanels.setLayout(new BoxLayout(panelContainingTheOtherPanels, BoxLayout.Y_AXIS));
        getContentPane().add(panelContainingTheOtherPanels);

        customerInfoSection.layoutCustomerDataSection(panelContainingTheOtherPanels);
        customerInfoSection.addCustomerInfo();
        
        customerAddressesSection.layoutCustomerAddressesSection(panelContainingTheOtherPanels);
        customerAddressesSection.addCustomerAddresses();

        customerOrdersSection.layoutCustomerOrdersSection(panelContainingTheOtherPanels);
        customerOrdersSection.createCustomerOrdersTable();
        customerOrdersSection.updateCurrentPageTextField();
        
        initComponents();
    }

    public void loadCustomerOrdersAndGenerateReport() throws JRException {
        if (false == customerInfoSection.isLoadingIconShowing()) {
            customerInfoSection.setLoadingIconVisibility(true);
            
            LoadCustomerOrdersAndGenerateReportWorker worker =
                    new LoadCustomerOrdersAndGenerateReportWorker(httpClient,
                            customerInfoSection.getCustomerInfo(),
                            customerAddressesSection.getCustomerAddresses(),
                            customerId, this);
            mainGui.scheduleOnSingleThread(worker);
        }
    }
    
    public void updateCustomerOrdersAndPagingInformation(List<CustomerOrder> customerOrders, int total, int page) {
        customerOrdersSection.updateCustomerOrdersAndPagingInformation(customerOrders, total, page);
    }
    
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Ver Cliente");
        
        setMinimumSize(MainGUI.MINIMAL_DISPLAY_DIMENSION);

        pack();
        
        // Para centralizar o JDialog em relação a seu parent.
        setLocationRelativeTo(getParent());
        
    }

    public void setLoadingIconVisibility(boolean showing) {
        customerInfoSection.setLoadingIconVisibility(showing);
        
    }
}

标签: javaswingoopinheritanceobject-composition

解决方案


推荐阅读