importjava.awt.*; import java.awt.event.*; import javax.swing.*; public class La
ID: 3615954 • Letter: I
Question
importjava.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Lab07q2extends JFrame{
JPanel centerPanel = new JPanel();
JPanel northPanel = new JPanel();
JPanel southPanel = new JPanel();
JLabel center = new JLabel("CENTER");
JLabel west = new JLabel ("WEST");
JLabel south = new JLabel ("SOUTH");
JLabel north = new JLabel ("NORTH");
JLabel east = new JLabel ("EAST");
public Lab07q2(){
super();
setSize(200,110);
setLocationRelativeTo(null);
setTitle("Ex2");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
centerPanel.setLayout(new FlowLayout());
northPanel.setLayout(new FlowLayout());
southPanel.setLayout(new FlowLayout());
centerPanel.add(center,BorderLayout.CENTER);
northPanel.add(north,BorderLayout.NORTH);
southPanel.add(south,BorderLayout.SOUTH );
add(centerPanel,BorderLayout.CENTER);
add(west,BorderLayout.WEST);
add(southPanel,BorderLayout.SOUTH);
add(northPanel,BorderLayout.NORTH);
add(east,BorderLayout.EAST);
listener test = new listener ();
east.addMouseListener(test);
west.addMouseListener(test);
south.addMouseListener(test);
north.addMouseListener(test);
center.addMouseListener(test);
setVisible(true);
}
class listener extends MouseAdapter{
public void mouseExited(MouseEvent a){}
public void mouseClicked(MouseEvent a){
if(a.getSource()== east)
JOptionPane.showMessageDialog(null,"Welcome to east");
if(a.getSource()== center)
JOptionPane.showMessageDialog(null,"Welcome to center ");
if(a.getSource()== west)
JOptionPane.showMessageDialog(null,"Welcome to west ");
if(a.getSource()== south)
JOptionPane.showMessageDialog(null,"Welcome to south ");
if(a.getSource()== north)
JOptionPane.showMessageDialog(null,"Welcome to north");
}
public void mouseEntered(MouseEvent a){}
public void mouseReleased(MouseEvent a){}
public void mousePressed(MouseEvent a){}
}
public static void main(String[] args){
Lab07q2 test = new Lab07q2();
}
}
Explanation / Answer
please rate - thanks import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Lab07q2 extends JFrame{ JLabel center = new JLabel("CENTER",JLabel.CENTER); JLabel west = new JLabel ("WEST",JLabel.CENTER); JLabel south = new JLabel ("SOUTH",JLabel.CENTER); JLabel north = new JLabel ("NORTH",JLabel.CENTER); JLabel east = new JLabel ("EAST",JLabel.CENTER); public Lab07q2(){ super(); setSize(200,110); setLocationRelativeTo(null); setTitle("Ex2"); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new BorderLayout()); add(north, BorderLayout.NORTH); add(south, BorderLayout.SOUTH); add(east, BorderLayout.EAST); add(west, BorderLayout.WEST); add(center, BorderLayout.CENTER); listener test = new listener (); east.addMouseListener(test); west.addMouseListener(test); south.addMouseListener(test); north.addMouseListener(test); center.addMouseListener(test); setVisible(true); } class listener extends MouseAdapter{ public void mouseExited(MouseEvent a){} public void mouseClicked(MouseEvent a){ if(a.getSource()== east) JOptionPane.showMessageDialog(null,"Welcome to east"); if(a.getSource()== center) JOptionPane.showMessageDialog(null,"Welcome to center "); if(a.getSource()== west) JOptionPane.showMessageDialog(null,"Welcome to west "); if(a.getSource()== south) JOptionPane.showMessageDialog(null,"Welcome to south "); if(a.getSource()== north) JOptionPane.showMessageDialog(null,"Welcome to north"); } public void mouseEntered(MouseEvent a){} public void mouseReleased(MouseEvent a){} public void mousePressed(MouseEvent a){} } public static void main(String[] args){ Lab07q2 test = new Lab07q2(); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.