Text based java application

The main idea is to try to create an application that will look like a standard Linux-type terminal. Now I am limited to Java, and this is what I want.

  • Main window (independent JFrame) with top menu (well, I know how to do this)
  • The entire area under the menu bar is for the β€œconsole”, with which users can interact by entering commands (I have no idea about this).

Is there any simple enough way to do such a thing. This should be a word processing application and should work independently of the system and autonomously. All working logic must be derived from user input.

Any ideas?

+6
source share
2 answers

The "console" can be implemented using JTextPane . Whenever the window has focus, you can write any click on the text field and analyze it, execute each time you press 'Enter' (use KeyListener and implement keyTyped ).

+2
source

I think you need two text components, a text area and a text field.

If the user enters text in the text box below and calls Enter , the text moves to the text area where it is no longer being edited. Processing is then performed, as a result, the result (or multiples) is printed into the text area, and the next line can be entered.

The top area may have a JScrollPane . One way or another, buffering must be handled.

Here is a simple, complete, and executable example:

 import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.plaf.ActionMapUIResource; /** CmdPrompt @author Stefan Wagner @date Mi 25. Apr 17:27:19 CEST 2012 (c) GPLv3 */ public class CmdPrompt extends JFrame { private static final String progname = "CmdPrompt 0.1"; private JTextField input; private JTextArea history; public CmdPrompt () { super (progname); JPanel mainpanel = new JPanel (); mainpanel.setLayout (new BorderLayout ()); this.getContentPane ().add (mainpanel); input = new JTextField (80); history = new JTextArea (); mainpanel.add (history, BorderLayout.CENTER); mainpanel.add (input, BorderLayout.SOUTH); ActionMap actionMap = new ActionMapUIResource (); actionMap.put ("enter", new AbstractAction () { @Override public void actionPerformed (ActionEvent e) { String cmd = input.getText (); String sofar = history.getText (); history.setText (sofar + "\n> " + cmd + "\n" + processCmd (cmd)); input.setText (""); } }); InputMap keyMap = new ComponentInputMap (input); keyMap.put (KeyStroke.getKeyStroke (KeyEvent.VK_ENTER, 0), "enter"); SwingUtilities.replaceUIActionMap (input, actionMap); SwingUtilities.replaceUIInputMap (input, JComponent.WHEN_IN_FOCUSED_WINDOW, keyMap); setSize (400, 400); setLocation (100, 100); setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); setVisible (true); } private void center () { Toolkit tk = Toolkit.getDefaultToolkit (); Dimension screen = tk.getScreenSize (); Dimension d = getSize (); setLocation ((screen.width - d.width) / 2, (screen.height - d.height) / 2); } public static String processCmd (String cmd) { String arr [] = cmd.split (" "); if ("rev".equals (arr [0])) { return reverse (cmd.substring (4)); } else if ("upp".equals (arr [0])) { return (cmd.substring (4)).toUpperCase (); } else if ("low".equals (arr [0])) { return (cmd.substring (4)).toLowerCase (); } else if ("help".equals (arr [0])) { return ("rev, upp, low, help"); } return "unknown command"; } public static String reverse (String cmd) { return (cmd.length () < 2) ? cmd : reverse (cmd.substring (1)) + cmd.charAt (0); } public static void main (final String args []) { Runnable runner = new Runnable () { public void run () { new CmdPrompt (); } }; EventQueue.invokeLater (runner); } } 
+4
source

Source: https://habr.com/ru/post/914116/


All Articles