As you know, I've been working with Java for the past two years. Java was the first programming language that I've learned even though C++ was more popular five years ago. I eventually learned C++ and thought I had forgotten Java. But it has incorporated into my everyday life.
It was Core Java that I first learned. I don't remember if they taught me Swing. But I remembered learning about Applets. I know the powerfulness of Swing but never really had a chance to learn about it. Now that I have some spare time to expand my knowledge and intelligence, I want to learn about this Java Swing.
So, as usual, I go to Java Sun website. One thing I like about Sun is that they have pretty good tutorials. But then to make a fair statement, I have never really tried other open source programming languages.
In order to make a Java Swing program, we need javax.swing.* package. GUI programs mostly about looks. So, in order to make a swing program more user friendly and better display, we need a few components from java.awt.* packages as well.
As with any first program about a new programming language, I started off with HelloWorld java. Here's my program :
public class HelloWorldSwing {
public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI(){
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Hellow World");
JPanel contentPane = new JPanel();
contentPane.setBackground(Color.red);
contentPane.setSize(100, 500);
contentPane.add(label,BorderLayout.CENTER);
frame.add(contentPane);
frame.pack();
frame.setVisible(true);
}
}
As you can see we need three components : JFrame, JPanel & JLabel. JFrame is the starting point of any Java Swing program. Creating a JFrame component is creating a window. It's the main container with other buttons such as close and minimize buttons. Then we create more components and add those to JFrame. In this program, we create a JLabel components for labels.
In order to add JLabel to JFrame, we need to call content pane. Content Pane exists inside JPanel. But in order to customize the content pane, we need to create a JPanel. As you saw in the code, I customized my content pane to have red background color through JPanel. I added label to content pane, which is added to JFrame.
Saturday, December 12, 2009
Saturday, October 17, 2009
Java Virtual Machine
Every programmer talks about Java Virtual Machine (JVM) and JVM is everywhere. But most people who knows Java do not really know what a JVM is, how it is implemented and why it is very popular. I will try my best to explain what I understand about JVM in this post. All comments, suggestions and corrections are welcomed.
What is Java Virtual Machine?
Like the name said, it's a virtual machine. It's a machine that executes Java Codes. It's a light weight and portable program that comes with all Java download. If you ever used VMWare to run an OS which is different from the one installed in your machine, then it'll be easier for you to understand how JVM works. In VMWare, you don't need to install the other OS, you create a virtual environment in which the OS runs. Same concept is here, you don't need a certain hardware or programs for your Java programs to run. All you need is a JVM and all the different Java programs will run on your machine.
Virtual Machine Implementation
Java has become a very popular language because of its JVM. Java itself is a high level programming language. If you write a Java program and ask your computer to run, it won't understand what it means. There are only 0's and 1's in the lowest level of our implementation. So, there's a way to convert what programmers write to what machines understand and that is where compilers come in. Those converted codes are called machine codes. When a Java program is compiled, it is converted to bytecode. Bytecodes are machine independent and can be further compiled in machine codes. Because of its machine independence, a Java program can be run anywhere as long as you have Java Runtime Enviroment installed in your computer. JRE is a program that takes of Java bytecodes according to the machines. All JRE comes with JVM. Since programmers do not have to worry about different machines, they can focus more on their programs.
You must be familiar with Java Applets from different websites. Because of JVM, those developers don't have to worry about different browsers, OS and hardware when creating Java applets. That's why Java has been a popular programming language and JVM has been a popular environment.
What is Java Virtual Machine?
Like the name said, it's a virtual machine. It's a machine that executes Java Codes. It's a light weight and portable program that comes with all Java download. If you ever used VMWare to run an OS which is different from the one installed in your machine, then it'll be easier for you to understand how JVM works. In VMWare, you don't need to install the other OS, you create a virtual environment in which the OS runs. Same concept is here, you don't need a certain hardware or programs for your Java programs to run. All you need is a JVM and all the different Java programs will run on your machine.
Virtual Machine Implementation
Java has become a very popular language because of its JVM. Java itself is a high level programming language. If you write a Java program and ask your computer to run, it won't understand what it means. There are only 0's and 1's in the lowest level of our implementation. So, there's a way to convert what programmers write to what machines understand and that is where compilers come in. Those converted codes are called machine codes. When a Java program is compiled, it is converted to bytecode. Bytecodes are machine independent and can be further compiled in machine codes. Because of its machine independence, a Java program can be run anywhere as long as you have Java Runtime Enviroment installed in your computer. JRE is a program that takes of Java bytecodes according to the machines. All JRE comes with JVM. Since programmers do not have to worry about different machines, they can focus more on their programs.
You must be familiar with Java Applets from different websites. Because of JVM, those developers don't have to worry about different browsers, OS and hardware when creating Java applets. That's why Java has been a popular programming language and JVM has been a popular environment.
Thursday, October 15, 2009
My First Mac Laptop
After using Windows for more than 10 years, I got my first Mac laptop. It is 13.3 inch Mac Pro which comes with built in webcam and audio speakers.
I use Mac Mini at work and the OS was Mac OSX 10.5. Honestly, I am not a Mac fan. As much as I like iTouch and Apple's softwares, I'm not used to the way programs work. And it's hard to get some software for Mac OSx(e.g GTalk). In my country, GTalk is the only chat client that is permitted to use. All other messengers were blocked. So, I was a bit disappointed about not being able to use GTalk. But I just installed the Google voice for my gmail and am pleased about it. For those who are wondering how to use GTalk voice on Mac OS, you can check out http://www.google.com/chat/video.
Another program I have it installed on my laptop was VM Fusion. I installed Windows XP on it. I'm still a windows fan. I'll see if this changes after some time.
I use Mac Mini at work and the OS was Mac OSX 10.5. Honestly, I am not a Mac fan. As much as I like iTouch and Apple's softwares, I'm not used to the way programs work. And it's hard to get some software for Mac OSx(e.g GTalk). In my country, GTalk is the only chat client that is permitted to use. All other messengers were blocked. So, I was a bit disappointed about not being able to use GTalk. But I just installed the Google voice for my gmail and am pleased about it. For those who are wondering how to use GTalk voice on Mac OS, you can check out http://www.google.com/chat/video.
Another program I have it installed on my laptop was VM Fusion. I installed Windows XP on it. I'm still a windows fan. I'll see if this changes after some time.
Subscribe to:
Posts (Atom)