Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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 492 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1
« 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.
Offline GabrielBailey74
« 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!
Legends of Yore - The Casual Retro Roguelike
Offline tyeeeee1
« Reply #3 - Posted 2013-01-29 23:45:47 »

Thanks, it's all working now. ^.^
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

ctomni231 (37 views)
2014-07-18 06:55:21

Zero Volt (35 views)
2014-07-17 23:47:54

danieldean (28 views)
2014-07-17 23:41:23

MustardPeter (31 views)
2014-07-16 23:30:00

Cero (46 views)
2014-07-16 00:42:17

Riven (47 views)
2014-07-14 18:02:53

OpenGLShaders (36 views)
2014-07-14 16:23:47

Riven (36 views)
2014-07-14 11:51:35

quew8 (32 views)
2014-07-13 13:57:52

SHC (68 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!