Hallöchen
ich soll mit Java Swing einen Ascii Converter bauen.
Von char in Ascii kann ich konvertieren, aber es funktioniert nicht andersherum.
Vielleicht kann mir jemand einen Tipp geben:/
Die Methide jButton2ActionPerformedAscii soll eig von Ascii in char zurückgeben
Liebe Grüße
Katharina
ich soll mit Java Swing einen Ascii Converter bauen.
Von char in Ascii kann ich konvertieren, aber es funktioniert nicht andersherum.
Vielleicht kann mir jemand einen Tipp geben:/
Code:
public class Ex1_2 {
public static void main(String[] args)
{
SwingApp app = new SwingApp(350, 250); // set app window size
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class SwingApp extends JFrame implements ActionListener
{
private JButton characterButton, asciiButton;
private JLabel c, a, yellow1, orange2, yellow2, orange1;
private JTextField character, ascii;
private final DrawPanel panel; // class variable
public SwingApp(int width, int height) // class constructor
{
super(); // call to super class constructor
Container pane = super.getContentPane(); // create layout, set colors
panel = new DrawPanel(); // add GUI components
panel.setBackground(Color.green);
// add other GUI components to the panel
orange1 = new JLabel("Char<-> ASCII converter", JLabel.CENTER);
orange1.setForeground(Color.blue);
orange1.setBackground(Color.orange);
orange1.setOpaque(true);
c = new JLabel(" ");
c.setForeground(Color.black);
c.setBackground(Color.green);
c.setOpaque(true);
a = new JLabel(" ");
a.setForeground(Color.black);
a.setBackground(Color.green);
a.setOpaque(true);
yellow1 = new JLabel();
yellow1.setBackground(Color.yellow);
yellow1.setOpaque(true);
yellow2 = new JLabel();
yellow2.setBackground(Color.yellow);
yellow2.setOpaque(true);
character = new JTextField(" ");
ascii = new JTextField(" ");
orange2 = new JLabel();
orange2.setBackground(Color.orange);
orange2.setOpaque(true);
characterButton = new JButton("Character");
characterButton.setForeground(Color.black);
characterButton.setBackground(Color.green);
characterButton.setOpaque(true);
asciiButton = new JButton("Ascii");
asciiButton.setForeground(Color.black);
asciiButton.setBackground(Color.green);
asciiButton.setOpaque(true);
pane.setLayout(null);
orange1.setBounds(0, 0, 350,20);
c.setBounds(0,20,175,250);
a.setBounds(175,20,175,250);
yellow1.setBounds(0,100,170,80);
yellow2.setBounds(185,100,170,80);
character.setBounds(70,105,20,25);
ascii.setBounds(240,105,45,25);
orange2.setBounds(0,182,350,20);
characterButton.setBounds(0,20,175,75);
asciiButton.setBounds(175,20,175,75);
pane.add(orange1);
pane.add(character);
pane.add(ascii);
pane.add(yellow1);
pane.add(yellow2);
pane.add(orange2);
pane.add(c);
pane.add(a);
pane.add(characterButton);
pane.add(asciiButton);
pane.add(panel);
characterButton.addActionListener(this);
asciiButton.addActionListener(this);
pane.add(panel); // add panel to the main app container
Toolkit toolkit = Toolkit.getDefaultToolkit(); // optionally position JFrame
Dimension screenSize = toolkit.getScreenSize(); // in the middle of the screen
super.setLocation((screenSize.width - width)/2, (screenSize.height - height)/2);
super.setTitle("charakter <-> ASCII converter"); // set desired window title
super.setSize(width, height); // set desired window size
super.setVisible(true); // make the app window visible
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt){
String char1;
int ascii1 = 0;
boolean isAChar = true;
try{
char1 = character.getText();
for (int j=0; j<=1; j++)
{
char c2 = char1.charAt(j);
ascii1 = (int) c2;
}
ascii.setText(String.valueOf(ascii1));
orange2.setText("conversion completed");
orange2.setForeground(Color.WHITE);
isAChar=true;
}catch (IndexOutOfBoundsException e){
orange2.setText("Not a Character");
isAChar=false;
}
}
private void jButton2ActionPerformedAscii(java.awt.event.ActionEvent evt){
String newAscii;
char aChar = 0;
try{
newAscii = ascii.getText();
for(int i = 0; i<=1; i++){
int ascii2= Integer.parseInt(newAscii);
char c2 = (char) ascii2;
aChar = c2;
}
character.setText(String.valueOf(aChar));
orange2.setText("conversion completed");
orange2.setForeground(Color.WHITE);
}catch(NumberFormatException e){
orange2.setText("Not a Number");
orange2.setForeground(Color.WHITE);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == characterButton){
jButton2ActionPerformed(e);
}
if(e.getSource() == asciiButton){
jButton2ActionPerformedAscii(e);
}
}
class DrawPanel extends JPanel // main window panel
{
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g); // must be the 1st line
// add here other code for drawing on panel
}
}
}
Die Methide jButton2ActionPerformedAscii soll eig von Ascii in char zurückgeben
Liebe Grüße
Katharina