Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (407)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Quick Swing Question  (Read 279 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1

Senior Member


Medals: 3
Projects: 1



« Posted 2013-01-29 23:30:59 »

I went off and started messing around and it seems that I messed up somehow. I'm attempting to place a JTextArea onto a JPanel as I usually do but it's not showing up; does anyone see where I messed up?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
import javax.swing.*;
import java.awt.*;

class Window extends JFrame
{
   private Insets insets;
   
   public Window()
   {
      JFrame mainFrame = new JFrame();
      mainFrame.setSize(1024, 786);
      mainFrame.setLocationRelativeTo(null); //Centers the window on the users screen.
     mainFrame.setTitle("Temp Name");
      mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
      mainFrame.setResizable(false);
      insets = mainFrame.getInsets();
      mainFrame.setLayout(null); //Makes it so you can manually positon everything on the JPanel using XY coords.
     JPanel mainPanel = new JPanel(); //Creates a new JPanel to put everything on.
     mainFrame.getContentPane().add(mainPanel);
     
      TextArea gameTextArea = new TextArea();
      gameTextArea.setEditable(false);
      gameTextArea.setBounds(50, 30, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
     mainPanel.add(gameTextArea);
     
      mainFrame.setVisible(true);
   }
}


Example: http://i.imgur.com/RSB3HkV.png

I've probably just forgotten a statement or something but I'm not sure.
Offline BoBear2681

JGO Coder


Medals: 18



« Reply #1 - Posted 2013-01-29 23:41:44 »

You set the layout of the JFrame to null, when you probably meant to set the content pane's layout to null.  This works:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public Window() {
      JFrame mainFrame = new JFrame();
      mainFrame.setSize(1024, 786);
      mainFrame.setLocationRelativeTo(null); //Centers the window on the users screen.
     mainFrame.setTitle("Temp Name");
      mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
      mainFrame.setResizable(false);
      insets = mainFrame.getInsets();
      mainFrame.getContentPane().setLayout(null);
     
      JTextArea gameTextArea = new JTextArea();
      gameTextArea.setEditable(false);
      gameTextArea.setBounds(50, 30, 100, 20); //X position, Y Position, Length in pixels, Height in pixels.
     mainFrame.getContentPane().add(gameTextArea);
     
      mainFrame.setVisible(true);
}


Standard Swing-fanatic disclaimer:  It's really preferable to use a LayoutManager rather than null layout, as then your UI could be designed to allow the window to be resizable and still look nice, not to mention handling different LookAndFeels having different widget sizes, the user's desktop settings, etc.
Online GabrielBailey74

JGO Knight


Medals: 8
Projects: 6


<3 Java


« Reply #2 - Posted 2013-01-29 23:41:57 »

I managed to get it working.

If you create a JPanel and add it to the Window before adding it's components, it might not go smooth (I don't know).

The issue here was that you were calling 'mainFrame.setLayout(null)', I changed that to 'mainPanel.setLayout(null)'.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
   public Window()
   {
      JFrame mainFrame = new JFrame();
      mainFrame.setSize(1024, 786);
      mainFrame.setLocationRelativeTo(null); //Centers the window on the users screen.
     mainFrame.setTitle("Temp Name");
      mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
      mainFrame.setResizable(false);
      insets = mainFrame.getInsets();

      JPanel mainPanel = new JPanel(); //Creates a new JPanel to put everything on.
     mainPanel.setLayout(null);

     
      TextArea gameTextArea = new TextArea();
      gameTextArea.setEditable(false);
      gameTextArea.setBounds(50, 30, 100, 20);
      mainPanel.add(gameTextArea);
   
      mainFrame.add(mainPanel);
      mainFrame.setVisible(true);
   }


Cheers mate.

EDIT: I was literally 1 second too late Tongue, either will work.

Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline tyeeeee1

Senior Member


Medals: 3
Projects: 1



« Reply #3 - Posted 2013-01-29 23:45:47 »

Thanks, it's all working now. ^.^
Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (88 views)
2013-05-17 21:29:12

alaslipknot (96 views)
2013-05-16 21:24:48

gouessej (128 views)
2013-05-16 00:53:38

gouessej (123 views)
2013-05-16 00:17:58

theagentd (131 views)
2013-05-15 15:01:13

theagentd (119 views)
2013-05-15 15:00:54

StreetDoggy (161 views)
2013-05-14 15:56:26

kutucuk (184 views)
2013-05-12 17:10:36

kutucuk (185 views)
2013-05-12 15:36:09

UnluckyDevil (191 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.199 seconds with 20 queries.