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)); } } 

    2 answers 2

    Declare a variable switch:

     private boolean shiftPressed = false; 

    And in switch'e something like this:

     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 = (shiftPressed) ? false : true; break; default: text = String.valueOf(ac); text = (shiftPressed) ? text.toUpperCase() : text.toLowerCase(); ta.setText(ta.getText() + text); break; } 

    I think this is better than using your register method. You have row (1-4) three times in the end is repeated.

    • Thank you very much. Since you helped me, can I ask you to explain some points? - Muscled Boy
    • @MuscledBoy, ask. I didn’t work with java almost, but I’ll try to help - UserName
    • 1) I used part of the for code (JButton bt: first) bt.addActionListener (this); to add an interface, and how more precisely to decipher for ? - Muscled Boy
    • 2) I guess what the lines are doing ** shiftPressed = (shiftPressed)? false: true; ** and text = (shiftPressed)? text.toUpperCase (): text.toLowerCase (); . in the first case, the button has if it was false - will be true when pressed and in the second, we change the fonts (by the way, I thought that these methods change the case of the entire string, but only entered, but it turns out they can also be used to change the register of the future character entered? ???) - Muscled Boy
    • And how in the second paragraph is used? : how are the conditions of action taking place? - Muscled Boy

    in the Keyboard class, you keep the state of the shift (boolean pressed / released), you create a method in which you add characters to ta , taking into account the state of the shift, and you call this method in default:

    • Well, so that all this was ThreadSafe, then you need to keep the state of the shift in Atomic - user1919757
    • I do not understand what you mean: "to make it all ThreadSafe, then you need to keep the state of the shift in Atomic" ? I just did not come across such things. If you could explain what it is. - Muscled Boy
    • You see, I am not strong in the streams, and I know very little about them - Muscled Boy
    • correct, if I may say so my code, otherwise I don’t understand anything at all. I will be grateful to you. - Muscled Boy