GUI Interface



Share on Google+Share on Google+

Hina Saeed
GUI Interface
3 Answer(s)      9 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:
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
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  how interface support multiple inheritance in java
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  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  What is marker interface ?? what is its use in java programming?? is this us in programming ??Explain is implementation with code
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
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
Convert the code to GUI
GUI code  GUI code
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
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
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
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


Advertisement null