GUI Interface

GUI Interface

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);
textfield.setHorizontalAlignment(JTextField.RIGHT);
textfield.setFont(BIGGER_FONT);
//--- Clear button
JButton clearButton = new JButton("Clear");
clearButton.setFont(BIGGER_FONT);
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(""));
}else{
JButton b = new JButton(keyTop);
b.addActionListener(numListener);
b.setFont(BIGGER_FONT);
buttonPanel.add(b);
}
}
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]);
b.addActionListener(opListener);
b.setFont(BIGGER_FONT);
opPanel.add(b);
}
//--- 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.setContentPane(con);
this.setTitle("Simple Calculator example");
this.setResizable(false);
}//end constructor
private void actionMethod() {
m_startNumber = true;
textfield.setText("0");
str = "=";
m_logic.setTotal("0");
}

June 9, 2008 at 5:02 PM

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

} catch (NumberFormatException ex) {
actionMethod();
textfield.setText("Error");
}
str = e.getActionCommand();
}//endif m_startNumber
}//endmethod
}//end class
class NumListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String digit = e.getActionCommand();
if (m_startNumber) {
textfield.setText(digit);
m_startNumber = false;
} else {
textfield.setText(textfield.getText() + digit);
}
}
}
class ClearListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
actionMethod();
}
}
}
JFrame window = new CalcGUI();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
window.setSize(300,300);
window.setMinimumSize(true);
window.setMaximumSize(true);

}
}

June 9, 2008 at 5:03 PM

CalcLogic.java

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.

http://www.roseindia.net/java/

Thanks.









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 addCourse.java) 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
Advertisements
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
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
interface
interface   what is the use of marking interface
interface.
interface.  Write short note on interface.   Please visit the following link: Java Interface
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
INTERFACE  how interface support multiple inheritance in java
interface
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
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
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
interface  develop a library interface which has drawbook(),returnbook()(with fine),checkstatus() and reservebook() methods.all the methods tagged with public
interface
interface  What is marker interface ?? what is its use in java programming?? is this us in programming ??Explain is implementation with code
interface
interface  develop a library interface which has drawbook(),returnbook()(with fine),checkstatus() and reservebook() methods.all the methods tagged with public
Interface
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
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
Interface  I need to implement the interface without overriding its method and also don't use abstract class for this. How to do
interface
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
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
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
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 Application example  GUI Application example
Convert the code to GUI
GUI Example  GUI Example code to learn
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
What is an interface?
What is an interface?  What is an interface
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
Write a GUI Application  best way to write a GUI based application
Convert the code to GUI
Java and GUI application Example  Java and GUI application Example
Convert the code to GUI
How to Convert the code to GUI   How to convert a code into GUI
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
Can an Interface implement another Interface?
Can an Interface implement another Interface?   Hi, Can an Interface implement another Interface? Thanks
Can an Interface extend another Interface?
Can an Interface extend another Interface?   Hi, Can an Interface extend another Interface? thanks
GUI problem
GUI problem  How do I make a Jbutton which is shaped like a circle. This button needs to be clicked in order to change color.   import java.awt.*; import java.awt.geom.*; import javax.swing.*; public class
ServletRequest interface
ServletRequest interface   What is new in ServletRequest interface
Netbeans GUI Ribbon
Netbeans GUI Ribbon  how to create ribbon task in java GUI using netbeans
Convert the code to GUI
Java Code to GUI   can any one convert My code to GUI code
Convert the code to GUI
How to create GUI application in Java   How to create GUI application in Java
Map interface
Map interface  What is the Map interface
Java interface
Java interface  What is the List interface
List interface
List interface  What is the List interface

Ads