0% found this document useful (0 votes)
49 views

Bram Keren

The document contains code for a Java application that counts and displays the number of different vehicle types (sedan, MPV, motorcycle) that pass by. It includes: 1) Buttons to increment and decrement counts for each vehicle type, and labels to display the current counts. 2) Anonymous inner classes to handle button click events and update the counts and labels. 3) A frame layout and main method to display the application.

Uploaded by

Bramantara Yudha
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Bram Keren

The document contains code for a Java application that counts and displays the number of different vehicle types (sedan, MPV, motorcycle) that pass by. It includes: 1) Buttons to increment and decrement counts for each vehicle type, and labels to display the current counts. 2) Anonymous inner classes to handle button click events and update the counts and labels. 3) A frame layout and main method to display the application.

Uploaded by

Bramantara Yudha
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

import javax.swing.

*;

import java.awt.*;

import java.awt.event.*;

public class TrafficCounter implements Runnable

{ private JLabel label1;

private JLabel label2;

private JLabel label3;

private int hitung1 = 0;

private int hitung2 = 0;

private int hitung3 = 0;

public void run()

{ JFrame frm1 = new JFrame("Aplikasi Hitung Kendaraan");

frm1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// class sedan

JButton b1 = new JButton("”Sedan”");

// anonymous class 1

ActionListener act1 = new ActionListener()

{ public void actionPerformed(ActionEvent ae)

{ hitung1++;

label1.setText(String.valueOf(hitung1));

}; // akhir a.c.1

b1.addActionListener(act1);
JButton b2 = new JButton("”Koreksi”");

// a.c.2

ActionListener act2 = new ActionListener()

{ public void actionPerformed(ActionEvent ae)

{ if (hitung1 == 0) hitung1 = 0;

else hitung1-=1;

label1.setText(String.valueOf(hitung1));

}; // akhir a.c.2

b2.addActionListener(act2);

label1 = new JLabel(String.valueOf(hitung1));

// class mpv

JButton b3 = new JButton("”MPV”");

// anonymous class 1

ActionListener act3 = new ActionListener()

{ public void actionPerformed(ActionEvent ae)

{ hitung2++;

label2.setText(String.valueOf(hitung2));

}; // akhir a.c.1

b3.addActionListener(act3);

JButton b4 = new JButton("”Koreksi”");

// a.c.2

ActionListener act4 = new ActionListener()

{ public void actionPerformed(ActionEvent ae)


{ if (hitung2 == 0) hitung2 = 0;

else hitung2-=1;

label2.setText(String.valueOf(hitung2));

}; // akhir a.c.2

b4.addActionListener(act4);

label2 = new JLabel(String.valueOf(hitung2));

// class motor

JButton b5 = new JButton("”Motor”");

// anonymous class 1

ActionListener act5 = new ActionListener()

{ public void actionPerformed(ActionEvent ae)

{ hitung3++;

label3.setText(String.valueOf(hitung3));

}; // akhir a.c.1

b5.addActionListener(act5);

JButton b6 = new JButton("”Koreksi”");

// a.c.2

ActionListener act6 = new ActionListener()

{ public void actionPerformed(ActionEvent ae)

{ if (hitung3 == 0) hitung3 = 0;

else hitung3-=1;

label3.setText(String.valueOf(hitung3));

}
}; // akhir a.c.2

b6.addActionListener(act6);

label3 = new JLabel(String.valueOf(hitung3));

Container kon1 = frm1.getContentPane();

LayoutManager lay1 = buatLayout();

kon1.setLayout(lay1);

kon1.add(b1);

kon1.add(b3);

kon1.add(b5);

kon1.add(label1);

kon1.add(label2);

kon1.add(label3);

kon1.add(b2);

kon1.add(b4);

kon1.add(b6);

frm1.setLocationRelativeTo(null); // middle of the screen

Dimension sz = new Dimension(450,250);

frm1.setPreferredSize(sz);

// displaying

frm1.pack();

frm1.setVisible(true);

}
public static void main(String a[])

{ // create object implements Runnable int’face

TrafficCounter sw = new TrafficCounter();

// run in EDT

SwingUtilities.invokeLater(sw);

private LayoutManager buatLayout()

{ return new GridLayout(3,0);

}
import javax.swing.*;

import java.awt.event.*;

import java.util.Random;

class testasal extends JFrame

{ private JPanel bram=new JPanel();

private JTextField tfluas=new JTextField();

private JTextField txtpanjang=new JTextField(2);

private JTextField txtlebar=new JTextField(2);

private JLabel lblluas=new JLabel("Luas");

private JLabel lblpanjang=new JLabel("Panjang");

private JLabel lbllebar=new JLabel("Lebar");

public static void main (String []arg)

{ testasal x = new testasal();

x.setVisible(true);

public testasal (){

super (" Hitung luas");

setSize (500,500);

bram.setLayout(null);

/*peletakkan objek ne.......*/


lblpanjang.setBounds(30,30,100,25);

lbllebar.setBounds(30,80,100,25);

lblluas.setBounds(30,175,100,25);

bram.add(lblpanjang);

bram.add(lbllebar);

bram.add(lblluas);

txtpanjang.setBounds(150,30,50,25);

txtlebar.setBounds(150,80,50,25);

tfluas.setBounds(150,175,50,25);

bram.add(txtpanjang);

bram.add(txtlebar);

bram.add(tfluas);

getContentPane().add(bram);

txtpanjang.addKeyListener(new KeyAdapter()

{ public void keyPressed (KeyEvent evt)

{ char c = (char) evt.getKeyCode ();

String d= txtpanjang.getText();
if(evt.getSource()== txtpanjang)

if(c==KeyEvent.VK_ENTER)

if(d.length()==2)

{ if(!Character.isDigit(d.charAt(1)) || !Character.isDigit(d.charAt(0)))

{ JOptionPane.showMessageDialog(null,"eh bego lo salah input",

"ERROR",JOptionPane.ERROR_MESSAGE);

txtpanjang.setText("");

txtpanjang.requestFocus();

else

{ txtlebar.requestFocus();

else if(Character.isDigit(d.charAt(0)))

{ txtlebar.requestFocus();

else

{ JOptionPane.showMessageDialog(null,"eh bego lo salah input",

"ERROR",JOptionPane.ERROR_MESSAGE);

txtpanjang.setText("");

txtpanjang.requestFocus();

public void keyTyped(KeyEvent ke)

{ char c= ke.getKeyChar();
if(txtpanjang.getText().length()>3)

ke.consume();

});

txtlebar.addKeyListener(new KeyAdapter(){

public void keyPressed (KeyEvent evt){

char c=(char) evt.getKeyCode();

String d=txtlebar.getText();

if(evt.getSource()==txtlebar)

if(c==KeyEvent.VK_ENTER)

if(d.length()==2){

if(!Character.isDigit(d.charAt(1)) || !Character.isDigit(d.charAt(0))){

JOptionPane.showMessageDialog(null,"NILAI YANG ANDA INPUT BUKAN ANGKA",

"ERROR",JOptionPane.ERROR_MESSAGE);

txtlebar.setText("");

txtlebar.requestFocus();

}else {

tfluas.requestFocus();

hitungluas();

posisiAcak();

} else if(Character.isDigit(d.charAt(0))) {

tfluas.requestFocus();

hitungluas();
posisiAcak();

} else {

JOptionPane.showMessageDialog(null,"NILAI YANG ANDA INPUT BUKAN ANGKA",

"ERROR",JOptionPane.ERROR_MESSAGE);

txtlebar.requestFocus();

public void keyTyped(KeyEvent ke) {

char c= ke.getKeyChar();

if(txtlebar.getText().length()>1)

ke.consume();

});

private void hitungluas(){

String sPanjang=txtpanjang.getText();

String sLebar=txtlebar.getText();

int panjang =Integer.parseInt(sPanjang);

int lebar =Integer.parseInt(sLebar);

int luas = panjang*lebar;

String sLuas= Integer.toString(luas);

tfluas.setText(sLuas);
}

private void posisiAcak() {

Random r1 =new Random();

int corX,corY;

corX =r1.nextInt(400);

corY =r1.nextInt (45);

tfluas.setBounds(corX,corY,50,25);

}
Jcombobox

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class TestJComboBox extends JFrame implements ItemListener {

String[] items = {"merah","hijau","biru","hitam","kuning"};

JComboBox cbo = new JComboBox(items);

public TestJComboBox() {

getContentPane().setBackground(Color.red);

setLayout(new FlowLayout());

add(cbo);

cbo.addItemListener(this);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setTitle("Test ComboBox");
setBounds(300,300,250,250);//( X, Y, panjang X, panjang Y)

public static void main(String [] args){

TestJComboBox theApp = new TestJComboBox();

theApp.setVisible(true);

public void itemStateChanged(ItemEvent e){

if(e.getStateChange()==ItemEvent.SELECTED){

String item = (String)e.getItem();

if(item.equals("merah")){

getContentPane().setBackground(Color.green);

} else if (item.equals("hijau")){

//getContentPane().setBackground(Color.black);

new test1().setVisible(true);

} else if (item.equals("biru")){

getContentPane().setBackground(Color.blue);

} else if (item.equals("hitam")){

getContentPane().setBackground(Color.yellow);

} else if (item.equals("kuning")){

getContentPane().setBackground(Color.yellow);

}
TestRadioCheck

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class TestRadioCheck extends JFrame implements ActionListener {

JRadioButton radMale = new JRadioButton("Male");

JRadioButton radFemale = new JRadioButton("Female");

JRadioButton radMember = new JRadioButton("Member");

JRadioButton radNonMember = new JRadioButton("Non-Member");

JPanel pnlRadio = new JPanel();

JPanel pnlCheck = new JPanel();

JCheckBox haveCC= new JCheckBox("Have Credit Card");

JButton btnShow = new JButton("Show");


public TestRadioCheck() {

//SET RADIO BUTTON

pnlRadio.setLayout(new GridLayout(2,3));

pnlRadio.add(new JLabel("Gender"));

pnlRadio.add(radMale);

pnlRadio.add(radFemale);

pnlRadio.add(new JLabel("Membership"));

pnlRadio.add(radMember);

pnlRadio.add(radNonMember);

ButtonGroup grpGender = new ButtonGroup();

grpGender.add(radMale);

grpGender.add(radFemale);

ButtonGroup grpMembership = new ButtonGroup();

grpMembership.add(radMember);

grpMembership.add(radNonMember);

radMale.setSelected(true);

radMember.setSelected(true);

//SET CHECK
pnlCheck.setLayout(new FlowLayout());

pnlCheck.add(haveCC);

pnlCheck.add(btnShow);

btnShow.addActionListener(this);

setLayout(new FlowLayout());

add(pnlRadio);

add(pnlCheck);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setTitle("Test Combobox2");

setBounds(0,0,350,150);

public static void main (String[]args){

TestRadioCheck theApp = new TestRadioCheck();

theApp.setVisible(true);

public void actionPerformed(ActionEvent e){

String str ="";

if(radMale.isSelected()) str+= "You are male\n";

else str+="You are female\n";

if(radMember.isSelected()) str+= "You are our member\n";

else str+="You are not our member\n";

if(haveCC.isSelected()) str+= "You have credit card\n";


else str+="You don\'t have credit card";

JOptionPane.showMessageDialog(this,str);

You might also like