twitter



Pertma buat file baru,boleh menggunakan jcreator ataupun menggunakan notepad++ dan save file tersebut dengan nama Calc.java


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//import fungsi swing
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;

import javax.swing.KeyStroke;
//class utama
public class Calc extends JFrame
{
final int MAX_INPUT_LENGTH = 20;
final int INPUT_MODE = 0;
    final int RESULT_MODE = 1;
    final int ERROR_MODE = 2;
    int displayMode;
boolean clearOnNextDigit, percent;
double lastNumber;
String lastOperator;
private JMenu jmenuFile;
private JMenuItem jmenuitemExit,jmenuitemAbout;
private JLabel jlbOutput;
private JButton jbnButtons[];
private JPanel jplMaster,jplBackSpace,jplControl;
Font f12 =new Font("Times New Roman", 0, 12);
Font f121 =new Font("Times New Roman", 1, 12);
public Calc()
{
jmenuFile = new JMenu("file");
jmenuFile.setFont(f121);
jmenuFile.setMnemonic(KeyEvent.VK_F);
jmenuitemExit = new  JMenuItem("Exit");
jmenuitemExit.setFont(f12);
jmenuitemExit.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_X,ActionEvent.CTRL_MASK ));
jmenuFile.add(jmenuitemExit);
JMenuBar mb = new JMenuBar();
mb.add(jmenuFile);
setJMenuBar(mb);
setBackground(Color.gray);
jplMaster = new JPanel();
jlbOutput = new JLabel("0");
jlbOutput.setHorizontalTextPosition(JLabel.RIGHT);
jlbOutput.setBackground(Color.WHITE);
jlbOutput.setOpaque(true);
getContentPane().add(jlbOutput, BorderLayout.NORTH);
jbnButtons = new JButton[23];
JPanel jplButtons = new JPanel();
for (int i=0; i<=9; i++)
{
jbnButtons[i] = new JButton(String.valueOf(i));
}
jbnButtons[10] = new JButton("+/-");
jbnButtons[11] = new JButton(".");
jbnButtons[12] = new JButton("*");
jbnButtons[13] = new JButton("/");
jbnButtons[14] = new JButton("*");
jbnButtons[15] = new JButton("-");
jbnButtons[16] = new JButton("+");
jbnButtons[17] = new JButton("sqrt");
jbnButtons[18] = new JButton("1/x");
jbnButtons[19] = new JButton("%");
jplBackSpace = new JPanel();
jplBackSpace.setLayout(new GridLayout(1, 1, 2, 2));
jbnButtons[20] = new JButton(" <-- BackSpace");
jplBackSpace.add(jbnButtons[20]);
jplControl = new JPanel();
jplControl.setLayout(new GridLayout(1, 2, 2,2));
jbnButtons[21] = new JButton(" CE ");
jbnButtons[22]  = new JButton("C");
jplControl.add(jbnButtons[21]);
jplControl.add(jbnButtons[22]);
for (int i=0; i<jbnButtons.length; i++){   
jbnButtons[i].setFont(f12);        
if (i<10)
jbnButtons[i].setForeground(Color.magenta);                  
else
jbnButtons[1].setForeground(Color.blue);
}
jplButtons.setLayout(new GridLayout(4, 5, 2, 2));
for (int i=7; i<=9; i++)
{
jplButtons.add(jbnButtons[i]);
}
jplButtons.add(jbnButtons[13]);
jplButtons.add(jbnButtons[17]);
for(int i=4; i<=6; i++)
{
jplButtons.add(jbnButtons[i]);
}
jplButtons.add(jbnButtons[14]);
jplButtons.add(jbnButtons[18]);
for(int i=1; i<=3; i++)
{
jplButtons.add(jbnButtons[i]);
}
jplButtons.add(jbnButtons[15]);
jplButtons.add(jbnButtons[19]);
jplButtons.add(jbnButtons[0]);
jplButtons.add(jbnButtons[10]);
jplButtons.add(jbnButtons[11]);
jplButtons.add(jbnButtons[16]);
jplButtons.add(jbnButtons[12]);
jplMaster.setLayout(new BorderLayout());
jplMaster.add(jplBackSpace, BorderLayout.WEST);
jplMaster.add(jplControl, BorderLayout.EAST);
jplMaster.add(jplButtons, BorderLayout.SOUTH);
getContentPane().add(jplMaster, BorderLayout.SOUTH);
requestFocus();
addWindowListener(new WindowAdapter()
{
public void windowClosed(WindowAdapter e)
{
System.exit(0);
}
}
);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public  static  void main(String args[]){
Calc calci = new Calc();
Container contentPane = calci.getContentPane();
calci.setTitle("Java Swing Calcculator");
calci.setSize(241, 217);
calci.pack();
calci.setLocation(400, 250);
calci.setVisible(true);
calci.setVisible(false);
}
}
Selamat mencoba…
Note : program ini baru tampilanya saja,blom bisa di input. Jadi tunngu aja kelanjutanya…

1 komentar:

  1. kok Tampilannya setelah di Run langsung hilang,,,,,
    Kurang apa ya....????

Posting Komentar