- Get link
- X
- Other Apps
Pada praktikum kali ini membahas tentang AWT
dan String. Digunakan untuk membangun GUI dan berisi platform oriented.
Komponen di dalam
AWT :
Komponen
di dalam Swing :
Contoh
1 Pengaturan Border Layout:
Source Code:
/** Main Program BorderLayoutDemo**/ (BorderLayoutDemo.java)
package
borderlayoutdemo;
import
javax.swing.*;
import
java.awt.*;
public class
Frame_Border_Layout extends javax.swing.JFrame {
public Frame_Border_Layout() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold
defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
public static void main(String args[]) {
JFrame frame = new JFrame ("Contoh
Border Layout");
BorderLayout
layout = new BorderLayout(1,1);
frame.getContentPane().setLayout(layout);
JButton btnNorth
= new JButton("Posisi NORTH");
JButton btnSouth
= new JButton("Posisi SOUTH");
JButton btnEast =
new JButton("Posisi EAST");
JButton btnWest =
new JButton("Posisi WEST");
JButton btnCenter
= new JButton("Posisi CENTER");
frame.getContentPane().add(btnNorth,BorderLayout.NORTH);
frame.getContentPane().add(btnSouth,BorderLayout.SOUTH);
frame.getContentPane().add(btnEast,BorderLayout.EAST);
frame.getContentPane().add(btnWest,BorderLayout.WEST);
frame.getContentPane().add(btnCenter,BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.show();
/* Set the Nimbus look and feel */
//<editor-fold
defaultstate="collapsed" desc=" Look and feel setting code
(optional) ">
/* If Nimbus (introduced in Java SE 6)
is not available, stay with the default look and feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for
(javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if
("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Frame_Border_Layout.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Frame_Border_Layout.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Frame_Border_Layout.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch
(javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Frame_Border_Layout.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new
Runnable() {
public void run() {
new
Frame_Border_Layout().setVisible(true);
}
});
}
// Variables declaration - do not
modify
// End of variables declaration
}
Contoh
2 Pengaturan Box Layout
Source Code:
(Project
BoxLayoutDemo.java)
package boxlayoutdemo;
import javax.swing.*;
import java.awt.*;
public class BoxLayoutDemo {
public static void main(String[] args) {
JFrame frame = new JFrame ("BoxLayoutDemo");
Box comp = new Box(BoxLayout.Y_AXIS);
//X_AXIS
JButton btn1 = new JButton("Button
1");
JButton btn2 = new JButton("Button
2");
JButton btn3 = new JButton("Button
3");
JButton btn4 = new JButton("Long
Named Button 4");
JButton btn5 = new JButton("Button
5");
btn1.setAlignmentX(Component.CENTER_ALIGNMENT);
btn2.setAlignmentX(Component.CENTER_ALIGNMENT);
btn3.setAlignmentX(Component.CENTER_ALIGNMENT);
btn4.setAlignmentX(Component.CENTER_ALIGNMENT);
btn5.setAlignmentX(Component.CENTER_ALIGNMENT);
comp.add(btn1);
comp.add(btn2);
comp.add(btn3);
comp.add(btn4);
comp.add(btn5);
frame.getContentPane().add(comp);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.show();
}
}
Tugas Buatlah Frame Sistem Biodata
Mahasiswa
Source Code:
package mahasiswa;
public class frameMahasiswa extends
javax.swing.JFrame {
/**
* Creates new form frameMahasiswa
*/
public frameMahasiswa() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated
Code">
private void initComponents() {
jFrame1 = new javax.swing.JFrame();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
Nama = new javax.swing.JTextField();
asalSekolah = new javax.swing.JTextField();
lakiLaki = new javax.swing.JRadioButton();
Perempuan = new javax.swing.JRadioButton();
Jurusan = new javax.swing.JComboBox<>();
jScrollPane1 = new javax.swing.JScrollPane();
tampil = new javax.swing.JTextArea();
simpan = new javax.swing.JButton();
keluar = new javax.swing.JButton();
javax.swing.GroupLayout jFrame1Layout = new
javax.swing.GroupLayout(jFrame1.getContentPane());
jFrame1.getContentPane().setLayout(jFrame1Layout);
jFrame1Layout.setHorizontalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame1Layout.setVerticalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Nama ");
jLabel2.setText("Asal Sekolah ");
jLabel3.setText("Jenis Kelamin ");
jLabel4.setText("Jurusan");
Nama.addActionListener(new
java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
NamaActionPerformed(evt);
}
});
lakiLaki.setText("Laki - laki");
lakiLaki.addActionListener(new java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
lakiLakiActionPerformed(evt);
}
});
Perempuan.setText("Perempuan");
Perempuan.addActionListener(new java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
PerempuanActionPerformed(evt);
}
});
Jurusan.setModel(new javax.swing.DefaultComboBoxModel<>(new
String[] { "Informatika", "Telekomunikasi",
"DKV", "Industri", "Elektro", "Sistem
Informasi", "RPL" }));
Jurusan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
JurusanActionPerformed(evt);
}
});
tampil.setColumns(20);
tampil.setRows(5);
jScrollPane1.setViewportView(tampil);
simpan.setText("Simpan");
simpan.addActionListener(new java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
simpanActionPerformed(evt);
}
});
keluar.setText("Keluar");
keluar.addMouseListener(new java.awt.event.MouseAdapter() {
public void
mouseClicked(java.awt.event.MouseEvent evt) {
keluarMouseClicked(evt);
}
});
keluar.addActionListener(new java.awt.event.ActionListener() {
public void
actionPerformed(java.awt.event.ActionEvent evt) {
keluarActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(72, 72, 72)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addGroup(layout.createSequentialGroup()
.addComponent(simpan)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(keluar))
.addComponent(jScrollPane1)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addGap(63, 63, 63)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Perempuan)
.addComponent(lakiLaki)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(Nama)
.addComponent(asalSekolah, javax.swing.GroupLayout.PREFERRED_SIZE, 180,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(Jurusan, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(97,
Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(Nama,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(asalSekolah,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(lakiLaki))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Perempuan)
.addGap(5, 5, 5)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(Jurusan,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 76,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(simpan)
.addComponent(keluar))
.addContainerGap(35,
Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void NamaActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void lakiLakiActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
}
private void PerempuanActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
}
private void JurusanActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
}
private void simpanActionPerformed(java.awt.event.ActionEvent evt)
{
String nama = Nama.getText();
String asal = asalSekolah.getText();
String jur = (String) Jurusan.getSelectedItem();
String JenKel="";
if (lakiLaki.isSelected()){
JenKel = lakiLaki.getText();
}else{
JenKel = Perempuan.getText();
}
tampil.setText("Nama : "+nama+"\nAsal :
"+asal+"\nJurusan : "+jur+"\nJK : "+JenKel);
// TODO add your handling code here:
}
private void keluarMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
private void keluarActionPerformed(java.awt.event.ActionEvent evt)
{
System.exit(0);
// TODO add your handling code here:
}
/**
* @param args the command line
arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look
and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the
default look and feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo
info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if
("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(frameMahasiswa.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(frameMahasiswa.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(frameMahasiswa.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(frameMahasiswa.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new
frameMahasiswa().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox<String> Jurusan;
private javax.swing.JTextField Nama;
private javax.swing.JRadioButton Perempuan;
private javax.swing.JTextField asalSekolah;
private javax.swing.JFrame jFrame1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton keluar;
private javax.swing.JRadioButton lakiLaki;
private javax.swing.JButton simpan;
private javax.swing.JTextArea tampil;
// End of variables declaration
}
- Get link
- X
- Other Apps
Comments
Post a Comment