hi,
i'm just trying out some simple java gui programming using awt classes.
Code:
/*
Calculator_GUI.java
*/
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//start of class
public class Calculator_GUI extends JFrame {
//attributes
private JTextField textBox;
private JTextArea answerBox, msgBox;
private JButton resetBtn, suggestBtn;
//constructor
public Calculator_GUI() {
super("Calculator");
Container container = getContentPane();
container.setLayout(new FlowLayout());
//text box for input
textBox = new JTextField("0", 10);
textBox.setEditable(true);
container.add(textBox);
//reset button
resetBtn = new JButton("RESET");
container.add(resetBtn);
//register the event handlers
TextFieldHandler fieldHandler = new TextFieldHandler();
textBox.addActionListener(fieldHandler);
ButtonHandler btnHandler = new ButtonHandler();
resetBtn.addActionListener(btnHandler);
setSize(400, 300);
setVisible(true);
}
//inner class for handling textbox events
private class TextFieldHandler implements ActionListener {
//handle textbox event
public void actionPerformed(ActionEvent e) {}
}
//inner class for handling button events
private class ButtonHandler implements ActionListener {
//handle button event
public void actionPerformed(ActionEvent e) {
String text = textBox.getText();
JOptionPane.showMessageDialog(null, text);
textBox.setText("0");
}
}
//main function
public static void main (String args[]) {
Calculator_GUI cal = new Calculator_GUI();
cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
} //close brkt for class
the code is very simple. just a text field and a reset button.
i get funny problems when i try to run this on linux. the main problm is with the text editing. it doesn't show the real time changes when i try to edit. nor do i see the blinking cursor. i got frustrated, and tried running the class file on windows. and it worked! now, isn't java designed to be portable?? why am i getting inconsistent behaviour across platforms?? odd... do i really need to switch to windows??
anyone knows why??
platform: FC4
java 1.5