BinCalc.java
Select all
/* BinCalc.java CIS 260 2006-03-01 David Klick Demonstration of creating a GUI component using other Swing components. */ import javax.swing.*; import java.awt.*; import java.awt.event.*; class BinCalc extends JPanel { private JTextField txt; private JButton[] btn = new JButton[4]; public BinCalc() { // set up panels and layouts setLayout(new GridLayout(2, 1, 10, 10)); JPanel pnlTop = new JPanel(); JPanel pnlBottom = new JPanel(); pnlTop.setLayout(new BorderLayout()); pnlBottom.setLayout(new GridLayout(1, 4, 10, 5)); // create buttons btn[0] = new JButton("0"); btn[1] = new JButton("1"); btn[2] = new JButton("Clear"); btn[3] = new JButton("Calc"); // change button 2's font Font f = new Font("Comic Sans MS", Font.ITALIC, 12); // add buttons for (int i=0; i<4; i++) pnlBottom.add(btn[i]); // create and add text display txt = new JTextField(20); txt.setText(""); txt.setEditable(false); pnlTop.add(txt); add(pnlTop); add(pnlBottom); } public void clear() { txt.setText(""); } public String getText() { return txt.getText(); } public void setText(String s) { clear(); append(s); } public void append(String s) { for (int i=0; i