Nama : Nurohman
NPM : 201343500091
Matkul : Pemrograman Visual
Dosen : Nahot Frastian, M.Kom
Universitas : Universitas Indraprasta PGRI
1.
Buat Program di bawah ini dan perbaiki program
di bawah ini :
import java.swing*;
public class PasswordField{
public static void main (String () args){
JLabel L1=new JLabel (“User Name”),
JLabel L2=new JLabel (“Password”),
JTextField tf1=new JTextField(25),
JFrame frame=new JFrame(“Silahkan Sign
In”),
JPasswordField psw=new JPasswordField(20),
Frame.setSize(500,500),
L1.setBounds(10,50,100,25),
L2.setBounds(10,100,100,25),
tf1.setBounds(100,50,250,25),
paswd.setBounds(100,100,250,25),
frame.add(L1),
frame.add(L2),
frame.add(tf1),
frame.add(psw),
frame.setLayout(null),
frame.setVisible(true),
}
}
}
Jawaban yang benar :
import javax.swing.*;
public class PasswordField{
JLabel L1=new JLabel ("User
Name");
JLabel L2=new JLabel
("Password");
JTextField tf1=new JTextField(25);
JFrame frame=new JFrame("Silahkan Sign
In");
JPasswordField paswd=new
JPasswordField(20);
frame.setSize(500,500);
L1.setBounds(10,50,100,25);
L2.setBounds(10,100,100,25);
tf1.setBounds(100,50,250,25);
paswd.setBounds(100,100,250,25);
frame.add(L1);
frame.add(L2);
frame.add(tf1);
frame.add(paswd);
frame.setLayout(null);
frame.setVisible(true);
}
}
Output
2.
Buat Program di bawah ini :
import javax.swing.*;
import
javax.swing.table*;
import java.awt*;
/**
*
*@author Ernis
*/
public class
SaddingKolom{
/**
*@param args the command
line arguments
*/
public static void main
(String [] args){
SaddingKolom
saddingKolom=new SaddingKolom();
}
public SaddingKolom();
JFrame frame=new
JFrame(“Contoh Sadding Kolom”);
JPanel panel = new
JPanel();
String data[][]={{“01”,”andi”,”pemrograman
visual”,”90”},{“02”,”budi”,”pemrograman
visual”,”80”},{“03”,”Susan”,”pemrograman
visual”,”75”},{“04”,”Ali”,”pemrograman
visual”,”70”}}
String
col[]={“No”,”Nama”,”Mata Kuliah”,”Nilai”}
DefauldTableModel
model=new DefaultTableModel(data.col)
Pengaturan
JTable table=new
JTable(model){
@Override
public Component
prepareRenderer(TableCellRenderer renderer, int index_row, int index_col){
Component comp=super
prepareRenderer(renderer, index_row, index_col);
Odd col index, selected
or not selected
If(index_col%2!=0
&& !isCellSelected(index_row,index_col)){
Comp.setBackground(Color.GREEN);
}
Else{
Comp.setBackground(Color.white);
}
Return comp
}
}
JTableHeader
header=table.getTableHeader()
header.setBackground(Color
yellow);
JScrollPane pane=new
JScrollPane(table);
panel.add(pane)
frame.add(panel)
frame.setSize(460,200);
frame.setUndecorated(true);
frame.getRootPane()setWindowsDecorationStyle(JRootPane.PLAIN_DIALOG)
frame.setVisible(true)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
}
}
}
Jawaban yang benar :
import javax.swing.*;
import
javax.swing.table.*;
import java.awt.*;
public class
SaddingKolom{
public static void main (String [] args){
SaddingKolom saddingKolom = new
SaddingKolom();
}
public SaddingKolom(){
JFrame frame = new JFrame("Contoh
SaddingKolom");
JPanel panel = new JPanel();
String
data[][]={{"01","andi","pemrograman
visual","90"},{"02","budi","pemrograman
visual","80"},
{"03","susan","pemrograman
visual","75"},{"04","ali","pemrograman
visual","70"}};
String
col[]={"No","Nama","Mata
Kuliah","Nilai"};
DefaultTableModel model = new
DefaultTableModel(data,col);
//Pengaturan
JTable table = new JTable(model){
public Component
prepareRenderer(TableCellRenderer renderer,int index_row,int index_col){
Component comp =
super.prepareRenderer(renderer, index_row, index_col);
//odd col index,selected or not
selected
if(index_col%2!=0&&!isCellSelected(index_row,index_col)){
comp.setBackground(Color.GREEN);
}
else{
comp.setBackground(Color.white);
}
return comp;
}
};
JTableHeader header =
table.getTableHeader();
header.setBackground(Color.yellow);
JScrollPane pane = new JScrollPane(table);
panel.add(pane);
frame.add(panel);
frame.setSize(460,200);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
OUTPUT
3.
Perbaiki program di bawah ini :
import javax.awt*;
import javax.awt.event*;
import java.swing*;
public class formInputDataMhs extends
JFrame implements ActionListener{
public formInputDataMhs(){
super(“form Input Data Mhs”)
setSize(500,380)
setVisible(true)
setLayout(null)
add(lblNpm)
add(lblNama)
add(lblProdi)
add(lblNamaDosen)
add(lblAlamat)
add(lblNotlp)
add(lblTanda)
add(JtNpm)
add(JtNama)
add(JtProdi)
add(JtNamaDosen)
add(JtAlamat)
add(JtNotlp)
add(JtTanda)
add(Btn)
add()
{
public void add()
lblNpm.setBounds(10,20,90,25)
lblNama.setBounds(10,60,90,25)
lblProdi.setBounds(10,100,90,25)
lblNamaDosen.setBounds(10,140,90,25)
lblAlamat.setBounds(10,180,90,25)
lblNotlp.setBounds(10,220,90,25)
lblTanda.setBounds(180,220,90,25)
JtNpm.setBounds(120,20,250,25)
JtNama.setBounds(120,60,250,25)
JtProdi.setBounds(120,100,250,25)
JtNamaDosen.setBounds(120,140,250,25)
JtAlamat.setBounds(120,180,250,25)
JtNotlp.setBounds(120,220,250,25)
JtTanda.setBounds(200,220,50,25)
Btn.setBounds(120,260,90,25)
Btn.addActionListener(this)
{
public void actionPerformed(ActionEvent e)}
if(e.getSource()==Btn)}
JOptionPane.showMessageDialog(null,”Anda
Harus Login”)}
new data()}
{
{
JLabel lblNpm = new JLabel(“NPM”)
JLabel lblNama = new JLabel(“Nama”)
JLabel lblProdi = new JLabel(“Prodi”)
JLabel lblNamaDosen = new JLabel(“Nama
Dosen”)
JLabel lblAlamat = new JLabel(“Alamat”)
JLabel lblNotlp = new JLabel(“Nomor
Telepon”)
JLabel lblTanda = new JLabel(“---”)
JTextField JtNpm=new JTextField()
JTextField JtNama=new JTextField()
JTextField JtProdi=new JTextField()
JTextField JtNamaDosen=new JTextField()
JTextField JtAlamat=new JTextField()
JTextField JtNotlp=new JTextField()
JTextField JtTanda=new JTextField()
JButton Btn=new JButton(“Simpan”)
{
Jawaban yang benar :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class formInputDataMhs extends
JFrame implements ActionListener{
JLabel lblNpm = new
JLabel("NPM");
JLabel lblNama = new
JLabel("Nama");
JLabel lblProdi = new
JLabel("Prodi");
JLabel lblNamaDosen = new JLabel("Nama
Dosen");
JLabel lblAlamat = new
JLabel("Alamat");
JLabel lblNotlp = new JLabel("Nomor
Telepon");
JLabel lblTanda = new
JLabel("---");
JTextField JtNpm=new JTextField();
JTextField JtNama=new JTextField();
JTextField JtProdi=new JTextField();
JTextField JtNamaDosen=new JTextField();
JTextField JtAlamat=new JTextField();
JTextField JtNotlp=new JTextField();
JTextField JtTanda=new JTextField();
JButton Btn=new
JButton("Simpan");
public formInputDataMhs(){
super("form Input Data Mhs");
setSize(500,380);
setVisible(true);
setLayout(null);
add(lblNpm);
add(lblNama);
add(lblProdi);
add(lblNamaDosen);
add(lblAlamat);
add(lblNotlp);
add(lblTanda);
add(JtNpm);
add(JtNama);
add(JtProdi);
add(JtNamaDosen);
add(JtAlamat);
add(JtNotlp);
add(JtTanda);
add(Btn);
lblNpm.setBounds(10,20,90,25);
lblNama.setBounds(10,60,90,25);
lblProdi.setBounds(10,100,90,25);
lblNamaDosen.setBounds(10,140,90,25);
lblAlamat.setBounds(10,180,90,25);
lblNotlp.setBounds(10,220,90,25);
lblTanda.setBounds(180,250,90,25);
JtNpm.setBounds(120,20,250,25);
JtNama.setBounds(120,60,250,25);
JtProdi.setBounds(120,100,250,25);
JtNamaDosen.setBounds(120,140,250,25);
JtAlamat.setBounds(120,180,250,25);
JtNotlp.setBounds(120,220,250,25);
JtTanda.setBounds(120,260,50,25);
Btn.setBounds(120,300,100,25);
Btn.addActionListener(this);
}
public void actionPerformed(ActionEvent
ae){
if(ae.getSource()==Btn){
JOptionPane.showMessageDialog(null,"Anda
Harus Login");
{
{
JLabel lblNpm = new JLabel("NPM");
JLabel lblNama = new
JLabel("Nama");
JLabel lblProdi = new
JLabel("Prodi");
JLabel lblNamaDosen = new JLabel("Nama
Dosen");
JLabel lblAlamat = new
JLabel("Alamat");
JLabel lblNotlp = new JLabel("Nomor
Telepon");
JLabel lblTanda = new
JLabel("---");
JTextField JtNpm=new JTextField();
JTextField JtNama=new JTextField();
JTextField JtProdi=new JTextField();
JTextField JtNamaDosen=new JTextField();
JTextField JtAlamat=new JTextField();
JTextField JtNotlp=new JTextField();
JTextField JtTanda=new JTextField();
JButton Btn=new
JButton("Simpan");
}
}
}
}
}
Output
4.
Perbaikin program di bawah ini :
import
java,swing*;
public class
JmenuItemTest {
JFrame
myFrame = null;
public static void main (String[]a)
(new JmenuItemTest()).test();
{
private void
test() }
myFrame =
newJFrame (“Menu Item Test”);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setBounds(50,50,250,150)
myFrame.setContentPane(new
JdesktopPane())
JMenuBar myMenuBar
= new JMenuBar()
JMenu myMenu
= getFileMenu()
myMenuBar.add(myMenu)
myMenu =
getColorMenu()
myMenuBar.add(myMenu)
myMenu =
getOptionMenu()
myMenuBar.add(myMenu)
JmenuItem
myItem = new jmENUiTEM(“Help”);
myMenuBar.add(myItem);
myFrame.setJMenuBar(myMenuBar);
myFrame.setVisible(true);
}
Private JMenu
getFileMenu() {
JMenu myMenu = new JMenu(“File”);
JMenuItem myItem = new JmenuItem(“Open”);
myMenu.add(myItem);
myItem =new JmenuItem(“Close”);
myItem.setEnabled(false);
myMenu.add(myItem);
myMenu,addSeparator();
myItem = new JmenuItem(“Exit”);
myMenu.add(myItem)
return myMenu;
}
private JMenu
getColorMenu() {
JMenu myMenu = new JMenu(“Color”);
JmenuItem myItem = new JmenuItem(“Red”);
private JMenu
getOptionMenu() {
JMenu myMenu = new JMenu(“Option”);
JMenu mySubMenu = getOnOffMenu(“Sound”);
myMenu,add(mySubMenu);
mySubMenu = getOnOffMenu (“Auto Save”);
mySubMenu.setEnabled(false);
myMenu.add(mySubMenu);
return myMenu;
}
Private JMenu
getOnOffMenu(String title){
JMenu myMenu = new JMenu(title);
JMenuItem myItem = new JMenuItem(“On”);
myMenu.add(myItem);
myItem = new JMenuItem(“Off”);
myMenu,add(myItem);
return
myMenu;
}
}
}
Jawaban yang benar :
import javax.swing.*;
public class JmenuItemTest {
JFrame myFrame = null;
public static void main (String[]a){
(new JmenuItemTest()).test();
}
private void test() {
myFrame = new JFrame ("Menu Item
Test");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setBounds(50,60,250,150);
myFrame.setContentPane(new JDesktopPane());
JMenuBar myMenuBar = new JMenuBar();
JMenu myMenu=getFileMenu();
myMenuBar.add(myMenu);
myMenu=getColorMenu();
myMenuBar.add(myMenu);
myMenu=getOptionMenu();
myMenuBar.add(myMenu);
JMenuItem myItem = new
JMenuItem("Help");
myMenuBar.add(myItem);
myFrame.setJMenuBar(myMenuBar);
myFrame.setVisible(true);
}
private JMenu getFileMenu() {
JMenu myMenu=new JMenu("File");
JMenuItem myItem=new JMenuItem("Open");
myMenu.add(myItem);
myItem=new JMenuItem("Close");
myItem.setEnabled(false);
myMenu.add(myItem);
myMenu.addSeparator();
myItem=new JMenuItem("Exit");
myMenu.add(myItem);
return myMenu;
}
private JMenu getColorMenu() {
JMenu myMenu=new JMenu("Color");
JMenuItem myItem=new JMenuItem("Purple");
return myMenu;
}
private JMenu getOptionMenu() {
JMenu myMenu=new JMenu("Option");
JMenu mySubMenu=getOnOffMenu("Sound");
myMenu.add(mySubMenu);
mySubMenu=getOnOffMenu ("Auto Save");
mySubMenu.setEnabled(false);
myMenu.add(mySubMenu);
return myMenu;
}
private JMenu getOnOffMenu(String title){
JMenu myMenu = new JMenu(title);
JMenuItem myItem = new JMenuItem("On");
myMenu.add(myItem);
myItem = new JMenuItem("Off");
myMenu.add(myItem);
}
}
Output
0 Comments