Form & Button (Sumber Dosen Pertama Pa Achmad)

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 10

Form & Button (Sumber dosen pertama Pa Achmad)

 
import javax.swing.*;
public class Tugas_Button {
    public static void main (String args[])
    {
        JTextField txt1 = new JTextField();
        JTextField txt2 = new JTextField();
        JTextField txt3 = new JTextField();
        JPanel panel1= new JPanel();
        //JPanel bidang frame, kalau ingin nambahin object
        JLabel lbl1=new JLabel("NPM");
        JLabel lbl2=new JLabel("Nama");
        JLabel lbl3=new JLabel("Alamat");
        JLabel lbl4=new JLabel("Form Mahasiswa Baru");
        JButton btnOk=new JButton("OK");
        JButton btnCancel=new JButton("Cancel");
        JFrame frame = new JFrame();
        frame.setSize(500,400);
        frame.setTitle("Form Mahasiswa Baru");
        frame.setVisible(true);
        frame.setLocation(400,200);
        panel1.setLayout(null);
        panel1.add(lbl1);
        panel1.add(lbl2);
        panel1.add(lbl3);
        panel1.add(lbl4);
        panel1.add(txt1);
        panel1.add(txt2);
        panel1.add(txt3);
 
        panel1.add(btnOk);
        panel1.add(btnCancel);
 
        txt1.setBounds(150,50,200,25);
        txt2.setBounds(150,100,200,25);
        txt3.setBounds(150,150,200,25);
        lbl1.setBounds(50,50,100,25);
        lbl2.setBounds(50,100,100,25);
        lbl3.setBounds(50,150,100,25);
        lbl4.setBounds(150,10,300,25);
   //     lbl4.setForeground(Color.red);
        btnOk.setBounds(75,200,100,25);
        btnCancel.setBounds(200,200,100,25);
        //(left,top,huruf,top)
        frame.getContentPane().add(panel1);
    }
 
 
}
 

 
------------------------------------------------------
Tombo 1 Sumber google
 
 
import javax.swing.*;
 
class cobaSwing extends JFrame {
 
public cobaSwing() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
 
public static void main(String args[]) {
cobaSwing mainFrame = new cobaSwing();
mainFrame.setSize(300, 200);
mainFrame.setTitle("Coba Swing");
JPanel pnl = new JPanel();
 
JButton tombol1 = new JButton("Tombol 1");
pnl.add(tombol1);
 
mainFrame.add(pnl);
mainFrame.setVisible(true);
 
}
 
}
 
 
 
-----------------------------------------------------
 
GUI sumber dari dosen Bram
 
import javax.swing.*;
class GUI extends JFrame
{
static void main (String []GUI)
{new GUI();}
JLabel lbl1=new JLabel ("Input Form");
JLabel lbl2=new JLabel ("nama :");
 
GUI()
{
setSize(500,400);
setVisible(true);
setLayout(null);
/*(x,y,px,ly)*/
lbl1.setBounds(100, 100, 300, 25);
lbl2.setBounds(100, 130, 400, 30);
add(lbl1);
add(lbl2);
 
}
}
 

 
---------------------------------------
TestJComboBox sumber BRam
 
 
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.orange);
           new GUI().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 sumber DOsen Bram
 
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);
        }
    }
 

 
Selamat berjuan !!!!!!!!!!

You might also like