Вы находитесь на странице: 1из 3

CSD J0201 JAVA [LAB-11] Objectives: GUI Programming + Event Handling Programming in Java

Activity I Q1. Write the following code in the file GUITest.java. Compile it and Observe the o/p? import java.awt.*; import javax.swing.*; public class GUITest { public static void main(String[] args) { JFrame frame=new JFrame(); //create a new frame JButton button1 = new JButton("Click me"); JButton button2 = new JButton("Click me too"); JPanel panel= new Jpanel(); //add a panel to the frame

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(BorderLayout.WEST, panel); panel.add(BorderLayout.SOUTH, button1); //add buttons to the panel panel.add(BorderLayout.NORTH, button2); frame.setSize(400,600); //set the default size of the frame

frame.setVisible(true); } // End of main() Method }// End of class GUITest

//Display the frame

Activity II Q2. Write the following code in the file GUITest.java. Compile it and Observe the o/p? import java.awt.BorderLayout; import java.awt.Color; import javax.swing.*; public class Panels { public static void main(String[] args) { JFrame frame= new JFrame("Panel Test"); JPanel colorPanel,textButtonPanel; colorPanel = new JPanel(); textButtonPanel = new JPanel(); JButton red= new JButton("Red"); JButton blue= new JButton("blue"); JButton hello = new JButton("Hello");

1|Page

CSD J0201 JAVA [LAB-11]


JButton hi = new JButton("Hi"); colorPanel.add(red); colorPanel.add(blue); textButtonPanel.add(hello); textButtonPanel.add(hi); colorPanel.setBackground(Color.darkGray); textButtonPanel.setBackground(Color.blue); frame.getContentPane().add(BorderLayout.EAST,colorPanel); frame..add(BorderLayout.SOUTH,textButtonPanel); frame.setSize(400,600); //set the default size of the frame frame.setVisible(true); } } Activity III Q3. Design and implement a frame with four push buttons named as Hello, Hi, Red and Green, and a string label in the centre. Clicking any of the buttons Hello or Hi causes the string to be displayed accordingly and clicking on any of the buttons Red or Green changes the foreground color of the text accordingly. The layout is shown below:

import java.awt.*; import javax.swing.*; import java.awt.event.*; class Buttonlistener implements ActionListener { private JLabel l1 ; Buttonlistener(JLabel l1) { this.l1 = l1; } public void actionPerformed(ActionEvent ae) { String str = ae.getActionCommand(); // Read the Command who has generated ActionEvent if(str.equals("Hello")) l1.setText("Hello How are you"); if(str.equals("Hi")) l1.setText("Hi How are you"); if(str.equals("RED")) l1.setForeground(Color.red); if(str.equals("GREEN")) l1.setForeground(Color.green);

2|Page

CSD J0201 JAVA [LAB-11]


}// End of actionPerformed() } // End of class class Mainframetest { public static void main(String args[]) { JFrame f1 = new JFrame("Main Frame"); JPanel p1 = new JPanel(); p1.setLayout(new BoxLayout(p1,BoxLayout.Y_AXIS)); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); p3.setLayout(new BorderLayout()); JButton b1 = new JButton("RED"); JButton b2 = new JButton("GREEN"); JButton b3 = new JButton("Hello"); JButton b4 = new JButton("Hi"); p1.add(b1); p1.add(b2); p2.add(b3); p2.add(b4); JLabel l1 =new JLabel("Hello How are you"); p3.add(l1,BorderLayout.CENTER); Buttonlistener btnL = new Buttonlistener(l1); b1.addActionListener(btnL); b2.addActionListener(btnL); b3.addActionListener(btnL); b4.addActionListener(btnL); f1.add(p1,BorderLayout.EAST); f1.add(p3,BorderLayout.CENTER); f1.add(p2,BorderLayout.SOUTH); f1.setSize(400,600); f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f1.setVisible(true); }// End of main() }// End of MainFrameTest

3|Page

Вам также может понравиться