Language: Java
FrameGUI class
1: 2: 3: //imports 4: import java.awt.BorderLayout; 5: import java.awt.event.ActionEvent; 6: import java.awt.event.ActionListener; 7: import javax.swing.JPanel; 8: import javax.swing.JButton; 9: import java.awt.Color; 10: 11: 12: 13: // FrameGUI inherits from JPanel and implements ActionListener 14: // this allows the program to listen for button clicks 15: public class FrameGUI extends JPanel implements ActionListener 16: { 17: private JButton btnSetToBlack,btnSetToBlue; 18: private BorderLayout layout; 19: 20: //constructor 21: public FrameGUI() 22: { 23: //instantiate a new BorderLayout with 50 pixel gaps between 24: // components of the layoutManager 25: layout = new BorderLayout(50,50); 26: //set the FrameGUI's layout to the layout defined above 27: this.setLayout(layout); 28: 29: //instantiate the 2 buttons 30: btnSetToBlack = new JButton("BLACK"); 31: btnSetToBlue = new JButton("BLUE"); 32: 33: //add the 2 buttons to FrameGUI (specifying position) 34: this.add(btnSetToBlack, BorderLayout.WEST); 35: this.add(btnSetToBlue, BorderLayout.EAST); 36: //here, the component you are required to make will be added to the gui 37: // adding your component --> 38: this.add(new DrawComponent(), BorderLayout.CENTER); 39: 40: //action listeners assigned to buttons (to listen for user actions) 41: btnSetToBlack.addActionListener(this); 42: btnSetToBlue.addActionListener(this); 43: 44: //default colours, visiblity and size of frameGUI defined here 45: this.setBackground(Color.GRAY); 46: this.setVisible(true); 47: this.setSize(800,600); 48: } 49: 50: //actionPerformed(ActionEvent event) allows event information to be processed 51: //below the event source is checked (i.e. what button was pressed) and the 52: //background colour is changed accordingly 53: public void actionPerformed(ActionEvent event) 54: { 55: if (event.getSource() == btnSetToBlack) this.setBackground(Color.BLACK); 56: if (event.getSource() == btnSetToBlue) this.setBackground(Color.BLUE); 57: } 58: }
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

