Share on Google+Share on Google+

Hina Saeed
GUI Interface
3 Answer(s)      8 years ago
Posted in : Java Beginners


View Answers

June 9, 2008 at 5:01 PM

Hi Hina,

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.Component;

public class Calc {
public static void main(String[] args) {
System.out.println("This is simple calculator program.");
class CalcGUI extends JFrame {
private final Font BIGGER_FONT = new Font("monspaced", Font.PLAIN, 20);
private JTextField textfield;
private boolean m_startNumber = true;
private String str = "=";
private CalcLogic m_logic = new CalcLogic();
public CalcGUI() {
//--- Display field
textfield = new JTextField("0", 12);
//--- Clear button
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(new ClearListener());
//--- One listener for all numeric keys.
ActionListener numListener = new NumListener();
String buttonOrder = "7894561230.";
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(5, 3, 5, 5));
for (int i = 0; i < buttonOrder.length(); i++) {
String keyTop = buttonOrder.substring(i, i+1);
if(keyTop.equals(" ")){
buttonPanel.add(new JLabel(""));
JButton b = new JButton(keyTop);
ActionListener opListener = new OpListener();
JPanel opPanel = new JPanel();
opPanel.setLayout(new GridLayout(5, 1, 5, 5));
String[] opOrder = {"+", "-", "*", "/", "="};
for (int i = 0; i < opOrder.length; i++) {
JButton b = new JButton(opOrder[i]);
//--- Layout the top-level panel.
JPanel con = new JPanel();
con.setLayout(new BorderLayout(5, 5));
con.add(textfield, BorderLayout.NORTH );
con.add(buttonPanel, BorderLayout.CENTER);
con.add(opPanel, BorderLayout.EAST );
con.add(clearButton, BorderLayout.SOUTH );
//--- Finish building the window (JFrame)
this.setTitle("Simple Calculator example");
}//end constructor
private void actionMethod() {
m_startNumber = true;
str = "=";

June 9, 2008 at 5:02 PM

class OpListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (m_startNumber) {
textfield.setText("ERROR - No operator");
} else {
m_startNumber = true;
try {
String displayText = textfield.getText();
if(str.equals("=")) {
} else if(str.equals("+")) {
} else if(str.equals("-")) {
} else if(str.equals("*")) {
} else if(str.equals("/")) {
textfield.setText("" + m_logic.getTotalString());

} catch (NumberFormatException ex) {
str = e.getActionCommand();
}//endif m_startNumber
}//end class
class NumListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String digit = e.getActionCommand();
if (m_startNumber) {
m_startNumber = false;
} else {
textfield.setText(textfield.getText() + digit);
class ClearListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFrame window = new CalcGUI();


June 9, 2008 at 5:03 PM

public class CalcLogic {
//-- Instance variables.
private int total;
public CalcLogic() {
total = 0;
public String getTotalString() {
return ""+total;
public void setTotal(String n) {
total = convertToNumber(n);
public void add(String n) {
total += convertToNumber(n);
public void subtract(String n) {
total -= convertToNumber(n);
public void multiply(String n) {
total *= convertToNumber(n);
public void divide(String n) {
total /= convertToNumber(n);
private int convertToNumber(String n) {
return Integer.parseInt(n);


Read for more information.


Related Tutorials/Questions & Answers:
Gui Interface - Java Beginners
Gui Interface  hi I have two seperate interfaces in the same projects . my question is how can I access this interface using a jbutton (i.e jbutton = next where next points to another interface What would
GUI Interface - Java Beginners
GUI Interface  Respected sir, please send me the codimg of basic calculator,functionality must be include additon ,subtraction,division and multiplication. But use classes javax swing java awt java awt.event no other
GUI Interface .Reply me with in hour..Its Urgent - Java Beginners
GUI Interface .Reply me with in hour..Its Urgent  Hi ... Now i am... have to create GUI Interace ..How should i create the Interface. In Existing... a GUI interface with some 6 to 8 Vote symbols and should integrate with the already
GUI  How to GUI in Net-beans ... ??   Please visit the following link:
interface   what is the use of marking interface
interface.  Write short note on interface.   Please visit the following link: Java Interface
interface  will the interface implements a interface   Hi Friend, No. Interface can extends another interface but cannot implements it, because interface will not contain the implementation. Thanks
interface   Hi I have interface in that interface 3 methods are there , after some days client said that,i want to add one more method in that interface ,so how can add 4 method so that the implemented class did not affect
INTERFACE  how interface support multiple inheritance in java
interface  can we extend interface?   Hi Friend, Yes an interface can be extended by other interface like using extends keyword interface A{ } interface B extends A{ } For Example: interface IntefaceA { void
interface  what the use of interface?   An interface is one... variables.Any class can implement(inherit)the interface and make use... is achieved by using the interface (by implementing more than one interface at a time
interface  develop a library interface which has drawbook(),returnbook()(with fine),checkstatus() and reservebook() methods.all the methods tagged with public
interface  develop a library interface which has drawbook(),returnbook()(with fine),checkstatus() and reservebook() methods.all the methods tagged with public
interface  What is marker interface ?? what is its use in java programming?? is this us in programming ??Explain is implementation with code
for Interface in java? and want to know why they used interface instead of multiple inheritance? Thanks in advance   An interface is one which has abstract... implement(inherit)the interface and make use of the methods(functions
interface  why do we need interface in java..if it`s usefull for to obtain multiple inheritance in the sense how it gonna be achieved...and i can...)...the y we need interface...THis is question often i heard from my developer
Interface  I need to implement the interface without overriding its method and also don't use abstract class for this. How to do
interface  What is the exact use of interface in real time scenario? some people says that interface provides multiple inheritance. Is it true...; Through interface, we can achieve the multiple inheritance. Java does
Interface  1.Create an interface names ShapeInterface that has two... class a.Rectangle that uses the interface b.Circle that uses the interface and abstract class   interface ShapeInterface{ int l=0,b=0; public
Interface  1.Create an interface names ShapeInterface that has two... class a.Rectangle that uses the interface b.Circle that uses the interface and abstract class   interface ShapeInterface{ int l=0,b=0; public
Interface  Declare an Interface called property containting a method compute price to compute &return the price.The inerface is to be implemented by follwaing two class (1)Bungalow&(2)Flat both the lasses have following
GUI framework
GUI framework  what do u mean by GUI framework
GUI component
GUI component  How can a GUI component handle its own events
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 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 code  GUI code
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
Convert the code to GUI
GUI Application example  GUI Application example
Convert the code to GUI
GUI Example  GUI Example code to learn
gui question
gui question  design a gui application and write code to accept a string from the user in a textfeild and print using option pane whether it is a palindrome or not. hint: abba is a palindrome   import java.awt.*; import
Java Spring Hibernate Struts Training Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? file location SASLError using PLAIN: not-authorized


Advertisement null