2

I'm pretty sure I've done it this way before, but for some reason, the JFrame won't show up when I run it.

    JLabel originalString =  new JLabel("Original String: " 
                                        + str.getMutator());
    JLabel currentString = new JLabel("Current String: " 
                                      + str.getMutator());
    JLabel finalString =  new JLabel("Final String: " + str.getTarget());

    JPanel panel = new JPanel();
    panel.add(originalString);
    panel.add(currentString);
    panel.add(finalString);

    JFrame frame = new JFrame("Mutating String!");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(panel);
    frame.pack();
    frame.setVisible(true);
}
skaffman
  • 398,947
  • 96
  • 818
  • 769
Dave
  • 21
  • 1
  • 1
  • 2

2 Answers2

5

Try to set size or check with the preferred size of your components probably because you call pack().

frame.setSize(x, y);

Sheng Chien
  • 519
  • 4
  • 7
3

Your problem must be somewhere else (is the method called does it throw an exception ?) because your code works (I commented the str calls) :

http://img217.imageshack.us/img217/902/screenvlg.png

import javax.swing.*;
public class Test{
    public static void main(String... args){
        JLabel originalString =  new JLabel("Original String: " /*+ str.getMutator()*/);
        JLabel currentString = new JLabel("Current String: "/* + str.getMutator()*/);
        JLabel finalString =  new JLabel("Final String: " /* + str.getTarget()*/);

        JPanel panel = new JPanel();
        panel.add(originalString);
        panel.add(currentString);
        panel.add(finalString);

        JFrame frame = new JFrame("Mutating String!");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);
    }
}
Colin Hebert
  • 91,525
  • 15
  • 160
  • 151
  • 1
    I found the issue. I feel a bit embarrassed about this, I had a Scanner reading in user input and forgot about it... – Dave Sep 27 '10 at 22:14
  • 2
    We've all been there, comrade :) The mere act of asking a question often leads to an answer. – Geoffrey Zheng Sep 28 '10 at 02:01
  • Dave could you explain? What does the scanner reading input have to do with the JFrame? Do you have to close the scanner before showing the frame? – jimboweb Feb 20 '18 at 16:49