Java GUI Lesson 3 | JTextFields and Absolute Positioning
That's 101. Welcome to your third Java GUI tutorial! Today I'm gonna be talking about JLabels, which is another component, and positioning your components using absolute positioning.
So first I'm gonna go here, I'm gonna import java.awt.*;
and javax.swing.*;
Alright, and make this extend JFrame. The first thing I want to do is create two JTextFields. One is going to be where we can write things, and one is going to be uneditable; it's just gonna be there, kind of like a JLabel.
So, private static JTextField canChange; and private static JTextField cantChange. Yep, there we go! I didn't make a constructor. Went over all this in the last tutorial. And then I'm still gonna do canChange equals new JTextField, and it takes that string -- what you want to put in there, and that would be the default text that would be in there by default. Something doesn't have to be in there by default, but in canChange, I'm just gonna put "default text," and in cantChange I'm gonna put "I can't."
Alright, so the next thing we want to do is, because we're going to be positioning these using absolute positioning, we have to do this: we have to set layout to null. There's no layout! We're gonna be creating our own layout by setting the X and Y coordinates of everything.
By the way, the X and Y coordinates, the X and Y, is the top left corner of your thing. So if I’d like a rectangle and I did 0-0, that would make it the top-left corner. 0, 0 would be the top-left corner of my rectangle. X's start from zero and go up here, and Y starts from zero up here. I mean, X starts from zero and goes up, and Y starts from zero and increases as you go down. So it's kind of a little different than a regular graph.
Alright, so next thing I wanna do is make two dimensions. I'll call it Dimension size1 = canChange.getPreferredSize(); What this means is a dimension has things like .width and .height. If I get the preferred size of canChange, I can call like, it's kind of like its default width and height. This allows me to get like a default width and height for it, but I can code my own width and height.
I'll show how to do that in a sec. Dimension size2 = cantChange.getPreferredSize(); No, not what I want! So I get preferred size, and here's where we can actually -- the method to position with absolute is setBounds. Therefore, cantChange.setBounds(); This takes four things: the first one is the x-coordinate, second is the y-coordinate, third is the width, and the fourth is the height.
So let's say I'll put it at: alright, so this thing's gonna be at 350. I’ll put it like a hundred in, and like 20 down. And size1.getWidth() and size1.getHeight(). If I wanted to, I could just put a number in there, like 120, and then it would be that long; and like 30, and it would be that tall.
But I'm not, just letting you know, 'cause I just want to show you about the dimension thing. I setBounds, and I'll put that at: a hundred in, and like 55 down, and size2.getWidth() and size2.getHeight(). And I spelled width wrong; it’s fine.
The next thing we're gonna do is make it so cantChange you actually can't change 'cause right now this is normal. So I do cantChange.setEditable(), and that takes a boolean true or false, whether or not you're gonna edit it. I’ll make that editable false. I don’t have to do canChange.setEditable(true) because it's editable by default.
Now, I just have to add these: add(canChange); add(cantChange); and I guess I’ll just set the size and all that now: setSize(350, 300); setTitle(); and I’ll set that as JTextFields. SetDefaultCloseOperation(JFrame.EXIT_ON_CLOSE).
And yeah, that's about it. Now in here, I'm just gonna make the second object: secondX = new Second(); setVisible(true). Alright, so what we did is we made these two text fields, we set the default text for them, we said our layout to null so we can position them with X and Y coordinates as opposed to using a layout manager, which I’m not even going to go over because I never use them anyway.
It's just a bunch of different default layouts. The way you made these dimensions, so we can get like the default size for them, and then we use the setBounds method to create where they are, and just use the default width and height. Set editable to false so you can't change this one; you can change that one. We added them, set the size, set the title, and made it so it exits when you press X.
Here, we just put it in our main method so it can actually run. I will show you this right now: here we go! JTextFields "default text" and "can't change."
I'm clicking, and I'm unable to delete, and I cannot change it. This I can type anything I want in here, and this I can, and it closes when I press exit.
So that's all for your third Java GUI tutorial! Thank you for watching Mac Hits One and Walk 101! Subscribe, and goodbye!