Java对话的继承如何工作?我失去了一些属性
抱歉,我对Java或摇摆应用没有太多了解。我创建了一个名为DLGShape的对话框,其中有2个文本字段,2个按钮和2个标签。我尝试创建DLGRECTANGLE,而不是从DLGShape继承的Jdialog继承。父母和子类的设计是相同的,但是在DLGRECTANGLE中,我没有标签,按钮和文本字段列出为属性,我有公开获取和设置方法和getContentpane。突然,我的DLGRECTANGLE布局从Gridbaglayout变为Borderlayout,为什么这样做?我的默认DLGSHAPE构造函数将布局设置为GridBaglayout,而我的DLGrectangle构造函数也称为Super构造函数。
我尝试手动键入代码以创建标签并将其放入内容面板中,但不会出现。我以为,也许我需要将内容面板成为DLGSHAPE的属性,并可能受到保护。
这是我的dlgshape代码: 包绘图;
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);
}
}
Sorry I don't have much knowledge about Java or swing applications. I created a dialog called DlgShape and in it i have 2 text fields, 2 buttons and 2 labels. I tried creating DlgRectangle and instead of it inheriting from JDialog I inherited from 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. Suddenly my layout of the DlgRectangle changed from GridBagLayout to BorderLayout, why did it do that? I have the default DlgShape constructor set the layout to GridBagLayout and my DlgRectangle constructor also calls the super constructor.
I tried manually typing the code to create a label and put it in the content panel but it doesn't appear. I had a thought that maybe I need to get the content panel to be a property of DlgShape and maybe make it protected.
Here's my code for DlgShape:
package drawing;
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:
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只需要将DLGShape中的属性从私人转换为公共或受保护。
所以
应该
学分 @bohemian的评论
I just needed to switch my properties in DlgShape from private to public or protected.
So
should be
Credits go to the comment by @Bohemian