简体   繁体   English

Java SWING应用程序的设计问题

[英]design question for java SWING app

Note: This is for a SWING course I am taking. 注意:这是我要参加的SWING课程。

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. 点具有x,y圆具有x,y,半径矩形具有x,y,宽度,高度等

I was thinking of creating a super dialog class with X,Y and extending it to allow for Width,Height or Radius etc. 我正在考虑使用X,Y创建一个超级对话框类,并将其扩展为允许Width,Height或Radius等。

For example, the rectangleDialog would invoke the super constructor with the additional parameters required: 例如,rectangularDialog将使用所需的其他参数调用超级构造函数:

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 然后超类负责构建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. 如果扩展JFrame,则将其称为SomethingFrame。 If PARAMETERS is a normal class, it should not be in capitals. 如果PARAMETERS是普通班级,则不应大写。

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. 我还建议扩展JPanel而不是JFrame,并让实例化这些类的人确定是否将它们放在JFrame或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. JFrame会创建一个全新的窗口,通常您的应用程序只有一个主窗口,而对话框和面板是动态创建的。

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

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