Here is a basic example of a calculation using Java swing. Interesting to note are the calculation event, the horizontal alignment and the font for the result. I struggle for some time to find the equivalent in Python for Java "null" value; well, it is "None". I love java.swing, looks so neat in Python. # File: ParallelResistors.py # Date: Aug 26, 2007 import java.awt #import java.awt as awt from javax.swing import JFrame, JPanel, JLabel, JTextField, JButton from javax.swing import SwingConstants class ParallelResistors(JFrame): def __init__(self): frame = JFrame("Parallel Resistors") frame.defaultCloseOperation=JFrame.EXIT_ON_CLOSE; pane = frame.getContentPane() pane.setLayout(None) self.createUserInterface(pane) frame.setSize(400, 350) frame.setVisible(True) def createUserInterface(self, contentPane): r1JLabel = JLabel() r1JLabel.setText("Resistor #1:") r1JLabel.setBounds(65, 55, 75, 15) contentPane.add(r1JLabel) self.r1JTextField = JTextField() self.r1JTextField.setText("0") self.r1JTextField.setBounds(145, 50, 100, 21) self.r1JTextField.horizontalAlignment = SwingConstants.RIGHT self.r1JTextField.actionPerformed = self.rEnter contentPane.add(self.r1JTextField) r2JLabel = JLabel() r2JLabel.setText("Resistor #2:") r2JLabel.setBounds(65, 85, 75, 15) contentPane.add(r2JLabel) self.r2JTextField = JTextField() self.r2JTextField.setText("0") self.r2JTextField.setBounds(145, 80, 100, 21) self.r2JTextField.horizontalAlignment = SwingConstants.RIGHT self.r2JTextField.actionPerformed = self.rEnter contentPane.add(self.r2JTextField) self.calcJTextField = JTextField() self.calcJTextField.setText("0") self.calcJTextField.setBounds(88, 200, 200, 32) self.calcJTextField.editable = 0 self.calcJTextField.setFont(java.awt.Font("Tahoma", 1, 14)); self.calcJTextField.horizontalAlignment = SwingConstants.CENTER contentPane.add(self.calcJTextField) self.msgJLabel = JLabel() self.msgJLabel.setText("Enter values for Resistors") self.msgJLabel.setBounds(128, 256, 250, 13) contentPane.add(self.msgJLabel) calJButton = JButton() calJButton.setText("Calculate") calJButton.setBounds(144, 136, 125, 23) calJButton.actionPerformed = self.calcClick contentPane.add(calJButton) def rEnter(self, event): self.msgJLabel.setText("Enter values for Resistors...!") print "hello" def calcClick(self, event): self.calculate() def calculate(self): # calculate the resistance of two resistors in parallel try: r1 = float(self.r1JTextField.text) r2 = float(self.r2JTextField.text) if r1 + r2 > 0 and r1>0 and r2>0 : rp = (r1 * r2) / (r1 + r2) self.calcJTextField.setText(str(rp)) self.msgJLabel.setText("Calculation Successful ...!") elif r1 == 0 or r2==0: self.msgJLabel.setText("Enter non-zero values for Resistors...!") else: self.msgJLabel.setText("Division by Zero ...!") except ValueError: self.msgJLabel.setText("Enter values for R1 and R2 ...!") if __name__ == '__main__': application = ParallelResistors()

View this message in context: Parallel Resistor Calculation. GUI example
Sent from the jython-users mailing list archive at Nabble.com.