1

I'm new to java, I would like some advice from you:

  • I would insert in setPreferredSize() the width and the height of the manager class, without using the "evil" statics, can someone tell me the smartest thing to do in this case?

  • is it possible to not create a constructor in manager class and pass the width and height always respecting the OOP's flexibility classes?

  • I was wrong to write add(Panel.getPanel()), it is correct? it respects the OOP?

Tell me where am I wrong in this code or what to fix, I want to learn,thanks you.

public class MainFrame extends JFrame {

   public static void main(String[] args) {
          final MainFrame mainFrame = new MainFrame();
          mainFrame.setVisible(true);
    }

   public MainFrame() {
    initFrame();
    }

   private void initFrame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    add(Panel.getPanel());
    setResizable(false);
    setUndecorated(true);
    pack();
    setLocationRelativeTo(null);

   }
 }


public class Panel extends JPanel  {

    public Panel() {
    setPreferredSize(new Dimension(/*?????????*/));
    }                   
}



 public class Manager  {
      private int width = 700;
      private int height = 700;
          // Some unrelated code follows


      public int getHeight(){
          return height;
       }
       public int getWidth(){
          return width;
       }

 }
nachokk
  • 14,363
  • 4
  • 24
  • 53
OiRc
  • 1,602
  • 4
  • 21
  • 60
  • 1
    1) [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi). 2) Is there a reason your extending `JPanel` or `JFrame` you are not adding functionality, 3) In java methods name starts with lower-case following a camel-case style by convention. 4) Your variables in Manager could be `final` – nachokk Mar 14 '14 at 14:25
  • @nachokk please tell me what method doesn't follow the java convention? all methods in my program follows camel-case convenction , for example: getHeight(),anyway, in the panel i added a keylistener and mouselistener, the frame has the functionality to start the application as you can see it has a main method. – OiRc Mar 14 '14 at 14:41
  • This questions may be better fit at http://codereview.stackexchange.com/ – WOUNDEDStevenJones Mar 14 '14 at 14:44

1 Answers1

3

I would insert in setPreferredSize() the width and the height of the manager class, without using the "evil" statics, can someone tell me the smartest thing to do in this case?

Read this, should help Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? . In general let the layout-manager to lead with this.

is it possible to not create a constructor in manager class and pass the width and height always respecting the OOP's flexibility classes?

Yep, make them class variable(static) and also final, this means

Option 1

 //check class name
 public class Manager  {
      public static final int width = 700;
      public static final int height = 700;
 }

Option 2,

 public class Manager  {
      private static int width = 700; //could be final
      private static int height = 700; // could be final
          // Some unrelated code follows


      public static int getHeight(){
         //you can make some algorithm in this way
          return height;
       }
       public static int getWidth(){
         //you can make some algorithm in this way
          return width;
       }

 }

Option 3: Define an Enum

public class Manager {

 public enum Dimension{
    WIDTH(700),
    HEIGHT(700);
   private final Integer value;

   Dimension(Integer value){
      this.value=value;
   }

   public Integer getValue(){
       return value;
   }
 }

}

I was wrong to write add(Panel.getPanel()), it is correct? it respects the OOP?

No, it isn't correct unless getPanel() is a static method. You should create an instance of your JPanel

 JPanel panel = new Panel();
 add(panel);

Also you are using inheritance in a bad way. You are subclassing but you are not adding any new functionality, and if you decide to subclass don't use Panel cause it's the name is like java.awt.Panel and it's very confusing.

Read more in oracle tutorials

Community
  • 1
  • 1
nachokk
  • 14,363
  • 4
  • 24
  • 53
  • i' m so happy, i ' ve response in a very linear way :D – OiRc Mar 14 '14 at 14:42
  • i don' t paste all my code, my interest was in this thing,that you explained well, i added listeners to the panel. – OiRc Mar 14 '14 at 14:59