0

I have two more classes that this main class is using, but I think that it can be answered without those classes because I think it is a logic problem.

I'm trying to create a JFrame that prints out a drawing. The way the API is set up, I want public Viewer to create the frame and set the title and then main to instantiate a viewer. But the way I have this set up keeps printing double the amount of frames that I need. Also, when I try to concatenate

v.setTitle(v.getTitle() + String.format(" pi = %.4f", pi));

it doesn't work. It just prints a new JFrame with just

String.format(" pi = %.4f", pi));

and the original JFrame prints its name (two separate frames). So I know it has to be a logic problem somewhere, but I can't figure it out.

import java.util.Scanner;
import javax.swing.JFrame;


public class Viewer extends JFrame{
    private ControlPanel cp;

    public Viewer(String name, int in){
          JFrame frame = new JFrame();
          frame.setSize(550, 550);
          frame.setTitle("Name: " + name);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          cp = new ControlPanel(in);    
          frame.add(cp);
          frame.setVisible(true);
    }


    /**
     * @param args
     */
    public static void main(String[] args) {
        int n = (int) 1e4;
        System.out.println("Enter the number of runs to make <1 to 4>:");
        @SuppressWarnings("resource")
        Scanner in = new Scanner(System.in);
        int runs = in.nextInt();
        if (runs > 4){
            runs = 4;
        }
        if (runs < 1){
            runs = 4;
        }
        for (int i = 1; i <= runs; i ++){
            n = n * runs;
            Viewer v = new Viewer("Trey Wilson", n);
            int hits = v.cp.getHits();
            double pi = 4.0 * hits / n;
            v.setTitle(v.getTitle() + String.format(" pi = %.4f", pi));
            v.pack();
            v.setVisible(true);
            v.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        }

    }
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Trey
  • 63
  • 1
  • 9
  • Why are you extending `JFrame` and creating a `JFrame` object? – Frakcool Sep 21 '16 at 23:29
  • You need to take away `frame.setVisible(true);` or `v.setVisible(true);`. I advice you take away `frame.setVisible(true);` but it all depends on what you want to do. – Young Emil Sep 21 '16 at 23:33
  • I'm completely new to Jframe (along with java) and was given an API to follow along with. What does extends mean? I'll give it a google, but didn't know if you had an answer on how to fix it. – Trey Sep 21 '16 at 23:33
  • `extends` means it inherits from `JFrame` class, it's a OOP concept – Frakcool Sep 21 '16 at 23:34
  • Ahhh I see. So instead of making a new frame, I can just automatically say setTitle(""). – Trey Sep 21 '16 at 23:36
  • I'm using the 'v.pack()' instead of setSize(), but the pack() function doesn't seem to be working. Of course setSize() will work since I can tell it however big to be, but how do I get pack() to work? – Trey Sep 21 '16 at 23:44
  • `pack()` works well if you have set the sizes or dimensions of your components that you are placing on the JFrame. – Young Emil Sep 21 '16 at 23:46
  • You have a lack of a lot of concepts of OOP, Java, etc, see [What does .pack() do?](http://stackoverflow.com/questions/22982295/what-does-pack-do) – Frakcool Sep 21 '16 at 23:46
  • Also see [Should I avoid the use of setPreferred|Maximum|Minimum size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) (Yes) – Frakcool Sep 21 '16 at 23:48

2 Answers2

2

Your code has multiple errors:

  1. It extends JFrame and creates a JFrame object (just remove the extends JFrame from the class, it's recommended to do so)

  2. Here lies your problem:

    Viewer v = new Viewer("Trey Wilson", n);
    

    this calls the Viewer's class constructor but the constructor itself sets it's own title, size, etc, and then you're modifying those attributes and making another call to setVisible(true) which makes this last frame visible again, so remove one or the other.

  3. You're not placing your program on the EDT, see SwingUtilities.invokeLater() why is it needed?

  4. See The use of multiple JFrames, Good / Bad Practice? (Bad) you should instead want to try using a single JFrame with multiple JPanels switching them with a Card Layout or using JDialogs to display / retrieve information to / from user

  5. From your comment above:

    I'm completely new to Jframe (along with java) and was given an API to follow along with. What does extends mean? I'll give it a google, but didn't know if you had an answer on how to fix it.

    I recommend you to learn the OOP basics, Java concepts first before going into Java Swing which will make your learning harder because it will add more complexity to it and make it harder to you to maintain or make a quality software.

Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
0

THIS SOLVES THE PROBLEM OF DOUBLE FRAME APPEARING:

So you can either take away frame.setVisible(true); in the constructor as in:

public Viewer(String name, int in){
      JFrame frame = new JFrame();
      frame.setSize(550, 550);
      frame.setTitle("Name: " + name);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      cp = new ControlPanel(in);    
      frame.add(cp);
}

OR Rather take away v.setVisible(true); from you main method.

I will advice you to take away frame.setVisible(true);. But depending on you requirement, choose the best for yourself.

Young Emil
  • 2,220
  • 2
  • 26
  • 37