GUI:
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Squize extends JFrame implements ActionListener
{
JTextField eingabeFeld, ausgabeFeld;
JButton enterButton;
public Squize (String title)
{
super (title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,300);
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
eingabeFeld = new JTextField("");
eingabeFeld.setHorizontalAlignment(SwingConstants.LEFT);
cp.add(eingabeFeld,BorderLayout.NORTH);
enterButton = new JButton("ENTER");
cp.add(enterButton, BorderLayout.CENTER);
enterButton.addActionListener(this);
ausgabeFeld = new JTextField();
ausgabeFeld.setHorizontalAlignment(SwingConstants.RIGHT);
ausgabeFeld.setEnabled(false);
cp.add(ausgabeFeld,BorderLayout.SOUTH);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Commandlinehandler commandlinehandler = new Commandlinehandler();
commandlinehandler.getCommands(eingabeFeld.getText()); // run the Inputhandler
}
public static void main(String args[])
{
new Squize("Squize");
}
}
Commandlinehandler
(in der ich gerne auf ausgabeFeld von Squize zugreifen würde)
Code:
import java.io.*;
import java.lang.Math;
class Commandlinehandler
{
static int square(int n)
{
return n * n;
}
public void getCommands(String input_commands)
{
// String input_first = input_commands.substring(0,1);
// char null_stelle[] = new char[1];
// input_commands.getChars(1, 1+1, null_stelle, 0);
int commands_length = input_commands.length();
char input_commands_char[] = new char[input_commands.length() + 1];
input_commands_char = input_commands.toCharArray(); // copy the whole string to a char array
if(input_commands_char[0] == (char)10)
{
System.err.println("error: wrong command, try 'h' for helpi");
}
else
{
switch (input_commands_char[0])
{
case 's':
if(commands_length != 1 && input_commands_char[1] == ' ')
{
for(int i=2; i<=commands_length-1; i++)
{
if(Character.isDigit(input_commands_char[i]))
{
if(i == commands_length-1)
{
int input_number = Integer.parseInt(input_commands.substring(2));
System.out.println("The Square of " + input_number + ": " + square(input_number));
// Squize.ausgabeFeld.setText("The Square of " + input_number + ": " + square(input_number));
}
}
else
{
System.err.println("usage: e.g. 's 5' (square a number)");
}
}
}
else
{
System.err.println("usage: e.g. 's 5' (square a number)");
}
break;
case 'r':
if(commands_length != 1 && input_commands_char[1] == ' ')
{
for(int i=2; i<=commands_length-1; i++)
{
if(Character.isDigit(input_commands_char[i]))
{
if(i == commands_length-1)
{
int input_number = Integer.parseInt(input_commands.substring(2));
System.out.println("The Square Root of " + input_number + ": " + Math.sqrt(input_number));
}
}
else
{
System.err.println("usage: e.g. 'r 9' (square root a number)");
}
}
}
else
{
System.err.println("usage: e.g. 'r 9' (square root of a number)");
}
break;
case 'h':
System.out.println('\n' + "Help" + '\n' + '\n' + "s ... square a number (e.g. s 5)" + '\n' + "r ... square root a number (e.g. r 9)"
+ '\n' + "q ... quit (exit)" + '\n');
break;
default:
System.err.println("error: wrong command, try 'h' for help");
break;
}
}
}
}
Danke im Voraus !!!
mfg leon22
Lesezeichen