-3

Well in the middle of designing my text based game I decided to take a route of making a GUI in windows builder, note that I just started learning GUI's so go easy on me.

Anyways the issue is on the start up of the program when the window that appears which is suppose to be the GUI is actually blank and doesn't allow me to exit so I have to terminate the program in eclipse.

Also note when executing it in the main startup, its executed as mainGameGUI, not under a constructor.

Here is the code.

private static JFrame frame;
private static JTextField txtBeginByTyping;

public static void mainGameGUI() {
    frame = new JFrame();
    frame.setBounds(100, 100, 600, 700);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);

    final JTextArea textArea = new JTextArea();
    textArea.setBounds(10, 415, 371, 205);
    textArea.setRows(8);
    textArea.setEditable(false);
    frame.getContentPane().add(textArea);

    JProgressBar progressBar = new JProgressBar();
    progressBar.setBackground(Color.RED);
    progressBar.setToolTipText("");
    progressBar.setForeground(Color.RED);
    progressBar.setValue(Player.currentHp);
    progressBar.setMaximum(Player.maxHp);
    progressBar.setBounds(401, 517, 173, 40);
    frame.getContentPane().add(progressBar);

    JLabel lblYourHealth = new JLabel("Health");
    lblYourHealth.setHorizontalAlignment(SwingConstants.CENTER);
    lblYourHealth.setFont(new Font("Trajan Pro", Font.PLAIN, 16));
    lblYourHealth.setBounds(401, 470, 173, 36);
    frame.getContentPane().add(lblYourHealth);

    JLabel lblDestructivePower = new JLabel("Destruction: " + Player.totalDamage);
    lblDestructivePower.setHorizontalAlignment(SwingConstants.LEFT);
    lblDestructivePower.setFont(new Font("Trajan Pro", Font.PLAIN, 16));
    lblDestructivePower.setBounds(401, 568, 173, 40);
    frame.getContentPane().add(lblDestructivePower);

    JLabel lblNewLabel = new JLabel("Gold Coins: " + Player.currency);
    lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);
    lblNewLabel.setFont(new Font("Trajan Pro", Font.PLAIN, 16));
    lblNewLabel.setBounds(401, 619, 173, 32);
    frame.getContentPane().add(lblNewLabel);

    JLabel lblNewLabel_1 = new JLabel("Realm of the Gods");
    lblNewLabel_1.setBounds(10, 11, 564, 60);
    frame.getContentPane().add(lblNewLabel_1);

    JLabel lblNewLabel_2 = new JLabel("Images go here");
    lblNewLabel_2.setBounds(10, 82, 564, 322);
    frame.getContentPane().add(lblNewLabel_2);

    txtBeginByTyping = new JTextField();
    txtBeginByTyping.setText("");
    txtBeginByTyping.setBounds(10, 631, 381, 20);
    frame.getContentPane().add(txtBeginByTyping);
    txtBeginByTyping.setColumns(10);
    txtBeginByTyping.addActionListener(new ActionListener()
    {

        public void actionPerformed(ActionEvent e) 
        {
            inputReader.input = txtBeginByTyping.getText();
            textArea.append(inputReader.input + "\n");
            txtBeginByTyping.setText("");
        }

    });
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane.setBounds(380, 415, 11, 205);
    frame.getContentPane().add(scrollPane);

    JLabel lblNewLabel_3 = new JLabel("Town of Neo");
    lblNewLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
    lblNewLabel_3.setFont(new Font("Trajan Pro", Font.PLAIN, 16));
    lblNewLabel_3.setBounds(401, 416, 173, 40);
    frame.getContentPane().add(lblNewLabel_3);
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Abszol
  • 57
  • 3
  • 11
  • 1
    Your descriptions sounds like a classic Swing threading issue, bug I don't see that you've posted the pertinent code to explain this. What exactly is this "inputReader" thing? I will bet your a 6 pack of your favorite beverage that it's this object that is tying up your GUI. Otherwise, I agree with the secondary recommendations made by Braj, 1+ to his answer. – Hovercraft Full Of Eels Jun 01 '14 at 20:05
  • Almost but its actually just a method I created, but I also agree with braj – Abszol Jun 01 '14 at 21:00
  • Are you going to show us this method or not? Come on now, don't leave us hanging.... like you did with your [last question](http://stackoverflow.com/questions/23916855/how-can-a-program-close-a-gui-without-the-program-ending). – Hovercraft Full Of Eels Jun 01 '14 at 21:04
  • 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). – Andrew Thompson Jun 02 '14 at 02:15

1 Answers1

4

Call frame.setVisible(true); in the end after adding all the components in it.

Some points:

  1. Never use null layout at all instead use proper Layout Manager.

    Read more A Visual Guide to Layout Managers where all the layout manger are described in detail along with sample code.

  2. Use SwingUtilities.invokeLater() to make sure that EDT is initialized properly.

    Read more

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76