Minggu, 04 Desember 2011

Source Code Tubes2 Pemograman Visual (Semester3)

1. Latihan (Yang ada Button didalam frame)


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

public class Menu {

public Menu() {

final JFrame frame2 = new JFrame("Menu");
frame2.setSize(320, 250);
frame2.setLocationRelativeTo(null);
frame2.setLayout(new FlowLayout(FlowLayout.LEFT));

final JMenuBar bar = new JMenuBar();

JMenu menuhome = new JMenu("Beranda");
bar.add(menuhome);

JMenuItem itemaccount = new JMenuItem("Pengaturan Account");
menuhome.add(itemaccount);
menuhome.addSeparator();

JMenuItem itemLogout = new JMenuItem("Logout");
itemLogout.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
int pesan = JOptionPane.showConfirmDialog(null,
"Keluar aplikasi?", "Peringatan",
JOptionPane.YES_NO_OPTION);

if (pesan == 0)
// System.exit(0);
// System.out.println("logout...");
frame2.setVisible(false);

}
});
menuhome.add(itemLogout);

JMenu menuprof = new JMenu("Profil");
bar.add(menuprof);

final JMenuItem menuabout = new JMenu("About as");
menuprof.add(menuabout);

JMenuItem itemgambar = new JMenuItem("Gambar");
itemgambar.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
MenuGambar mg = new MenuGambar();

}
});
menuabout.add(itemgambar);
final JMenuItem iteminfo = new JMenuItem("Info");
iteminfo.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
String isiinfo = ("PIS1002 adalah nama kelas di Politeknik Telkom , didalamnya terdiri dari orang-orang "
+ "yang berasal dari berbagai kota. Memiliki karakter yang berbeda-beda dari orang-orang "
+ "yang berasal dari berbagai kota. Memiliki karakter yang berbeda-beda dari orang-orang "
+ "yang berasal dari berbagai kota. Memiliki karakter yang berbeda-beda dari orang-orang "
+ "yang berasal dari berbagai kota. Memiliki karakter yang berbeda-beda ");
JTextArea info = new JTextArea(isiinfo, 5, 30);
JScrollPane scroll = new JScrollPane(info,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

info.setLineWrap(true);
info.setWrapStyleWord(true);
info.setForeground(Color.BLUE);
info.setFont(new Font("Papyrus", Font.ITALIC, 12));

//frame2.add(iteminfo);
// frame2.add(info);
frame2.add(scroll, BorderLayout.CENTER);
frame2.setVisible(true);

}
});
menuabout.add(iteminfo);
JMenu menuSearch = new JMenu("Pencarian");
bar.add(menuSearch);

JMenuItem itemnama = new JMenuItem("Nama");
menuSearch.add(itemnama);
menuSearch.addSeparator();
JMenuItem itememail = new JMenuItem("E-mail");
menuSearch.add(itememail);
JMenu File = new JMenu("File");
bar.add(File);
JMenuItem baru = new JMenu("New");
File.add(baru);
// JMenuItem java = new JMenuItem("Java Project");
//String t ="Java Project";
//baru.add(new JMenuItem(t));
baru.add(new JMenuItem("Java Project"));
frame2.add(bar);
frame2.setVisible(true);
}

public static void main(String[] args) {
Menu m = new Menu();

}
}

2. Latihan (Memasukkan User dan Password)
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class PisSepuluhDua {
public static void main (String[] args) {
JFrame utama = new JFrame("Login");
utama.getContentPane().setBackground(Color.WHITE);
utama.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
utama.setSize(400, 300);
utama.setLayout(new FlowLayout(FlowLayout.CENTER));
utama.setLocationRelativeTo(null);
JLabel label = new JLabel("User");
label.setSize(200, 100);
JLabel labelpass = new JLabel("Password");
labelpass.setSize(100, 100);
final JTextField txtuser = new JTextField(10);
txtuser.setToolTipText("User");
final JTextField txtpass = new JTextField(10);
txtpass.setToolTipText("masukkan password");
JButton button = new JButton("Klik");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String user = txtuser.getText();
String pass = txtpass.getText();
if (user.equals("utama") && pass.equals("utama")) {
JOptionPane.showMessageDialog(null,
"Login sukses");
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setLayout(new FlowLayout());
frame.setLocationRelativeTo(null);
JMenuBar bar = new JMenuBar();
JMenu MenuFile = new JMenu("File");
bar.add(MenuFile);
JMenuItem menuprofil = new JMenu("profil");
JMenuItem itempicture = new JMenuItem("picture");
menuprofil.add(itempicture);
JMenu edit = new JMenu("edit");
bar.add(edit);
frame.add(bar);
frame.setVisible(true);
}
});
utama.add(label);
utama.add(txtpass);
utama.add(labelpass);
utama.add(txtuser);
utama.add(button);
utama.setVisible(true);
}

}

3. Latihan (Google)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;


public class MichelleAmandaMP_30110452 {
public static void main(String[] args) {
JFrame frame = new JFrame("PIS 10-02");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setLayout(new FlowLayout());
frame.getContentPane().setBackground(Color.WHITE);
JLabel label = new JLabel();
label.setText("       Google       ");
label.setBackground(Color.WHITE);
frame.add(label, BorderLayout.CENTER);
JTextField txt = new JTextField(20);
txt.setToolTipText("ENTER WORD");
txt.setBackground(Color.lightGray);
frame.add(txt, BorderLayout.CENTER);
JButton button = new JButton();
button.setText("SEARCH");
frame.add(button, BorderLayout.BEFORE_FIRST_LINE);
JLabel label1 = new JLabel();
label1.setText("Gambar        Web          Iklan");
label1.setBackground(Color.darkGray);
frame.add(label1, BorderLayout.AFTER_LINE_ENDS);
frame.add(label);
frame.add(txt);
frame.add(button);
frame.add(label1);
frame.setVisible(true);
}

}

4. Latihan (GridBag Layout)

import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;

public class GridBagLayoutDemo {
  final static boolean shouldFill = true;

  final static boolean shouldWeightX = true;

  final static boolean RIGHT_TO_LEFT = false;

  public static void addComponentsToPane(Container pane) {
    if (RIGHT_TO_LEFT) {
      pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    }

    JButton button;
    pane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    if (shouldFill) {
      // natural height, maximum width
      c.fill = GridBagConstraints.HORIZONTAL;
    }

    button = new JButton("Button 1");
    if (shouldWeightX) {
      c.weightx = 0.5;
    }
    c.gridx = 0;
    c.gridy = 0;
    pane.add(button, c);

    button = new JButton("Button 2");
    c.gridx = 1;
    c.gridy = 0;
    pane.add(button, c);

    button = new JButton("Button 3");
    c.gridx = 2;
    c.gridy = 0;
    pane.add(button, c);

    button = new JButton("Long-Named Button 4");
    c.ipady = 40; // make this component tall
    c.weightx = 0.0;
    c.gridwidth = 3;
    c.gridx = 0;
    c.gridy = 1;
    pane.add(button, c);

    button = new JButton("5");
    c.ipady = 0; // reset to default
    c.weighty = 1.0; // request any extra vertical space
    c.anchor = GridBagConstraints.PAGE_END; // bottom of space
    c.insets = new Insets(10, 0, 0, 0); // top padding
    c.gridx = 1; // aligned with button 2
    c.gridwidth = 2; // 2 columns wide
    c.gridy = 2; // third row
    pane.add(button, c);
  }

  /**
   * Create the GUI and show it. For thread safety, this method should be
   * invoked from the event-dispatching thread.
   */
  private static void createAndShowGUI() {
    // Create and set up the window.
    JFrame frame = new JFrame("GridBagLayoutDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Set up the content pane.
    addComponentsToPane(frame.getContentPane());

    // Display the window.
    frame.pack();
    frame.setVisible(true);
  }

  public static void main(String[] args) {
    // Schedule a job for the event-dispatching thread:
    // creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        createAndShowGUI();
      }
    });
  }
}


5. Latihan (Box Layout)
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

public class BoxLayoutTest {

  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("BoxLayout Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    BoxLayout boxLayout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS); // top to bottom
    frame.setLayout(boxLayout);
    frame.add(new JButton("Button 1"));
    frame.add(new JButton("Button 2"));
    frame.add(new JButton("Button 3"));
    frame.pack();

    frame.setVisible(true);
  }
}

6. Latihan (Grid Layout)

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class GridLayoutTest {

  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("GridLayout Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(3, 2));
    frame.add(new JButton("Button 1"));
    frame.add(new JButton("Button 2"));
    frame.add(new JButton("Button 3"));
    frame.add(new JButton("Button 4"));
    frame.add(new JButton("Button 5"));
    frame.add(new JButton("Button 6"));
    frame.add(new JButton("Button 7"));
    frame.add(new JButton("Button 8"));
    frame.pack();
    frame.setVisible(true);
  }
}


7. Latihan (JTextField)

import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class JPasswordFieldTest extends JFrame {

  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("JTextField Test");
    frame.setLayout(new GridLayout(2, 2));
    JLabel label = new JLabel("User Name:", SwingConstants.RIGHT);
    JLabel label2 = new JLabel("Password:", SwingConstants.RIGHT);
    JTextField userNameField = new JTextField(20);
    JPasswordField passwordField = new JPasswordField();
    frame.add(label);
    frame.add(userNameField);
    frame.add(label2);
    frame.add(passwordField);
    frame.setSize(200, 70);
    frame.setVisible(true);
  }
}


8. Latihan (Frame)


import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JFrame;


public class Utama extends JFrame {
public Utama() {
super("My Frame");
//setTitle("My Frame");
setSize(300, 100);
// setSize(new Dimension(300,200));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
// show();
}
public static void main(String[] args) {
Utama u = new Utama();
JButton button = new JButton("OK");
// Container container = u.getContentPane();
// container.add(button);
u.getContentPane().add(button);
u.add(button);
}
}

9. Latihan (TryBox Layout)
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
public class TryBoxLayout {
  public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Box Layout");
    aWindow.setBounds(30, 30, 300, 300);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // Create left column of radio buttons
    Box left = Box.createVerticalBox();
    ButtonGroup radioGroup = new ButtonGroup();
    JRadioButton rbutton;
    radioGroup.add(rbutton = new JRadioButton("Red"));
    left.add(rbutton);
    radioGroup.add(rbutton = new JRadioButton("Green"));
    left.add(rbutton);
    radioGroup.add(rbutton = new JRadioButton("Blue"));
    left.add(rbutton);
    radioGroup.add(rbutton = new JRadioButton("Yellow"));
    left.add(rbutton);
    Box right = Box.createVerticalBox();
    right.add(new JCheckBox("A"));
    right.add(new JCheckBox("B"));
    right.add(new JCheckBox("C"));
    Box top = Box.createHorizontalBox();
    top.add(left);
    top.add(right);
    Container content = aWindow.getContentPane();
    content.setLayout(new BorderLayout());
    content.add(top, BorderLayout.CENTER);
    aWindow.pack();
    aWindow.setVisible(true);
  }
}

10. Latihan (Try Card Layout)


import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TryCardLayout extends JPanel implements ActionListener {
  CardLayout card = new CardLayout(50, 50); // Create layout
  public TryCardLayout() {
    setLayout(card);
    JButton button;
    for (int i = 1; i <= 6; i++) {
      add(button = new JButton(" Press " + i), "Card" + i); // Add a button
      button.addActionListener(this); // Add listener for button
    }
  }
  // Handle button events
  public void actionPerformed(ActionEvent e) {
    card.next(this); // Switch to the next card
  }
  public static void main(String[] args) {
    JFrame aWindow = new JFrame();
    aWindow.setBounds(30, 30, 300, 300);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    aWindow.getContentPane().add(new TryCardLayout());
    aWindow.setVisible(true);
  }
}

11. Latihan (Google dengan Koneksi Internet)
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.MalformedInputException;
import java.text.DateFormat.Field;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import org.omg.CORBA.portable.InputStream;

import frame.addActionListener;


public class MYGOOGLE {
protected Object field;

public static void main (String[] args) {
final JFrame frame = new JFrame("PIS 10-02");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("My google: " + frame.getWidth());
frame.setResizable(false);
frame.setAlwaysOnTop(true);
frame.getContentPane().setBackground(Color.WHITE);
frame.setLocationRelativeTo(null);
frame.setSize(450, 200);
frame.setLayout(new FlowLayout());

final JLabel label = new JLabel("ENTER THE URL");
label.setFont(new Font("", 10, 40));// 10->12, 40->80
label.setForeground(Color.BLUE);
frame.add(label, BorderLayout.CENTER);
final JTextField txt = new JTextField(25);
txt.setToolTipText("www.facebook.com");
frame.add(txt);
JButton button = new JButton("SEARCH");
button.setForeground(Color.BLACK);
button.setBackground(Color.ORANGE);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (txt.getText().equals("")) {
System.out.println("tdiak ada teks yang ditulis!");
JOptionPane.showMessageDialog(frame, label, "Judul Pesan", 1);
} else {
// frame.setVisible(false);
JFrame frame2 = new JFrame("Frame Hasil");
frame.setSize(600, 400);
frame.setLocationRelativeTo(null);
final JTextPane textPane = new JTextPane();
final JScrollPane scrollPane = new JScrollPane(textPane);
panggilKoneksi(txt, textPane);
frame2.add(scrollPane);
frame2.setVisible(true);
}
}
private void panggilKoneksi(final JTextField field, final JTextPane textPane) {
try {
URL url = new URL(field.getText());
URLConnection con = url.openConnection();
java.io.InputStream is = con.getInputStream();
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] b = new byte[10];
int len;
while ((len =  is.read(b, 0, b.length)) != -1) {
os.write(b, 0, len);
}
textPane.setText(new String(os.toByteArray()));
os.close();
is.close();
} catch (MalformedInputException e) {
JLabel label2 = new JLabel("Salah tulis di URL");
JOptionPane.showMessageDialog(frame, label2, "Kesalahan Tulis", 1);
} catch (Exception e) {
JLabel label3 = new JLabel("Salah tulis di URL ini");
JOptionPane.showMessageDialog(frame, label3, "Kesalahan Tulis", 0);
}
}
});
frame.add(button);
frame.setVisible(true);
}

}


12. Latihan (Button dan Frame)
import java.awt.Button;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;


public class iseng {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.setSize(300,100);
frame.setLocationRelativeTo(null);
JLabel michelle = new JLabel("masukkin kata");
JTextField txt = new JTextField(10);
JButton button = new JButton("klik dong");
frame.add(michelle);
frame.add(txt);
frame.add(button);
frame.setVisible(true);
}

}

13. Latihan (JLabel with Image on)
import java.awt.FlowLayout;
import java.awt.HeadlessException;



import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main extends JFrame {
  public Main() throws HeadlessException {
    setSize(300300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout(FlowLayout.LEFT));

    Icon icon = new ImageIcon("a.png");
    JLabel label1 = new JLabel("Full Name :", icon, JLabel.LEFT);

    JLabel label2 = new JLabel("Address :", JLabel.LEFT);
    label2.setIcon(new ImageIcon("b.png"));

    getContentPane().add(label1);
    getContentPane().add(label2);
  }

  public static void main(String[] args) {
    new Main().setVisible(true);
  }
}

14. Latihan (Simpel JDialog)
import java.awt.BorderLayout;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class AboutDialog extends JDialog implements ActionListener {
  public AboutDialog(JFrame parent, String title, String message) {
    super(parent, title, true);
    if (parent != null) {
      Dimension parentSize = parent.getSize()
      Point p = parent.getLocation()
      setLocation(p.x + parentSize.width / 4, p.y + parentSize.height / 4);
    }
    JPanel messagePane = new JPanel();
    messagePane.add(new JLabel(message));
    getContentPane().add(messagePane);
    JPanel buttonPane = new JPanel();
    JButton button = new JButton("OK")
    buttonPane.add(button)
    button.addActionListener(this);
    getContentPane().add(buttonPane, BorderLayout.SOUTH);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    pack()
    setVisible(true);
  }
  public void actionPerformed(ActionEvent e) {
    setVisible(false)
    dispose()
  }
  public static void main(String[] a) {
    AboutDialog dlg = new AboutDialog(new JFrame()"title""message");
  }

}



15. Latihan (JMenuBar)
import java.awt.event.KeyEvent;



import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MenuCreation {

  public static void main(final String args[]) {
    JFrame frame = new JFrame("MenuSample Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(fileMenu);

    // File->New, N - Mnemonic
    JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
    fileMenu.add(newMenuItem);

    frame.setJMenuBar(menuBar);
    frame.setSize(350250);
    frame.setVisible(true);
  }
}

Tidak ada komentar:

Posting Komentar

Entri Populer