Presentation is loading. Please wait.

Presentation is loading. Please wait.

Frame Windows Application program, not applet Construct and show frame JFrame frame = new JFrame(); *** frame.show(); *** Set default close operation..

Similar presentations


Presentation on theme: "Frame Windows Application program, not applet Construct and show frame JFrame frame = new JFrame(); *** frame.show(); *** Set default close operation.."— Presentation transcript:

1 Frame Windows Application program, not applet Construct and show frame JFrame frame = new JFrame(); *** frame.show(); *** Set default close operation.. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Add components to a panel, then set the panel as content pane JPanel panel = new JPanel(); panel.add(...); panel.add(...) frame.setContentPane(panel);

2 Text Components JTextField holds a single line of text JTextArea holds multiple lines Construct with new JTextArea(rows, columns) textArea.append(aString) appends text Use textArea.setEditable(false) to use for display only To add scroll bars, use JScrollPane scrollPane = new JScrollPane(textArea); panel.add(scrollPane); or frame.setContentPanel(scrollPane);

3 for example……. public class LineData0{ public static void main (String [] args) { int width = 5; JLabel x1Label = new JLabel("x1 = "); //instantiate components JTextField x1Field = new JTextField(width); JLabel y1Label = new JLabel("y1 = "); JTextField y1Field = new JTextField(width); JLabel x2Label = new JLabel("x2 = "); JTextField x2Field = new JTextField(width); JLabel y2Label = new JLabel("y2 = "); JTextField y2Field = new JTextField(width); JTextArea out = new JTextArea(15,30); JScrollPane sPane = new JScrollPane(out); JButton anotherButton = new JButton("Go"); //button event handling code would go here, this frame will just display

4 //create listener class class BtnListener implements ActionListener{ public void actionPerformed(ActionEvent e){ double x1 = Double.parseDouble(x1Field.getText()); double y1 = Double.parseDouble(y1Field.getText()); double x2 = Double.parseDouble(x2Field.getText()); double y2 = Double.parseDouble(y2Field.getText()); double slope = (y2-y1)/(x2-x1); out.append("Slope of line is: " + slope + "\n\n"); } //register a listener object with button component ActionListener bltn = new BtnListener(); anotherButton.addActionListener(bltn); *note the the textfields and textarea objects must have been declared as final in the main

5 //add components to panel JPanel thePanel = new JPanel (); thePanel.add(x1Label); thePanel.add(x1Field); thePanel.add(y1Label); thePanel.add(y1Field); thePanel.add(x2Label); thePanel.add(x2Field); thePanel.add(y2Label); thePanel.add(y2Field); thePanel.add(anotherButton); thePanel.add(sPane); // put panel on frame and show JFrame theFrame = new JFrame(); theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); theFrame.setContentPane(thePanel); theFrame.pack(); theFrame.show(); }

6 Alternatively, 2 frames could be used……. // put panels on frames JFrame aFrame = new JFrame(); aFrame.setContentPane(sPane); //omit addition of this pane to thePanel aFrame.pack(); aFrame.show(); JFrame theFrame = new JFrame(); theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); theFrame.setContentPane(thePanel); theFrame.pack(); theFrame.show(); }


Download ppt "Frame Windows Application program, not applet Construct and show frame JFrame frame = new JFrame(); *** frame.show(); *** Set default close operation.."

Similar presentations


Ads by Google