Fill This Form To Receive Instant Help
Homework answers / question archive / 9 button frame based application Create a Frame-based application that displays an array of nine buttons on a Panel
9 button frame based application
Create a Frame-based application that displays an array of nine buttons on a Panel. Assign the Panel to the Center of your frame. Each button should display the name of a course in your department. Include a TextField in the South. When the user clicks a button, the course name should display in the TextField. There should be a label in the North telling how to use the program.
Also please provide a screenshot of what the program looks like too please.
import java.awt.*; import java.awt.event.*; public class MF extends Frame { String course[]={"Course1","Course2","Course3","Course4","Course5","Course6","Course7","Course8","Course9"}; TextField txtField = new TextField(); Button[] but=new Button[9]; Label label1 = new Label("Press the button, the corresponding course name will be shown in the text field!"); MF() { super("9 button frame based application"); addWindowListener(closer); Panel pan = new Panel(); pan.setLayout(new GridLayout(3,3)); actionListener l1 = new actionListener(); for (int i=0;i<9;i++){ but[i]=new Button(course[i]); pan.add(but[i],i); but[i].addActionListener(l1); } this.add(label1,BorderLayout.NORTH); this.add(pan, BorderLayout.CENTER); this.add(txtField,BorderLayout.SOUTH); this.setSize(600,300); setVisible(true); } private class actionListener implements ActionListener{ public void actionPerformed(ActionEvent e){ for( int i=0;i<9; i++) if (e.getSource().equals(but[i])) txtField.setText(but[i].getLabel()) ; } } private WindowListener closer = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; public static void main(String[] args) { new MF(); } }