Results 1 to 6 of 6
- 06-24-2012, 04:34 PM #1
Member
- Join Date
- Apr 2012
- Location
- Den Haag, the Netherlands
- Posts
- 5
- Rep Power
- 0
JTextField on JPanel-class within JFrame-class
I'm having some difficulties showing my JTextFields in a JPanel class. This JPanel class is executed in a JFrame class.
I really can't see why they aren't there..
PS: The JFrame class does show the image from the Jpanel class.
Maybe some code says more than a thousand words ;)
This is the JPanel class with the JTextFields..
Java Code:(IMPORTS etc..) public class NaamInvoer extends JPanel implements ActionListener { private ImageIcon naamInvoerImg; private JTextField naamSpeler1; private JTextField naamSpeler2; private JTextField naamSpeler3; private JTextField naamSpeler4; private JButton volgende; public NaamInvoer() { this.setLayout(null); this.setSize(453, 436); this.setLocation(125, 160); this.setOpaque(false); this.naamSpeler1 = new JTextField(30); this.naamSpeler2 = new JTextField(30); this.naamSpeler3 = new JTextField(30); this.naamSpeler4 = new JTextField(30); this.naamSpeler1.setLocation(0, 0); add(naamSpeler1); add(naamSpeler2); add(naamSpeler3); add(naamSpeler4); this.naamInvoerImg = new ImageIcon(getClass().getResource("/images/naam invoeren2.png")); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(naamInvoerImg.getImage(), 0, 0, 453, 436, null); } public void actionPerformed(ActionEvent arg0) {} }
...And this is the JFrame class with that Jpanel class from above in it.
Java Code:(IMPORTS etc..) public class Opstart extends JFrame implements ActionListener, KeyListener { private GameBackground achtergrondPaneel; private Spel eenSpel; private ImageIcon systeemImage; private int answer; public Opstart() { achtergrondPaneel = new GameBackground(); systeemImage = new ImageIcon(Opstart.class.getResource("/images/icon.fw.png")); // eenSpel = new Spel(4); JPanel voerNaamIn = new NaamInvoer(); this.add(voerNaamIn); this.add(achtergrondPaneel); this.setJMenuBar(menuBar()); this.setTitle("Quixo"); this.setLocation(500,100); this.setSize(755, 755); this.setLayout(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); this.setVisible(true); this.setIconImage(systeemImage.getImage()); addKeyListener(this); } public static void main(String[] args) { new Opstart(); } }
-
Re: JTextField on JPanel-class within JFrame-class
The reason is because you're giving your JPanel a null layout and are not specifying your component location or size. So one solution is to call setBounds(...) on your component, but I'm going to recommend that you *not* do this, and instead I will recommend that you not use a null layout unless you've got a very good reason to do so. You don't. Instead read up on the layout managers and learn how to use them to their best effect. You won't regret doing this.
- 06-25-2012, 09:25 AM #3
Member
- Join Date
- Apr 2012
- Location
- Den Haag, the Netherlands
- Posts
- 5
- Rep Power
- 0
Re: JTextField on JPanel-class within JFrame-class
My good reason for using a null layout is that I want to position the jtextfields pixelperfect on the screen.
Howcome you are assuming I don't have a good reason for that?
- 06-25-2012, 09:50 AM #4
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
Re: JTextField on JPanel-class within JFrame-class
Hi floris,
This is more a case of miscommunication than an attempt to invalidate your reason. I understand what you are attempting to achieve and your intention for using a null layout.
Fubarable's advice is sound and it is worth researching the layout managers. The location of the textfield can be hardcoded instead but this is generally not advisable as this is effected by the resolution of the monitor you are viewing it on. What is seen as center on one resolution may appar bottom right on another.
Regards.
- 06-25-2012, 09:51 AM #5
Re: JTextField on JPanel-class within JFrame-class
You don't.Howcome you are assuming I don't have a good reason for that?
What happens when your program runs on a computer with settings that change the fonts of your GUI elements?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
-
Re: JTextField on JPanel-class within JFrame-class
Similar Threads
-
Painting things on a JPanel class from another class?
By Alerhau in forum New To JavaReplies: 2Last Post: 10-17-2011, 08:04 PM -
Problem Loading JTextField in JFrame / Problema al cargar JTextField en JFrame
By thor_inc in forum AWT / SwingReplies: 0Last Post: 08-30-2011, 09:18 AM -
Adding a jpanel to a customized Jpanel Class
By trishtren in forum AWT / SwingReplies: 7Last Post: 04-05-2011, 06:52 PM -
calling JPanel's JTextField from another JPanel class
By k2k in forum AWT / SwingReplies: 3Last Post: 04-20-2009, 11:31 PM -
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 09:34 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks