0

I set it to visible, do everything that needs to be done such as size and layout , and it won't show up when i click run. Anyone know why? I have looked on other view classes i have done in the past and dont see anything different

package model;

import java.awt.*;
import javax.swing.*;
import java.lang.reflect.Method;

@SuppressWarnings("serial")
public class View extends JFrame {
    private static final int FRAME_WIDTH = 1000;
    private static final int FRAME_HEIGHT = 800;
    private static final int FRAME_X_ORIGIN = 250;
    private static final int FRAME_Y_ORIGIN = 250;

    private JButton solveButton;

    private static Controller myController;
    public View(Controller controller)
    {
        myController = controller;
        this.setVisible(true);
        this.setLayout(null);
        this.setSize(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
        Container contentPane;
        this.setTitle("NQueens");

        this.setResizable(false);
        this.setBackground(Color.WHITE);

        solveButton = new JButton("Solve");
        solveButton.setBounds(500,460,80,80);
        this.add(solveButton);
        this.setVisible(true);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 3
    1) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). 2) Don't set the size of top level containers. Instead layout the content & call `pack()`. – Andrew Thompson Nov 07 '13 at 02:22
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Nov 07 '13 at 02:32
  • 1
    Works perfectly fine for me. Try removing the first `setVisible` statement and add `setLocationRelativeTo(null);` before the second... – MadProgrammer Nov 07 '13 at 02:37
  • Where's the `main`? What's constructing `View`? – MadProgrammer Nov 07 '13 at 02:42

2 Answers2

0

You're doing this.setSize(FRAME_X_ORIGIN, FRAME_Y_ORIGIN); Don't you mean this.setSize(FRAME_WIDTH, FRAME_HEIGHT); ? The way you've got it, your "solve" button will be off the edges of your frame.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
0

You need to call the parent constructor. Just add

super("My Window Title");

at the top of the constructor. And you are having two calls to setVisible(...) method. Remove the foremost one. It should be like

public View(Controller controller)
{
    super("NQueens");
    myController = controller;

    this.setLayout(null);
    this.setSize(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
    Container contentPane;

    this.setResizable(false);
    this.setBackground(Color.WHITE);

    solveButton = new JButton("Solve");
    solveButton.setBounds(500,460,80,80);
    this.add(solveButton);
    this.setVisible(true);
}

Hope this helps.

Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
  • @user2909132 Where's your main method? – Sri Harsha Chilakapati Nov 07 '13 at 02:31
  • @MadProgrammer What I didn't? Could you please explain a little bit? – Sri Harsha Chilakapati Nov 07 '13 at 02:37
  • You don't "need" to call `super("My Window Title");`, it makes no difference, the OP is using `setTitle` any way. The OP's code works fine for me (albit removing the first `setVisible` makes it work better) – MadProgrammer Nov 07 '13 at 02:39
  • @MadProgrammer My lecturer said that if the constructor isn't called, the display won't be created correctly on some systems. We're taught that we must call the parent constructor unless we need to change the default behaviour. – Sri Harsha Chilakapati Nov 07 '13 at 02:41
  • @SriHarshaChilakapati So call `super()`? There's no *need* to use `super(String)` in this case - granted, it's easier – MadProgrammer Nov 07 '13 at 02:48
  • @MadProgrammer That's true. I removed the setTitle method call instead and added the string to the constructor. – Sri Harsha Chilakapati Nov 07 '13 at 02:54
  • 1
    @SriHarshaChilakapati If you don't put an explicit call to either of the superclass constructors, the compiler acts as if you had just written `super()`, which has exactly the same effect as writing `super("")`. I feel that to tell you you _must_ call the parent constructor is very misleading. – Dawood ibn Kareem Nov 07 '13 at 04:14