无法在Java Swing中将合成应用于继承

时间:2019-07-03 20:08:13

标签: java swing oop inheritance object-composition

当我创建Swing组件时(即我创建了一个不扩展任何内容的类,尤其是诸如JPanel之类的Swing组件),我试图在继承上采用合成。尝试切换到代码的设计视图时,Eclipse的Window Builder插件出现问题。

它引发以下异常:

  

无根方法

     

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

     

请注意,即使WindowBuilder无法自动识别您的代码模式,有时您仍然可以通过提供@ 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);

    }
}

0 个答案:

没有答案