package swingdemo; import javax.swing.*; import java.awt.BorderLayout; import java.awt.event.*; public class MyFrame extends JFrame implements ActionListener { private JTextArea textArea = new JTextArea(); public MyFrame() { this.setTitle("SwingDemo Frame"); //-------------------------------------- //set the layout to BorderLayout //-------------------------------------- this.getContentPane().setLayout(new BorderLayout()); //----------------------------------- //creating graphical components //----------------------------------- textArea.setText(""); JScrollPane scrollPane = new JScrollPane(textArea); JButton clearTextButton = new JButton("Clear TextArea"); JButton appendTextButton = new JButton("Append Text"); JButton setTextButton = new JButton("Set Text"); JPanel panel = new JPanel(); panel.add(setTextButton); panel.add(appendTextButton); panel.add(clearTextButton); this.getContentPane().add(scrollPane, BorderLayout.CENTER); this.getContentPane().add(panel, BorderLayout.SOUTH); //--------------------------------- //set the event handling //--------------------------------- setTextButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ textArea.setText("Ein wenig Text"); } }); appendTextButton.addActionListener(new AppendTextActionListener()); clearTextButton.addActionListener(this); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /*this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } });*/ } /** * Example for an ActionListener as inner class */ private class AppendTextActionListener implements ActionListener{ public void actionPerformed(ActionEvent e){ textArea.append("Ein wenig mehr Text"); } } public void actionPerformed(ActionEvent e){ textArea.setText(""); } public static void main(String[] args){ MyFrame frame = new MyFrame(); frame.setSize(400,500); frame.setVisible(true); //frame.pack();frame.show(); } }