0% found this document useful (0 votes)
12 views2 pages

Kigitadarifitaduma

KigitadarifitaDuma

Uploaded by

tankwamaxwell
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Kigitadarifitaduma

KigitadarifitaDuma

Uploaded by

tankwamaxwell
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Gui programming in java using netbeans pdf

Java gui netbeans. How to create gui in java netbeans. How to use netbeans for java gui. Gui programming in java examples. Java gui project ideas. Netbeans gui tutorial. Contributed by Saleem Gul and Tomas Pavek This beginner tutorial is about creating a simple graphical user interface (GUI) with back-end functionality using Swing forms. We
will show how to code button and field behavior in a GUI and work through the design of a simple calculator application. The text fields will be used for receiving user input and displaying program output, while buttons initiate specific functionalities. The first step is to create an IDE project named NumberAddition. Then, we need to create a Java
container using JFrame component. In this step, we will populate our application's front end with JPanel, add JLabels, JTextFields, and JButtons, and customize the panel's border. (Note: The rest of the article remains unchanged) In this step, we will rename the display text for the components recently added to the JFrame. Double-click on jLabel1,
change the "text" property to "First Number:", and repeat this process for jLabel2 and jLabel3, renaming them to "Second Number:" and "Result:" respectively. To align the labels to the right, expand the width of the two shorter labels so that they are all equal in size, then open the Properties dialog for each label and change the "Horizontal
Alignment" property to "RIGHT". Remove any sample text from jTextField1, making it editable by right-clicking and selecting "Edit Text" (if needed). Repeat this process for jTextField2 and jTextField3. Rename the display text of jButton1 to "Clear", and similarly rename the texts of jButton2 to "Add" and jButton3 to "Exit". The finished GUI should
now resemble the screenshot provided. Next, we will give functionality to the Add, Clear, and Exit buttons. The jTextField1 and jTextField2 fields will be used for user input, while jTextField3 will display program output - effectively creating a simple calculator. To begin, assign an event handler to each button using ActionListener, responding to
ActionEvent. Right-click on the Exit button, select "Events" > "Action" > "actionPerformed", and note that many more events can be responded to. In the Source Code window, we will implement the action we want the button to perform when pressed (either by mouse click or via keyboard). The TODO line should now read as follows: private void
jButton3ActionPerformed(java.awt.event.ActionEvent evt) { //TODO add your handling code here: }. We will replace this line with System.exit(0);. Click the Design tab at the top of your work area to return to Form Design. Right-click on the Clear button (jButton1), select "Events" > "Action" > "actionPerformed", and we will have the Clear button
erase all text from the jTextFields. To achieve this, we will add code as above, with our finished source code reading: private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){ jTextField1.setText(""); jTextField2.setText(""); jTextField3.setText(""); }. This code changes the text in all three of our JTextFields to nothing, effectively
overwriting any existing Text with a blank. Here's how our button will work: It'll take input from two text fields (jTextField1 and jTextField2), convert those strings into floating-point numbers, add them together, and then put the result back into a string to display in another text field (jTextField3). To make this happen, we need to do three things.
First, define some float variables: num1, num2, and result. Then, parse the input from those text fields into floats using Float.parseFloat(). Next, add the two numbers together and store the result. Finally, convert that result back into a string and display it in jTextField3. To make this work, we'll need to add some code to our button's action event. So,
let's go back to the Form Design, right-click our Add button (jButton2), select Events > Action > actionPerformed, and add the following code: ```java private void jButton2ActionPerformed(java.awt.event.ActionEvent evt){ // Define float variables float num1, num2, result; // Parse text to floats num1 = Float.parseFloat(jTextField1.getText()); num2 =
Float.parseFloat(jTextField2.getText()); // Perform addition result = num1 + num2; // Convert result to string and display in jTextField3 jTextField3.setText(String.valueOf(result)); } ``` Now that we have our code, let's run the program. To do this from within the IDE, just choose Run > Run Project (Number Addition) or press F6. If you get a window
asking you to set your main class, select my.NumberAddition.NumberAdditionUI and click OK. To run the program outside of the IDE, first clean and build our project by choosing Run > Clean and Build Main Project (Shift-F11). Then, navigate to the NumberAddition/dist directory using your file explorer or manager. Double-click the
NumberAddition.jar file to launch the application. Alternatively, you can also launch the application from the command line. To do this, open up a command prompt or terminal window, change directories to the NumberAddition/dist directory, and then type the following statement: java -jar NumberAddition.jar Now, explore the menu without selecting
anything; alternatively, go to Window/IDE Tools and select Properties. In the Properties window, click the Events tab. Here, you can view and edit event handlers associated with the currently active GUI component. You can respond to various events like key presses, mouse clicks, and window size changes by generating event handlers from the
Events menu. The most common event is an Action event. To understand how event handling works, select an event from the Event menu, and the IDE will automatically create an event listener for you and link it to your component. Let's go through the steps to see how this works. Go back to NumberAdditionUI.java in the Editor and click the Source
tab. Scroll down to the methods jButton1ActionPerformed(), jButton2ActionPerformed(), and jButton3ActionPerformed() that you just implemented. These are event handlers. Next, scroll up to a method called initComponents(). If you don't see it, look for a line saying "Generated Code" and expand it by clicking the + sign next to it. Note the blue
block around the initComponents() method, which is auto-generated code that you can't edit. Browse through the method and find the code that initializes and places your GUI components on the form. In initComponents(), scroll down to where it reads jButton3.setText("Exit"); jButton3.addActionListener(new java.awt.event.ActionListener() { public
void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); This is where an event listener object is added to the GUI component. The ActionListener interface has an actionPerformed method taking an ActionEvent object, which is implemented by calling your jButton3ActionPerformed event handler. The button is now
listening for action events. Every time it's pressed, an ActionEvent is generated and passed to the listener's actionPerformed method, which executes the code you provided in the event handler. In general, each interactive GUI component needs to register with an event listener and implement an event handler. NetBeans IDE handles hooking up the
event listener for you, so you can focus on implementing the actual business logic that should be triggered by the event.

You might also like