简体   繁体   中英

design question for java SWING app

Note: This is for a SWING course I am taking.

I have an assignment to make a simple graphics package (draw circles, squares, etc).

I was thinking of having multiple dialog boxes for entering the shape parameters, ie:

Point has x,y Circle has x,y,radius Rectangle has x,y,width,height etc.

I was thinking of creating a super dialog class with X,Y and extending it to allow for Width,Height or Radius etc.

For example, the rectangleDialog would invoke the super constructor with the additional parameters required:

public abstract class XYDialog extends JFrame {
   public XYDialog(PARAMETERS ... params) {
       // build the dialog by iterating through PARAMETERS
   }
}


public class RectangleDialog extends XYDialog {
  public RectangleDialog() {
    super(PARAMETERS.WIDTH, PARAMETERS.HEIGHT);
  }
}

then the super class is responsible for building the GUI

Does this seem like a reasonable approach? Does this make sense?

Thanks

Yes, I think it's a good solution. But, as stated before, reconsider the naming of your classes. If you extend a JFrame, call it SomethingFrame. If PARAMETERS is a normal class, it should not be in capitals.

I would also suggest extending JPanel instead of JFrame, and let the one instatiating these classes determine if to put them in a JFrame or a JDialog. A JFrame creates a whole new window, and you normally only have one main window for your application, whereas dialogs and panels are created on the fly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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