I'm developing an application for my computer science classes. The task is to write a calculator but without using JTextField
s or JTextArea
s. I've come up with an idea of implementing KeyListener
which works nice in both appletviewer and JFrame
but doesn't work at all in Google Chrome (and probably other browsers).
Here're my code snippets.
//- BinaryCalc.java
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class BinaryCalc extends JApplet implements KeyListener {
private JPanel panel;
public BinaryCalc() {
super();
panel = new JPanel();
this.add(panel);
panel.addKeyListener(this);
panel.requestFocusInWindow();
}
@Override
public void init() {
JOptionPane.showMessageDialog(this, "applet");
panel.setFocusable(true);
panel.requestFocus();
}
public void keyPressed(KeyEvent e) {
JOptionPane.showMessageDialog(this, (char) e.getKeyCode());
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public JPanel getPanel() { return panel; }
public static void main(String args[]) {
JFrame frame = new JFrame("Binary Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(320, 240));
BinaryCalc kalkulator = new BinaryCalc();
frame.add(kalkulator);
frame.pack();
frame.setVisible(true);
kalkulator.getPanel().requestFocusInWindow();
}
}
And HTML file containing my applet.
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Kalkulator binarny</title>
</head>
<body>
<div style="text-align: center; border-bottom: 1px solid black">
<h1>Kalkulator Binarny</h1>
</div>
<br/>
<object style="display: block; margin: auto;" type="application/x-java-applet" width="320" height="240">
<param name="code" value="BinaryCalc.class" />
<!--- <param name="archive" value="Liczba.jar" /> -->
What a terrible failure: applet failed to load!
</object>
<br/>
</body>
</html>
Any ideas?