trizehn

UpperLowerCaseConverter

Jan 13th, 2021 (edited)
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.15 KB | None | 0 0
  1.  
  2. /**
  3.  * Sebuah program yang dapat format text menjadi Upper-case, Lower-case
  4.  * maupun case tersebut di invert
  5.  *
  6.  * @author Daffa Tristan Firdaus
  7.  * @version 14 Januari 2021
  8.  */
  9.  
  10. import javax.swing.*;
  11. import javax.swing.JFrame;
  12. import java.awt.*;
  13. import java.awt.event.ActionEvent;
  14. import java.awt.event.ActionListener;
  15. public class UpperLowerCaseConverter extends JFrame
  16. {
  17.     private JButton button1, button2, button3, button4, button5, button6;
  18.     private JTextArea textarea1, textarea2;
  19.     public static String invertCase(String str)
  20.     {
  21.         char[] chars = str.toCharArray();
  22.         for (int i = 0; i < chars.length; i++) {
  23.             chars[i] = Character.isUpperCase(chars[i])
  24.                                 ? Character.toLowerCase(chars[i])
  25.                                 : Character.toUpperCase(chars[i]);
  26.         }
  27.         return new String(chars);
  28.     }
  29.     public UpperLowerCaseConverter() {
  30.         JFrame f = new JFrame("Upper-case Lower-case Converter");
  31.         f.setTitle("Upper-case Lower-case Converter");
  32.         f.setSize(500,400);
  33.  
  34.         JPanel contentPane = new JPanel(null);
  35.         contentPane.setPreferredSize(new Dimension(500,400));
  36.         contentPane.setBackground(Color.DARK_GRAY);
  37.  
  38.         button1 = new JButton();
  39.         button1.setBounds(18,176,120,35);
  40.         button1.setBackground(new Color(214,217,223));
  41.         button1.setForeground(new Color(0,0,0));
  42.         button1.setEnabled(true);
  43.         button1.setFont(new Font("sansserif",0,12));
  44.         button1.setText("Upper-Case");
  45.         button1.setVisible(true);
  46.         button1.addActionListener(new ActionListener() {
  47.             @Override
  48.             public void actionPerformed(ActionEvent e) {
  49.                 String str = textarea1.getText();
  50.                 textarea2.setText(str.toUpperCase());
  51.             }
  52.         });
  53.  
  54.         button2 = new JButton();
  55.         button2.setBounds(145,176,120,35);
  56.         button2.setBackground(new Color(214,217,223));
  57.         button2.setForeground(new Color(0,0,0));
  58.         button2.setEnabled(true);
  59.         button2.setFont(new Font("sansserif",0,12));
  60.         button2.setText("Lower-Case");
  61.         button2.setVisible(true);
  62.         button2.addActionListener(new ActionListener() {
  63.             @Override
  64.             public void actionPerformed(ActionEvent e) {
  65.                 String str = textarea1.getText();
  66.                 textarea2.setText(str.toLowerCase());
  67.             }
  68.         });
  69.  
  70.         button3 = new JButton();
  71.         button3.setBounds(404,280,90,35);
  72.         button3.setBackground(new Color(214,217,223));
  73.         button3.setForeground(new Color(0,0,0));
  74.         button3.setEnabled(true);
  75.         button3.setFont(new Font("sansserif",0,12));
  76.         button3.setText("Copy");
  77.         button3.setVisible(true);
  78.         button3.addActionListener(new ActionListener() {
  79.             @Override
  80.             public void actionPerformed(ActionEvent e) {
  81.                 textarea2.selectAll();
  82.                 textarea2.copy();
  83.             }
  84.         });
  85.        
  86.         button4 = new JButton();
  87.         button4.setBounds(404,75,90,35);
  88.         button4.setBackground(new Color(214,217,223));
  89.         button4.setForeground(new Color(0,0,0));
  90.         button4.setEnabled(true);
  91.         button4.setFont(new Font("sansserif",0,12));
  92.         button4.setText("Paste");
  93.         button4.setVisible(true);
  94.         button4.addActionListener(new ActionListener() {
  95.             @Override
  96.             public void actionPerformed(ActionEvent e) {
  97.                 textarea1.paste();
  98.             }
  99.         });
  100.        
  101.         button5 = new JButton();
  102.         button5.setBounds(273,176,120,35);
  103.         button5.setBackground(new Color(214,217,223));
  104.         button5.setForeground(new Color(0,0,0));
  105.         button5.setEnabled(true);
  106.         button5.setFont(new Font("sansserif",0,12));
  107.         button5.setText("Invert Case");
  108.         button5.setVisible(true);
  109.         button5.addActionListener(new ActionListener() {
  110.             @Override
  111.             public void actionPerformed(ActionEvent e) {
  112.                 String str = textarea1.getText();
  113.                 textarea2.setText(invertCase(str));
  114.             }
  115.         });
  116.        
  117.         button6 = new JButton();
  118.         button6.setBounds(404,30,90,35);
  119.         button6.setBackground(new Color(214,217,223));
  120.         button6.setForeground(new Color(0,0,0));
  121.         button6.setEnabled(true);
  122.         button6.setFont(new Font("sansserif",0,12));
  123.         button6.setText("Clear");
  124.         button6.setVisible(true);
  125.         button6.addActionListener(new ActionListener() {
  126.             @Override
  127.             public void actionPerformed(ActionEvent e) {
  128.                 textarea1.setText("");
  129.             }
  130.         });
  131.  
  132.         textarea1 = new JTextArea();
  133.         textarea1.setBounds(18,18,380,150);
  134.         textarea1.setBackground(new Color(255,255,255));
  135.         textarea1.setForeground(new Color(0,0,0));
  136.         textarea1.setEnabled(true);
  137.         textarea1.setFont(new Font("sansserif",0,12));
  138.         textarea1.setText("Input");
  139.         textarea1.setBorder(BorderFactory.createBevelBorder(1));
  140.         textarea1.setVisible(true);
  141.  
  142.         textarea2 = new JTextArea();
  143.         textarea2.setBounds(18,220,380,150);
  144.         textarea2.setBackground(new Color(255,255,255));
  145.         textarea2.setForeground(new Color(0,0,0));
  146.         textarea2.setEnabled(true);
  147.         textarea2.setFont(new Font("sansserif",0,12));
  148.         textarea2.setText("Output");
  149.         textarea2.setBorder(BorderFactory.createBevelBorder(1));
  150.         textarea2.setVisible(true);
  151.  
  152.         contentPane.add(button1);
  153.         contentPane.add(button2);
  154.         contentPane.add(button3);
  155.         contentPane.add(button4);
  156.         contentPane.add(button5);
  157.         contentPane.add(button6);
  158.         contentPane.add(textarea1);
  159.         contentPane.add(textarea2);
  160.  
  161.         f.add(contentPane);
  162.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  163.         f.setLocationRelativeTo(null);
  164.         f.pack();
  165.         f.setVisible(true);
  166.     }
  167.    
  168.     public static void main(String[] args){
  169.         UpperLowerCaseConverter ulcc = new UpperLowerCaseConverter();
  170.     }
  171. }
Add Comment
Please, Sign In to add comment