簡體   English   中英

java 對話框 inheritance 是如何工作的? 我失去了一些財產

[英]How does java dialog inheritance work? I lose some properties

對不起,我對 Java 或 swing 應用程序了解不多。 我創建了一個名為 DlgShape 的對話框,其中有 2 個文本字段、2 個按鈕和 2 個標簽。 我嘗試創建 DlgRectangle 而不是繼承自 JDialog 我繼承自 DlgShape。 父子 class 的設計是相同的,但在 DlgRectangle 中,我沒有將標簽、按鈕和文本字段列為屬性,我有公共獲取和設置方法以及 getContentPane。 突然我的 DlgRectangle 布局從 GridBagLayout 變成了 BorderLayout,為什么會這樣? 我有默認的 DlgShape 構造函數將布局設置為 GridBagLayout,我的 DlgRectangle 構造函數也調用超級構造函數。

我嘗試手動輸入代碼以創建 label 並將其放入內容面板,但它沒有出現。 我有一個想法,也許我需要讓內容面板成為 DlgShape 的屬性,並可能使其受到保護。

這是我的 DlgShape 代碼:package 繪圖;

public class DlgShape extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private boolean validInput;
    private JTextField txtX;
    private JTextField txtY;
    private JButton btnOuterColor = new JButton("outer color");
    private JButton btnInnerColor = new JButton("inner color");
    JLabel lblX = new JLabel("X");
    
    private Shape shape;
    
    private void setShape(Shape shape) {
        this.shape=shape;
    }
    
    private Shape getShape(Shape shape) {
        return shape;
    }
    
    public void setValidInput(boolean validInput) {
        this.validInput=validInput;
    }
    
    public boolean isValidInput() {
        return this.validInput;
    }
    
    public JTextField getTxtX() {
        return txtX; 
    }
    public JTextField getTxtY() {
        return txtY; 
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            DlgShape dialog = new DlgShape();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public DlgShape() {
        setModal(true);
        setBounds(100, 100, 233, 300);
        
        
        //Auto generated
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        GridBagLayout gbl_contentPanel = new GridBagLayout();
        gbl_contentPanel.columnWidths = new int[]{0, 0, 0};
        gbl_contentPanel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0};
        gbl_contentPanel.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
        gbl_contentPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        contentPanel.setLayout(gbl_contentPanel);
        {
            
            GridBagConstraints gbc_lblX = new GridBagConstraints();
            gbc_lblX.anchor = GridBagConstraints.EAST;
            gbc_lblX.insets = new Insets(0, 0, 5, 5);
            gbc_lblX.gridx = 0;
            gbc_lblX.gridy = 0;
            contentPanel.add(lblX, gbc_lblX);
        }
        {
            txtX = new JTextField();
            GridBagConstraints gbc_txtX = new GridBagConstraints();
            gbc_txtX.insets = new Insets(0, 0, 5, 0);
            gbc_txtX.fill = GridBagConstraints.HORIZONTAL;
            gbc_txtX.gridx = 1;
            gbc_txtX.gridy = 0;
            contentPanel.add(txtX, gbc_txtX);
            txtX.setColumns(10);
        }
        {
            JLabel lblY = new JLabel("Y");
            GridBagConstraints gbc_lblY = new GridBagConstraints();
            gbc_lblY.anchor = GridBagConstraints.EAST;
            gbc_lblY.insets = new Insets(0, 0, 5, 5);
            gbc_lblY.gridx = 0;
            gbc_lblY.gridy = 1;
            contentPanel.add(lblY, gbc_lblY);
        }
        {
            txtY = new JTextField();
            GridBagConstraints gbc_txtY = new GridBagConstraints();
            gbc_txtY.insets = new Insets(0, 0, 5, 0);
            gbc_txtY.fill = GridBagConstraints.HORIZONTAL;
            gbc_txtY.gridx = 1;
            gbc_txtY.gridy = 1;
            contentPanel.add(txtY, gbc_txtY);
            txtY.setColumns(10);
        }
        {
            Component verticalStrut = Box.createVerticalStrut(20);
            GridBagConstraints gbc_verticalStrut = new GridBagConstraints();
            gbc_verticalStrut.insets = new Insets(0, 0, 5, 0);
            gbc_verticalStrut.gridx = 1;
            gbc_verticalStrut.gridy = 5;
            contentPanel.add(verticalStrut, gbc_verticalStrut);
        }
        {
            
            GridBagConstraints gbc_btnOuterColor = new GridBagConstraints();
            gbc_btnOuterColor.insets = new Insets(0, 0, 5, 0);
            gbc_btnOuterColor.gridx = 1;
            gbc_btnOuterColor.gridy = 6;
            contentPanel.add(btnOuterColor, gbc_btnOuterColor);
        }
        {
            
            GridBagConstraints gbc_btnInnerColor = new GridBagConstraints();
            gbc_btnInnerColor.gridx = 1;
            gbc_btnInnerColor.gridy = 7;
            contentPanel.add(btnInnerColor, gbc_btnInnerColor);
        }
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("OK");
                okButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        try {
                            int x=Integer.parseInt(txtX.getText());
                            int y=Integer.parseInt(txtY.getText());
                            validInput=true;
                        }catch(Exception exc){
                            JOptionPane.showMessageDialog(null, "Invalid values");
                        }
                    }
                });
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
            }
            {
                JButton cancelButton = new JButton("Cancel");
                cancelButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        validInput=false;
                        setVisible(false);
                    }
                });
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
            }
        }
    }
    
    public DlgShape(String title,Shape shape) {
        this();
        setTitle(title);
        setShape(shape);
        
        if(shape instanceof Point) {
            txtX.setText(String.valueOf(((Point)shape).getX()));
            txtY.setText(String.valueOf(((Point)shape).getY()));
            btnInnerColor.setVisible(false);
            btnOuterColor.setText("color");
            
        }else if(shape instanceof Line) {
            txtX.setText(String.valueOf(((Line)shape).getStartPoint().getX()));
            txtY.setText(String.valueOf(((Line)shape).getStartPoint().getY()));
            btnInnerColor.setVisible(false);
            btnOuterColor.setText("color");
        }else if(shape instanceof Rectangle) {
            txtX.setText(String.valueOf(((Rectangle)shape).getUpperLeftPoint().getX()));
            txtY.setText(String.valueOf(((Rectangle)shape).getUpperLeftPoint().getY()));
            
        }else if(shape instanceof Circle || shape instanceof Donut) {
            txtX.setText(String.valueOf(((Circle)shape).getCenter().getX()));
            txtY.setText(String.valueOf(((Circle)shape).getCenter().getY()));
        }
        
    }

}

和 DlgRectangle 的代碼,以及 DlgRectangle1,但我們稱它為 DlgRectangle:

public class DlgRectangle1 extends DlgShape {

    private final JPanel contentPanel = new JPanel();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            DlgRectangle1 dialog = new DlgRectangle1();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public DlgRectangle1() {
        super();
        //this.getContentPane().add(new JLabel("widht"));
        
        
        
        JLabel lblWidth = new JLabel("width");
        GridBagConstraints gbc_lblWidth = new GridBagConstraints();
        gbc_lblWidth.anchor = GridBagConstraints.EAST;
        gbc_lblWidth.insets = new Insets(0, 0, 5, 5);
        gbc_lblWidth.gridx = 0;
        gbc_lblWidth.gridy = 2;
        contentPanel.add(lblWidth, gbc_lblWidth);
        
    }
    public DlgRectangle1(String title,Rectangle rectangle) {
        super(title,rectangle);
    }

}

我只需要將 DlgShape 中的屬性從私有切換為公共或受保護。

所以

private JTextField txtX;
private JTextField txtY;
private JButton btnOuterColor = new JButton("outer color");
private JButton btnInnerColor = new JButton("inner color");

應該

public JTextField txtX;
public JTextField txtY;
public JButton btnOuterColor = new JButton("outer color");
public JButton btnInnerColor = new JButton("inner color");

將 go 歸功於@Bohemian 的評論

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM