Hina Saeed
GUI Interface
3 Answer(s)      5 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);
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 Pages:
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
GUI Alternatives
, it isn't difficult to build a Graphical User Interface (GUI) in Java, but it is hard... the GUI interface processing from the logic. This is easy to do by putting the interface and logic in separate classes. Some GUI generators below help
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
Rationale for GUI tutorial decisions
of programs that have no GUI interface. For example, Java is very popular... Java GUI interface. Objection: Covering GUIs means leaving something... choice. Java's first attempt at a GUI interface was called the Abstract
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
Java: Basic GUI
Java NotesBasic GUI Next - Big Blob This first example just shows what the user interface will look like. The following examples use this same user interface. This first example has no listeners, so it does nothing. Main
GUI - Swing vs. AWT
Java: GUI - Swing vs. AWT The original graphical user interface (GUI... and extendability problems with AWT were resolved by introducing a new GUI interface, known... and AWT Mixing both Swing and AWT components in the same interface can produce
Fill-in: Rainfall GUI
79 80 81 82 83 84 // RainfallGUI2.java - Provides a GUI interface... Java NotesFill-in: Rainfall GUI Name ______________________________ (5 points) Make a drawing of approximately what the window that this generates looks
bank account gui
) All of the methods are provided by the interface Transaction. 4.Add the interface...; Transaction>(); I already done with the GUI i just need the code to make the button
Component gui - Java Beginners
Component gui  Can you give me an example of Dialog in java Graphical user interface?  Hi friend, import javax.swing.*; public class DialogPaneDemo { public static void main(String[] args
Rainfall GUI Questions
46 // RainfallGUI.java - Provides a GUI interface to the RainfallStats class... Java NotesRainfall GUI Questions Name ______________________________ This is part of the code to implement the GUI that was used with the Rainfall
Bank account gui
of the methods are provided by the interface Transaction. 4. Add the interface
GUI - Java Beginners
GUI  whats wrong with my code failed to compile // Department interface //import java.sql.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; class EmployeeInformation extends JFrame implements
Java GUI problem with linklists and administration - Java Beginners
Java GUI problem with linklists and administration  Hi, I'm having trouble making a GUI interface in java, where a list of names are displayed using a jList. The java program should be able to allow the user to add names, remove
IDE for JSP GUI design - JSP-Servlet
IDE for JSP GUI design  Hello all, Can anyone suggest an application for designing JSP interface? I would like to know whether Adobe products like Adobe Flex or ColdFusion can be used for the purpose. Thanks. roshni
User Interface Toolkits
User Interface Toolkits User Interface Toolkits / Libraries are given below... to create GUI components like buttons, text fields, banners, menus, dialog... API also enable us for creating GUI components but the component build
interface
interface   what is the use of marking interface
Java Graphical user interface
with my home work Task 1: GUI Design and Implementation The user requirements of your Java quiz GUI application are specified by the following program flow... of the GUI in first stage. Your program window should be located at the centre
Linux as a Command Line Interface (CLI) Vs. Graphics User Interface (GUI).
Linux as a Command Line Interface (CLI) Vs. Graphics User Interface (GUI... a command line interface (CLI) to the operating system, while graphical shells provide a graphical user interface (GUI). A graphical user interface (GUI) is a human
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
User Interface Design
Java NotesUser Interface Design Links Sun's Java Look and Feel Design... to start. AskTog - www.asktog.com User Interface Design for Programmers - Joel... GUI Bloopers Designing Visual Interfaces - Mullet and Sano
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
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  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
Example - Calc GUI
Java: Example - Calc GUI Example - Calc Main, Example - Calc GUI, Example - Calc Extensions This is the source for the graphical user interface... GUI @author Fred Swartz @version 2004-04-20 Rodenbach */ import java.awt.
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
Java GUI to build a Student Registration Program
Java GUI to build a Student Registration Program   Write a program to register students for a college. Students have names, addresses and courses. Implement the interface class RegisterStudent. RegisterStudent has one method
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 Issues
on the interface. This idea is easy, and I don't think the students have much... to convince these traditional programmers to simply build the interface, then go... asleep? It used to bother me that the main program would build the GUI, then exit
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 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
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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.