Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // paket yang diperlukan
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- class ContohTextArea extends JFrame{
- // attribut
- //membuat tombol
- JButton tombol1 = new JButton("ambil nilai text area");
- JButton tombol2 = new JButton("set nilai ");
- JLabel label = new JLabel("nilai text area");
- // JTextArea(String text, int rows, int columns)
- JTextArea jta = new JTextArea("text awal",5,5);
- // menambahkan scroll ke text area
- JScrollPane sp;
- //konstruktor
- public ContohTextArea(){
- //frame seting---------------------------------------------------------------------
- //memanggil konstruktor kelas induk (JFrame)
- super("Contoh Textarea");
- //seting besar frame 400 x 400 px
- this.setSize(200,250);
- //seting agar bisa ditutup
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- //seting kemunculan frame di tengah window
- this.setLocationRelativeTo(null);
- //seting layout frame
- this.getContentPane().setLayout(null);
- //frame seting---------------------------------------------------------------------
- // setbounds(x,y,lebar,tinggi)
- tombol1.setBounds(10,10,150,25);
- tombol2.setBounds(10,40,150,25);
- label.setBounds(10,70,150,25);
- sp = new JScrollPane(jta);
- sp.setSize(150,100);
- sp.setLocation(10,110);
- /*
- # kalau mau wrap word pake ini, biar text gk bisa nyamping
- jta.setLineWrap(true);
- jta.setWrapStyleWord(true);
- */
- //menambahkan komponen ke frame dan seting letak
- this.add(tombol1);
- this.add(tombol2);
- this.add(label);
- this.add(sp);
- //seting aksi tombol
- tombol1.addActionListener(new ActionListener( ) {
- public void actionPerformed(ActionEvent e){
- // kode disini; cek keadaan check box
- String teksnya = jta.getText();
- label.setText(teksnya);
- }
- });
- tombol2.addActionListener(new ActionListener( ) {
- public void actionPerformed(ActionEvent e){
- // kode disini; cek keadaan check box ganti keadaan chek box
- jta.setText("teksnya bisa panjang \nlho...\n ini baris 3");
- }
- });
- // menampilkan frame
- show();
- }
- public static void main(String[] args){
- new ContohTextArea();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment