Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi im trying to build a console ( like windows cmd ).I guess i did the gui stuff correct but i cant carry users input to console.I mean if user types hello and enter console should display it.There are 2 classes one is the main other is console.

Java
import javax.swing.*;
    import javax.swing.text.StyledDocument;

    public class ana extends JFrame {
    
    	public JFrame frame;
    	public JTextPane console;
    	public  JTextField input;
    	public JScrollPane scrollpane;
    	public boolean trace= false;
    	public StyledDocument document;

    	public static void main(String[] args) {
            konsol k = new konsol();
            k.konsolum();
    	}
    }

This is the console class:

Java
import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    import javax.swing.JTextPane;
    import javax.swing.UIManager;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.Style;
    import javax.swing.text.StyleConstants;
    import javax.swing.text.StyledDocument;

    public class konsol   {
    	public JFrame frame;
    	public JTextPane console;
    	public  JTextField input;
    	public JScrollPane scrollpane;
    	public boolean trace= false;
    	public StyledDocument document;
        public void konsolum() {
    		try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    		} catch(Exception e) {}

    		frame = new JFrame();
    		frame.setTitle("Konsol");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		console = new JTextPane();
    		input = new JTextField();
    		scrollpane = new JScrollPane();
    		
    		frame.add(input,BorderLayout.SOUTH);
    		frame.add(scrollpane,BorderLayout.CENTER );
    		frame.setSize(660,350);
    		frame.setLocationRelativeTo(null);
    		frame.setResizable(false);
    		frame.setVisible(true);
    		frame.getContentPane().setBackground(new Color(50,50,50));

    		console.setEditable(false);
    		console.setFont(new Font("Courier New",Font.PLAIN,12));
    		console.setOpaque(false);
    		document = console.getStyledDocument();

    		scrollpane.getViewport().setOpaque(false);
    		scrollpane.setOpaque(false);
    		scrollpane.setBorder(null);

    		input.setEditable(true);
    		input.setCaretColor(Color.WHITE);
    		input.setForeground(Color.WHITE);
    		input.setOpaque(false);
    		input.setFont(new Font("Courier New",Font.PLAIN,12));
    		input.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String text=input.getText();
                }
            });

    		input.addKeyListener(new KeyListener(){
    			public void keyPressed(KeyEvent e) {
    			}
    			
    			public void keyReleased(KeyEvent e) {
    			}
    			
    			public void keyTyped(KeyEvent e) {
    			}
    		});
    	}
    }

I dont really know what to write inside `actionPerformed`. My main aim is to build a console which will understand commands and do stuff with them. Thanks

What I have tried:

addding console.setText(text);
to actionperformed but not workin
Posted
Comments
[no name] 23-Feb-17 10:50am    
http://www.cs.carleton.edu/faculty/dmusican/cs117s03/iocheat.html
Richard MacCutchan 23-Feb-17 12:35pm    
You just need to add cod to capture the Enter key in the textbox. Once you have that then you process the content as you require. What part of the above code is not working?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900