hafidh

JAVA GUI : Contoh TextArea

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