|
Post by hicaduda on Jul 28, 2014 23:22:17 GMT -5
In my last tutorial, I taught you how to create a window. In this tutorial, we are going to use that window and add icons to it. Icons are things that you would find in a basic software. Buttons, text boxes, check boxes, and drop boxes are all examples of icons.
import javax.swing.JFrame;
public class Main extends JFrame {
public void Main() { setSize(500, 400); setTitle("Test"); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true);
}
public static void main(String[] args) { new Main(); } This is the code we ended with last. We are now going to extends onto this code. Now an easy way to import all the of the icon classes, including JFrame, since JFrame is in the same location as them all, we can change 'import javax.swing.JFrame;' to 'import javax.swing.*;' Now that we've done that, we can begin. Underneath where we set all the properties for the window, we are going to get all of our icons initialized and ready. This tutorial we are only going to add a button. Before we set up the button, we are going to need something to put it on. That is where a JPanel comes in handy. All you have to do is simply create an instance and then add it to the JFrame.
JPanel p = new JPanel(); add(p);
Under where we set up the JPanel, we are going to make an instance of JButton. In between the parenthesis, we are going to type what the text of the button will be.
JButton b = new JButton("Button"); Now all you have to do is do as you did for the JPanel, except where the JButton is the JPanel, and the JPanel is the JFrame.
JPanel p = new JPanel(); add(p); JButton b = new JButton("Button"); p.add(b); Now you have that down, you can run your program and you should have a button with the word Button inside of it. Now the button doesn't do anything yet, but that is what we will be covering next tutorial.
|
|