简体   繁体   English

java 对话框 inheritance 是如何工作的? 我失去了一些财产

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

Sorry I don't have much knowledge about Java or swing applications.对不起,我对 Java 或 swing 应用程序了解不多。 I created a dialog called DlgShape and in it i have 2 text fields, 2 buttons and 2 labels.我创建了一个名为 DlgShape 的对话框,其中有 2 个文本字段、2 个按钮和 2 个标签。 I tried creating DlgRectangle and instead of it inheriting from JDialog I inherited from DlgShape.我尝试创建 DlgRectangle 而不是继承自 JDialog 我继承自 DlgShape。 The design of the parent and child class are identical but in the DlgRectangle I don't have the labels, buttons and text fields listed out as properties, I have public get and set methods and getContentPane.父子 class 的设计是相同的,但在 DlgRectangle 中,我没有将标签、按钮和文本字段列为属性,我有公共获取和设置方法以及 getContentPane。 Suddenly my layout of the DlgRectangle changed from GridBagLayout to BorderLayout, why did it do that?突然我的 DlgRectangle 布局从 GridBagLayout 变成了 BorderLayout,为什么会这样? I have the default DlgShape constructor set the layout to GridBagLayout and my DlgRectangle constructor also calls the super constructor.我有默认的 DlgShape 构造函数将布局设置为 GridBagLayout,我的 DlgRectangle 构造函数也调用超级构造函数。

I tried manually typing the code to create a label and put it in the content panel but it doesn't appear.我尝试手动输入代码以创建 label 并将其放入内容面板,但它没有出现。 I had a thought that maybe I need to get the content panel to be a property of DlgShape and maybe make it protected.我有一个想法,也许我需要让内容面板成为 DlgShape 的属性,并可能使其受到保护。

Here's my code for DlgShape: package drawing;这是我的 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()));
        }
        
    }

}

and code for DlgRectangle, well DlgRectangle1 but lets call it DlgRectangle:和 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);
    }

}

I just needed to switch my properties in DlgShape from private to public or protected.我只需要将 DlgShape 中的属性从私有切换为公共或受保护。

So所以

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

should be应该

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

Credits go to the comment by @Bohemian将 go 归功于@Bohemian 的评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM