import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JScrollPane; import javax.swing.JButton; import java.awt.Font; import java.awt.GridBagLayout; import java.awt.GridBagConstraints; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.Component; /* Author: James Tam Version: Novemember 4, 2015 Learning concepts: * JTextArea * JScrollPane * GridBagLayout & GridBagConstraints * Adding components to GridBag with a generic addWidget method */ public class MyFrame extends JFrame implements ActionListener { private JTextArea text; private JButton button; private JScrollPane scrollPane; private GridBagLayout aLayout; private GridBagConstraints aConstraint; public MyFrame() { aLayout = new GridBagLayout(); aConstraint = new GridBagConstraints(); createControls(); addWidget(button,0,0); addWidget(scrollPane,1,1); button.addActionListener(this); setSize(400,250); setVisible(true); setLayout(aLayout); } public void addWidget(Component c, int x, int y) { aConstraint.gridx = x; aConstraint.gridy = y; aLayout.setConstraints(c,aConstraint); add(c); } public void createControls() { text = new JTextArea(); scrollPane = new JScrollPane(text); text.setFont(new Font("Times",Font.BOLD, 16)); text.append("Default text"); button = new JButton("Press me"); } // Gets text from ScrollPane and sets title bar to this text // Also echos text to standard output (text screen). public void actionPerformed(ActionEvent e) { String s = text.getText(); System.out.println(s); } }