In the course I was tasked to make an applet keyboard. Prior to that, he did not have much to deal with applets. I read various articles. In general, there is a code that works correctly. Codes for the buttons he wrote. The problem is that I can’t think of a code for “Shift”. On the Internet, I found methods that allow changing the register, but they do not quite fit me. Please advise how to act. If you do not mind, then help with the code, I will be grateful.
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Keyboard extends JApplet implements ActionListener { String row1[] = {"1","2","3","4","5","6","7","8","9","0","Backspace"}; String row2[] = {"q","w","e","r","t","y","u","i","o","p","Enter"}; String row3[] = {"a","s","d","f","g","h","j","k","l",":","Shift"}; String row4[] = {"z","x","c","v","b","n","m",",",".","?","Spacebar"}; JButton first[]; JButton second[]; JButton third[]; JButton fourth[]; JTextArea ta; boolean shiftPressed = false; public void init(){ ta = new JTextArea(); ta.setFont(new Font("Times New Roman", 1, 18)); JLabel l = new JLabel("Enter your message here:" ); l.setFont(new Font("TimesNewRoman",Font.BOLD,14)); setLayout(new BorderLayout()); JPanel jpNorth = new JPanel(); JPanel jpCenter = new JPanel(); JPanel jpButtons = new JPanel(); add(jpNorth, BorderLayout.NORTH); add(jpCenter, BorderLayout.CENTER); add(jpButtons, BorderLayout.SOUTH); jpNorth.setLayout(new BorderLayout()); jpNorth.add(l, BorderLayout.WEST); jpNorth.add(l, BorderLayout.SOUTH); jpCenter.setLayout(new BorderLayout()); jpCenter.add(ta, BorderLayout.WEST); jpCenter.add(ta, BorderLayout.CENTER); jpButtons.setLayout(new GridLayout(4,1)); first = new JButton[row1.length]; JPanel p = new JPanel(new GridLayout(1, row1.length)); for(int i = 0; i < row1.length; i++) { JButton bt = new JButton(row1[i]); bt.setPreferredSize(new Dimension(100,50)); first[i] = bt; p.add(first[i]); } jpButtons.add(p); second = new JButton[row2.length]; p = new JPanel(new GridLayout(1, row2.length)); for(int i = 0; i < row2.length; i++) { second[i] = new JButton(row2[i]); p.add(second[i]); } jpButtons.add(p); third = new JButton[row3.length]; p = new JPanel(new GridLayout(1, row3.length)); for(int i = 0; i < row3.length; i++) { third[i] = new JButton(row3[i]); p.add(third[i]); } jpButtons.add(p); fourth = new JButton[row4.length]; p = new JPanel(new GridLayout(1, row4.length)); for(int i = 0; i < row4.length; ++i) { fourth[i] = new JButton(row4[i]); p.add(fourth[i]); } jpButtons.add(p); for(JButton bt : first) bt.addActionListener(this); for(JButton bt : second) bt.addActionListener(this); for(JButton bt : third) bt.addActionListener(this); for(JButton bt : fourth) bt.addActionListener(this); } public void actionPerformed(ActionEvent ae){ String ac = ae.getActionCommand(); String text; switch (ac) { case "Enter": ta.setText(ta.getText()+"\n"); break; case "Spacebar": ta.setText(ta.getText()+" "); break; case "Backspace": text = ta.getText(); ta.setText(text.substring(0, text.length() - 1)); break; case "Shift": shiftPressed = true; break; default: register(); break; } } private String register() { if (shiftPressed == true) { row1[] = {"1","2","3","4","5","6","7","8","9","0","Backspace"}; row2[] = {"Q","W","E","R","T","Y","U","I","O","P","Enter"}; row3[] = {"A","S","D","F","G","H","J","K","L",":","Shift"}; row4[] = {"Z","X","C","V","B","N","M",",",".","?","Spacebar"}; shiftPressed = false; } else { row1[] = {"1","2","3","4","5","6","7","8","9","0","Backspace"}; row2[] = {"q","w","e","r","t","y","u","i","o","p","Enter"}; row3[] = {"a","s","d","f","g","h","j","k","l",":","Shift"}; row4[] = {"z","x","c","v","b","n","m",",",".","?","Spacebar"}; } return ta.setText(ta.getText() + String.valueOf(ac)); } }