Skip to main content

Posts

Showing posts with the label Swing

Java Swing class hierarchy

import javax.swing.JFrame; import javax.swing.JLabel; //import statements //Check if window closes automatically. Otherwise add suitable code public class HelloWorldFrame extends JFrame { public static void main(String args[]) { new HelloWorldFrame(); } HelloWorldFrame() { JLabel jlbHelloWorld = new JLabel(" Hello World "); add(jlbHelloWorld); this .setSize(100, 100); // pack(); setVisible( true ); } }

Draw Lines in java

In this example we are going to draw  Lines. the Lines can be drawn with draw Lines(). In this we are using Graphics class and Applet class. void draw Line( int start x, int start y, int end x, int end y ); This method is used to draw Line Here draw Line(); use to create Line. int start x, int start y denote the starting point of line same as int end x, int end y denote the finishing point of line. Here first we are importing two packages. Then after we have write applet tag into comments. In this tag we have pass three arguments code ,height  and width. The code attribute is used to call the applet class. And Height , width is used for Sizing the window. Then we have create a class and extends this class with Applet Class to accurse the property of applet. We had override paint method of applet class. Then we pass the value of starting point int start x, int start y(0,0) and then pass the value of end point int end x, int end y(100,100).   import java.awt.*; import ...

Java GUI Tutorial – Part 2

Introduction This tutorial will teach you about Graphical User Interfaces, or GUI’s. GUI’s are important because it’s an effective way for the user to interact with the program. The way Java allows you to create GUI’s is the Abstract Windowing Toolkit, or AWT. Up until now you’ve only learned how to make a nice looking GUI, what you’ll learn now will teach you how to make them actually *DO* stuff! You do this using the java.awt.event.* package. Well, time to start learning events! Setting Up Your Program To receive events, first you have to include some packages and an interface or two. An interface is something which adds to your programs functionality. First, you must import the java.awt.event.* package: import java.awt.event.*; Next, you need to include one of the EventListener interfaces. This applet includes the ActionListener interface: public class MyApplet extends java.applet.Applet implements ActionListener { The EventListener interfaces enable a component of a graphi...

Java GUI Tutorial – Part 1

Introduction This tutorial will teach you about Graphical User Interfaces, or GUI’s. GUI’s are important because it’s an effective way for the user to interact with the program. The way Java allows you to create GUI’s is the Abstract Windowing Toolkit, or AWT. The java.awt package includes: Ø Buttons, checkboxes, labels, other basic components Ø Text fields, text areas, and other more complex components Ø Dialog boxes and other windows Ø Drop down boxes and other menus This tutorial is divided into two parts – The first one (which you’re reading right now) will teach you about the different AWT components. It will teach you what they are, how to use them, give a complete working example of it (located in the Part 1 folder in the Code directory), and give a screenshot of what it looks like. The second one will teach you about all the events that these different objects have. You’ll learn how to set up event handling, how to detect which object the event belongs to, and more. Wel...