hafidh

JAVA GUI : Jtextarea & JscrollPane

Nov 26th, 2011
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. // paket yang diperlukan
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.ActionEvent;
  6.  
  7. class ContohTextArea extends JFrame{
  8.     // attribut
  9.     //membuat tombol
  10.     JButton tombol1 = new JButton("ambil nilai text area");
  11.     JButton tombol2 = new JButton("set nilai ");
  12.     JLabel label = new JLabel("nilai text area");
  13.     // JTextArea(String text, int rows, int columns)
  14.     JTextArea jta = new JTextArea("text awal",5,5);
  15.     // menambahkan scroll ke text area
  16.     JScrollPane sp;
  17.    
  18.     //konstruktor
  19.     public ContohTextArea(){
  20.         //frame seting---------------------------------------------------------------------
  21.         //memanggil konstruktor kelas induk (JFrame)
  22.         super("Contoh Textarea");
  23.         //seting besar frame 400 x 400 px
  24.         this.setSize(200,250);
  25.         //seting agar bisa ditutup
  26.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  27.         //seting kemunculan frame di tengah window
  28.         this.setLocationRelativeTo(null);
  29.         //seting layout frame
  30.         this.getContentPane().setLayout(null);
  31.         //frame seting---------------------------------------------------------------------
  32.         // setbounds(x,y,lebar,tinggi)
  33.         tombol1.setBounds(10,10,150,25);
  34.         tombol2.setBounds(10,40,150,25);
  35.         label.setBounds(10,70,150,25);
  36.         sp  = new JScrollPane(jta);
  37.         sp.setSize(150,100);
  38.         sp.setLocation(10,110);
  39.         /*
  40.         # kalau mau wrap word pake ini, biar text gk bisa nyamping
  41.         jta.setLineWrap(true);
  42.         jta.setWrapStyleWord(true);
  43.         */
  44.         //menambahkan komponen ke frame dan seting letak
  45.         this.add(tombol1);
  46.         this.add(tombol2);
  47.         this.add(label);
  48.         this.add(sp);
  49.        
  50.         //seting aksi tombol
  51.         tombol1.addActionListener(new ActionListener( ) {
  52.             public void actionPerformed(ActionEvent e){
  53.                 // kode disini; cek keadaan check box
  54.                 String teksnya = jta.getText();
  55.                 label.setText(teksnya);
  56.             }
  57.         });
  58.         tombol2.addActionListener(new ActionListener( ) {
  59.             public void actionPerformed(ActionEvent e){
  60.                 // kode disini; cek keadaan check box ganti keadaan chek box
  61.                 jta.setText("teksnya bisa panjang \nlho...\n ini baris 3");
  62.             }
  63.         });
  64.         // menampilkan frame
  65.         show();
  66.        
  67.     }
  68.    
  69.     public static void main(String[] args){
  70.         new ContohTextArea();
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment