Writing a GUI program

Writing a GUI program

View Answers

November 21, 2009 at 2:55 PM

Hi Friend,

Try the following code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.regex.*;

class GuiApplication extends JFrame {
JLabel label1, label2;
JTextField text;
JTextArea area;
JScrollPane pane;
JPanel panel, panel1, panel2;
JButton button1, button2;

GuiApplication() {
panel = new JPanel(new GridLayout(3, 2));
panel1 = new JPanel(new GridLayout(3, 2));
panel2 = new JPanel(new GridLayout(3, 2));
label1 = new JLabel("Enter number");
label2 = new JLabel("Result is");
text = new JTextField(15);
area = new JTextArea(2, 2);
pane = new JScrollPane(area);
button1 = new JButton("Check");
button2 = new JButton("Prime Factors");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String value = text.getText();
int i = 0;
Pattern p = Pattern.compile("[A-Z,a-z]");
Matcher m = p.matcher(value);
if (m.find()) {
JOptionPane.showMessageDialog(null,
"Please enter only numbers");
} else {
int num = Integer.parseInt(value);
for (i = 2; i < num; i++) {
int n = num % i;
if (n == 0) {
area.setText("Entered number is not Prime!");
break;
}
}
if (i == num) {
area.setText("Number is Prime!");
}
}
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String value = text.getText();
Pattern p = Pattern.compile("[A-Z,a-z]");
Matcher m = p.matcher(value);
if (m.find()) {
JOptionPane.showMessageDialog(null,
"Please enter only numbers");
} else {
int num = Integer.parseInt(value);
int perfectNo = 0;
String perfect = "";
area.setText("Factors are:");
for (int i = 1; i < num; i++) {
if (num % i == 0) {
perfectNo += i;
perfect += Integer.toString(i) + " ";

System.out.println(i);
}
}
area.setText(perfect);
}
}
});
panel1.add(label1);
panel1.add(text);
panel1.add(button1);
panel1.add(button2);
panel2.add(label2);
panel2.add(pane);
panel.add(panel1, BorderLayout.NORTH);
panel.add(panel2, BorderLayout.SOUTH);

add(panel);

}

public static void main(String[] args) {
GuiApplication app = new GuiApplication();
app.setVisible(true);
app.pack();
}
}

Thanks









Related Tutorials/Questions & Answers:
Writing a GUI program - Java Beginners
Writing a GUI program  Hello everyone! I'm trying to write a program... if that number is prime or not. When the second button is pressed the program... to write the code for the GUI. Could anyone please help?  Hi Friend
GUI application program
GUI application program  Write a GUI application program that meets the following requirements: Create an array with 100 randomly chosen integers. Create a textfield to enter an array index and another textfield to display
Advertisements
writing program - Java Beginners
writing program  WAP to input Name of Cricketer, Number of matches, total runs scored for 250 players from the user and print data in descending order of their average runs using bubble sort. Average runs=total runs divided
writing angularjs hello world program
tutorials. I want to make simple program and thinking to start from the Hello World program in AngularJS. Searching for simple example for writing angularjs...writing angularjs hello world program  Hi, I just started to learn
writing program in bluej - Java Beginners
writing program in bluej  How to write the program on follwing WAP to inpout a string and print Input : I stidied Hard   Hi Friend, Try the following: import java.util.*; public class UserInput{ public
Need help writing a console program
Need help writing a console program  I need help cant seems to figure it out! Write a program that consists of three classes. The first class will be the actual program. The second class will simply convert a string to lower
java GUI program - Java Beginners
java GUI program  java program that creates the following GUI, when user enter data in the textfield, the input will be displayed in the textarea.   Hi friend, import javax.swing.*; import
Java GUI Program - Java Beginners
Java GUI Program  How is the following program supposed to be coded? Write an application that prompts the user to enter the radius of a circle by Day 7 under a thread in your Team Forum called Week Three Program for 10 points
want a program for cd writing in java - Java Beginners
want a program for cd writing in java  Hi Can u tell some body, doing a program on cd writing in java. I m facing some problem on it. Thanks in advance. Regards sanjaya
Java gui program for drawing rectangle and circle
Java gui program for drawing rectangle and circle   how to write java gui program for drawing rectangle and circle? there shoud be circle.... and the program must also show message dialog "this is a red circle" when click
need help on writing a program. - Java Beginners
Writing first Java Program  Hi, I need help to write my first Java Program other then Hello World! Thanks!!  Hi Thanks for choosing roseIndia to get your query solved. check given link to see the solution of your Java
Need in desperate help in writing a console program
Need in desperate help in writing a console program  Write a console program that repeatedly prompts the user to enter data until they type done (any case, Upper, Lower, or Mixed). As they enter the data, assign it to a two
Need in desperate help in writing a console program
Need in desperate help in writing a console program  Write a console program that repeatedly prompts the user to enter data until they type done (any case, Upper, Lower, or Mixed). As they enter the data, assign it to a two
Need in desperate help in writing a console program
Need in desperate help in writing a console program  Write a console program that repeatedly prompts the user to enter data until they type done (any case, Upper, Lower, or Mixed). As they enter the data, assign it to a two
java program for writing xml file - Java Beginners
java program for writing xml file  Good morning i want to write values from my database(one table)into one xml file. Like i have 3 coloumns in my table along with their values,so when click some button i need to generate one
GUI convert to celsius program - Java Beginners
GUI convert to celsius program  how to write java GUI program to convert Fahrenheit to Celsius that need user to input value of Fahrenheit then click button convert and value of Celsius will display as a output.   Hi
Java GUI to build a Student Registration Program
Java GUI to build a Student Registration Program   Write a program... in sorted order by last name. Demonstrate the correct operation of your program by creating a driver program to load the student data base and the course data
I really need a tutor for Java program that has to do with multithreading and gui!
I really need a tutor for Java program that has to do with multithreading and gui!  I am looking for a Tutor to help me with a Java program, specially GUI and multithreading. If you can tutor, please email me
without writing web.xml file we can write servlet program
without writing web.xml file we can write servlet program  Sir Morning... Sir I have one Question "without writing web.xml file we can write servlet program". if yes which way? if no why? but without use Annotation........ Plz
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.  Hello, I would like to know how to write a program to read data from a file. Then use the numerical data within
I am writing a program which converts a string to a timestamp and add days to it
I am writing a program which converts a string to a timestamp and add days to it  java.sql.Timestamp ts2 = java.sql.Timestamp.valueOf("2012-01-11 12:34:39"); long l=ts2.getTime() +(100060602424);//add 24 days Date d=new Date(l
Having a hard time writing program to calculate test scores..........
Having a hard time writing program to calculate test scores..........  i need a program that does this:Standardized Test Scores A local high... to have your program open the data file, count the number of elements in the file
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.  Hello, I would like to know how to write a program to read data from a file. Then use the numerical data within
GUI
GUI  How to GUI in Net-beans ... ??   Please visit the following link: http://www.roseindia.net/java/java-tips/background/30java_tools/netbeans.shtml
GUI
GUI  Write a GUI application for the WebBuy Company that allows a user to compose the three parts of a complete email message: the â??To:â??, â??Subject:â?? and â??Message:â?? text. The â??To:â??, and â??Subject:â?? Text areas
How can I write a GUI stack program by Java to push Image files instead of other data type items?
How can I write a GUI stack program by Java to push Image files instead of other data type items?   How can I write a GUI stack program by Java to push Image files instead of other data type items? for the push instruction I
Writing Simple Java Script Program
Writing Simple Java Script Program   ... of JavaScript and create your first JavaScript program. Creating your first... java script program. To run the program open the html file in any browser
i written the program in the files but in adding whole file is writing once again - Java Beginners
i written the program in the files but in adding whole file is writing once again  Very Urgent: Write a small record management application file and it should not be re-written for every add/delete operation.  Hi
GUI problem
GUI problem  Create a class called CDProgram and write a GUI program to compute the amount of a certificate of deposit on maturity. The sample data follows: Amount deposited: 80000.00 Years: 15 Interest Rate: 7.75 Hint
GUI framework
GUI framework  what do u mean by GUI framework
writing characters
writing characters  how to write a group of characters to a string ... i mean for writing single character using charAt in java
ModuleNotFoundError: No module named 'gui'
ModuleNotFoundError: No module named 'gui'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'gui' How to remove the ModuleNotFoundError: No module named 'gui' error
GUI component
GUI component  How can a GUI component handle its own events
Convert the code to GUI
GUI code  GUI code
Magic Matrix in GUI
Magic Matrix in GUI  I want program in java GUI contain magic matrix for numbers
Ghost Writing
Ghost Writing Ghost Writing is the kind of writing when someone... of writing it. So ghostwriter is a person who works behind the scene on a writing project in return of certain amount. The write-ups of the writer
gui question
gui question  design a gui application for me and write its code in which the user enters a no. in a textfield and onn clicking the button the sum of the digits of the no. should be displayed. hint: suppose the user enters 12
java gui
java gui   friends... good day.. i have doubt in java gui. ? i created 1 java gui application. That has two text fields jtext1,jtext2. case: user entered value in first textfield(jtext1) and pressed the enter key . the cursor
Convert the code to GUI
GUI Example  GUI Example code to learn
Convert the code to GUI
GUI Application example  GUI Application example
Blog Writing
Blog Writing A blog, the short form of web log is an online user... on that. But the professionals are a better choice for writing the blog for commercial or promotional purpose. We provides the services of writing blogs for the commercial
Version of gj-gui>gj-gui dependency
List of Version of gj-gui>gj-gui dependency
Java GUI
Java GUI  1) Using Java GUI, create a rectangular box that changes color each time a user click a change color button. 2) Modify Question 1 to include a new button named insert image, that allow user to insert a bitmap image
Writing Actions
Writing Actions The Action is responsible for controlling of data flow within an application. You can make any java class as action. But struts have some built in interface and class for making action easily. To make an Action class
Convert the code to GUI
Java GUI Class Example  Java GUI Class Example
Convert the code to GUI
GUI Java JSP application  GUI Java JSP application
Convert the code to GUI
GUI Application Development   GUI Application Development
Convert the code to GUI
Java and GUI application Example  Java and GUI application Example
Convert the code to GUI
Write a GUI Application  best way to write a GUI based application
Convert the code to GUI
How to Convert the code to GUI   How to convert a code into GUI

Ads