public class Konsole implements ActionListener, KeyListener{
private JFrame
frame;
private JPanel
inputContainer;
private JButton
submit;
private JTextArea
textArea;
private JTextField
inputField;
protected JScrollPane
scroll;
public Konsole(){
/// Frame initialisieren \\\
frame = new JFrame ("Konsole - Konsole.");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.GRAY);
frame.setIconImage(Globalizer.image("resource/Icon.png"));
/// Konsolen-Output initialisieren \\\
textArea = new JTextArea("");
textArea.setEditable(false);
textArea.setPreferredSize(new Dimension(960, 540));
textArea.setBackground(Color.BLACK);
Font font = new Font("Verdana", Font.BOLD, 12);
textArea.setFont(font);
textArea.setForeground(Color.WHITE);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
/// Panel initialisieren \\\
inputContainer = new JPanel();
inputContainer.setLayout(new BorderLayout());
/// Scroll-Bar \\\
scroll = new JScrollPane();
scroll.getViewport().add(textArea);
scroll.getVerticalScrollBar().setUnitIncrement(10);
scroll.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setPreferredSize(new Dimension(960, 540));
/// Kommandozeile + Button \\\
inputField = new JTextField();
inputField.addKeyListener(this);
inputField.setBackground(Color.GRAY);
Font font2 = new Font("Verdana", Font.BOLD, 12);
inputField.setFont(font2);
inputField.setForeground(Color.WHITE);
submit = new JButton ("Submit");
submit.setPreferredSize(new Dimension(120, 50));
submit.addActionListener(this);
inputContainer.add(inputField, BorderLayout.CENTER);
inputContainer.add(submit, BorderLayout.EAST);
// Alles zusammenfügen \\\
frame.add(inputContainer, BorderLayout.SOUTH);
frame.add(scroll, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void enter(String text){
textArea.append(text + "\n");
}
ArrayList<String> history = new ArrayList<String>();
int latestthing = 0;
boolean hd = true;
public String getHistoryAbobe(){
if(history.isEmpty()){return "";}
if(hd){
latestthing = history.size() - 1;
hd = false;
}
if(history.get(latestthing) == null){return "";}
String a = history.get(latestthing)==null?"":history.get(latestthing);
if(latestthing > 0){
latestthing--;
}
return a;
}
public String getHistoryBelow(){
if(history.isEmpty()){return "";}
if(hd){
latestthing = history.size() - 1;
hd = false;
}
if(history.get(latestthing) == null){return "";}
String a = history.get(latestthing)==null?"":history.get(latestthing);
if(latestthing > 0){
latestthing++;
}
return a;
}
void processInput(){
if((inputField.getText() != "") || (inputField.getText() != null)){
String in = inputField.getText();
history.add(in);
inputField.setText("");
GlobalUtil.perform(in);
}
}
void p(String in){
GlobalUtil.perform(in);
}
@Override
public void keyReleased(KeyEvent arg0) {
}
@Override
public void keyTyped(KeyEvent arg0) {
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
processInput();
}
if(e.getKeyCode() == KeyEvent.VK_UP){
inputField.setText(getHistoryAbobe());
}
if(e.getKeyCode() == KeyEvent.VK_DOWN){
inputField.setText(getHistoryBelow());
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == submit){
processInput();
}
}
}