Color Chooser
D. Color Chooser
import java.awt.;
void reaksi()
import java.awt.event.;
import javax.swing.colorchooser.;
warna.addActionListener(new ActionListener()
import javax.swing.;
class colorcoser extends JFrame
public void actionPerformed(ActionEvent e)
JLabel cop=new JLabel("PROGRAM GANTI WARNA");
if(e.getSource()==warna)
JInternalFrame jInternalFrame1 = new JInternalFrame();
JColorChooser jColorChooser1 = new JColorChooser();
jInternalFrame1.setVisible(true);
JPanel Panel1 = new JPanel();
JPanel Panel2 = new JPanel(); JButton btChange = new JButton();
JButton btCancel = new JButton();
btCancel.addActionListener(new
JButton warna = new JButton("Ganti Warna");
public void actionPerformed(ActionEvent e)
super("PROGRAM GANTI WARNA");
if(e.getSource()==btCancel)
jInternalFrame1.setVisible(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} void komponen()
getContentPane().setLayout(null);
btChange.addActionListener(new
getContentPane().add(jInternalFrame1);
ActionListener()
jInternalFrame1.getContentPane().setLayout(null);
jInternalFrame1.setVisible(false);
public void actionPerformed(ActionEvent e)
getContentPane().add(cop);
getContentPane().add(warna);
if(e.getSource()==btChange)
cop.setBounds(150, 10, 150, 23);
warna.setBounds(150, 40, 150, 23);
Color
Panel1.setLayout(null); newColor=jColorChooser1.getColor();
Panel1.setBorder(javax.swing.BorderFactory.createEtchedBorder (javax.swing.border.EtchedBorder.RAISED));
getContentPane().setBackground(newColor);
Panel1.add(jColorChooser1);
jInternalFrame1.setVisible(false);
jColorChooser1.setBounds(9, 10, 440, 290); jInternalFrame1.getContentPane().add(Panel1);
Panel1.setBounds(10, 10, 460, 310); Panel2.setLayout(null);
Panel2.setBorder(javax.swing.BorderFactory.createEtchedBorder
public static void main(String[] args)
(javax.swing.border.EtchedBorder.RAISED));
btChange.setText("Change");
colorcoser evt=new colorcoser();
Panel2.add(btChange);
evt.komponen();
btChange.setBounds(230, 10, 100, 23);
evt.reaksi();
btCancel.setText("Cancel");
Panel2.add(btCancel); btCancel.setBounds(350, 10, 100, 23);
jInternalFrame1.getContentPane().add(Panel2); Panel2.setBounds(10, 330, 460, 40); jInternalFrame1.setBounds(4, 70, 490, 420); getContentPane().setBackground(new
java.awt.Color(234,235,213));
setVisible(true);
A. Contoh Aplikasi Database Sederhana :
Anda Buat Database baru dengan nama coba, kemudian buat table dengan mengeksekusi query berikut ini :
CREATE TABLE IF NOT EXISTS `mahasiswa` (
`nim` varchar(12) NOT NULL, `nama` varchar(40) NOT NULL, `alamat` varchar(200) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Kemudian Anda Buat Kelas Baru Dengan Nama Database2 dan Anda Pastekan Kode Berikut: import java.sql.;
import javax.swing.; import java.awt.event.;
author Gianto-echa
public class Database2 extends JFrame { Connection con=null;
JLabel
jLabel1 = new JLabel(); JTextField txtnama = new JTextField(); JLabel jLabel2 = new JLabel(); JScrollPane jScrollPane1 = new JScrollPane(); JTextArea txtalamat = new JTextArea(); JLabel jLabel3 = new JLabel(); JButton bthapus = new JButton(); JButton btcari = new JButton(); JButton btupdate = new JButton(); JTextField txtnim = new JTextField(); JLabel jLabel4 = new JLabel(); JButton btsimpan = new JButton();
Database2() { setSize(320,280); setLocation(100,100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
void Koneksi() { try {
String connectionURL = "jdbc:mysql:localhostcoba"; String username = "root"; String password = "root"; Class.forName("com.mysql.jdbc.Driver"); String connectionURL = "jdbc:mysql:localhostcoba"; String username = "root"; String password = "root"; Class.forName("com.mysql.jdbc.Driver");
System.err.println("Sukses Koneksi");
} catch(Exception ex) {
System.err.println("Tidak Berhasil Koneksi"); System.exit(1);
void simpan()
try
{ Statement statement = con.createStatement(); String sql="insert into mahasiswa
values('"+txtnim.getText()+"','"+txtnama.getText()+"','"+txtalamat.getText()+"');";
statement.executeUpdate(sql); statement.close(); JOptionPane.showMessageDialog(null,"Berhasil disimpan");
} catch(Exception ex) { JOptionPane.showMessageDialog(null,"nim data sudah ada"); System.err.print(ex);
} public void cari()
try
{ Statement stat = con.createStatement(); String sql="SELECT FROM mahasiswa Where nim like'"+txtnim.getText()+"'"; ResultSet isi = stat.executeQuery(sql);
if(isi.next()) {
txtnama.setText(isi.getString(1)); txtalamat.setText(isi.getString(2));
} else { JOptionPane.showMessageDialog(null,"mahasiswa dengan nim tersebut tidak ada..!");
stat.close(); } catch (Exception ex) {
JOptionPane.showMessageDialog(null,"Error :"+ex); }
void update()
try
{ Statement stm=con.createStatement(); String sql="update mahasiswa set nama='"+txtnama.getText()+"',
alamat='"+txtalamat.getText()+"'where nim='"+txtnim.getText()+"'";
stm.executeUpdate(sql); stm.close(); JOptionPane.showMessageDialog(null,"Berhasil diedit");
} catch(Exception ex) { JOptionPane.showMessageDialog(null,"Error :"+ex); }
public void hapus()
{ try
{ Statement statement = con.createStatement(); String sql="delete from mahasiswa "+ " where nim='"+txtnim.getText()+"'"; statement.executeUpdate(sql); statement.close(); JOptionPane.showMessageDialog(null,"Data telah di Hapus");
} catch(Exception ex) {
JOptionPane.showMessageDialog(null,"Error :"+ex); }
void aksi()
btsimpan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) { simpan(); }
}); btupdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) { update(); }
}); bthapus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) { hapus(); }
btcari.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) { cari(); }
private void initComponents() {
getContentPane().setLayout(null); getContentPane().setLayout(null);
jLabel2.setText("Alamat"); getContentPane().add(jLabel2); jLabel2.setBounds(20, 100, 34, 14);
txtalamat.setColumns(20); txtalamat.setRows(5); jScrollPane1.setViewportView(txtalamat);
getContentPane().add(jScrollPane1); jScrollPane1.setBounds(90, 96, 166, 70);
jLabel3.setText("Nama"); getContentPane().add(jLabel3); jLabel3.setBounds(20, 70, 27, 14);
bthapus.setText("Delete");
getContentPane().add(bthapus); bthapus.setBounds(180, 180, 73, 23);
btcari.setText("Cari");
getContentPane().add(btcari); btcari.setBounds(200, 40, 51, 23);
btupdate.setText("Update");
getContentPane().add(btupdate); btupdate.setBounds(90, 180, 73, 23); getContentPane().add(txtnim); txtnim.setBounds(90, 40, 100, 20);
jLabel4.setText("Nim"); getContentPane().add(jLabel4); jLabel4.setBounds(20, 40, 17, 14);
btsimpan.setText("Simpan");
getContentPane().add(btsimpan); btsimpan.setBounds(10, 180, 67, 23);
setVisible(true);
public static void main(String args[])
Database2 db2=new Database2(); db2.initComponents(); db2.Koneksi(); db2.aksi();
}