So this was the example given in the notes for my current project. Usually I run the the example code and play around with it to see how everything works. However i'm not sure how to properly call the functions in the example code.
This what was given to me:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Draw extends JPanel implements ActionListener
{
JTextField tfInfo;
JLabel lblColor, lblShapes;
JCheckBox cbRed, cbBlue;
ButtonGroup shapes;
JRadioButton rbCircle, rbSquare;
JButton btnSubmit;
public Draw()
{
setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
tfInfo = new JTextField("Color & Shapes", 15);
lblColor = new JLabel("Colors:");
cbRed = new JCheckBox("Red");
cbBlue = new JCheckBox("Blue");
lblShapes = new JLabel("Shapes:");
shapes = new ButtonGroup();
rbCircle = new JRadioButton("Circle");
rbSquare = new JRadioButton("Square");
btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(this);
add(tfInfo);
add(lblColor);
add(cbRed);
add(cbBlue);
add(lblShapes);
add(rbCircle);
add(rbSquare);
add(btnSubmit);
shapes.add(rbCircle);
shapes.add(rbSquare);
}
public static void main(String [] args)
{
Draw n = new Draw();
n.setVisible(true);
}
public void actionPerformed(ActionEvent a)
{
if(a.getSource() == btnSubmit)
{
if(cbRed.isSelected()&&cbBlue.isSelected())
{
if(rbCircle.isSelected())
{
tfInfo.setText("urple Circle");
}
else if(rbSquare.isSelected())
{
tfInfo.setText("Purple Square");
}
}
else if(cbRed.isSelected())
{
if(rbCircle.isSelected())
{
tfInfo.setText("Red Circle");
}
else if(rbSquare.isSelected())
{
tfInfo.setText("Red Square");
}
}
else if(cbBlue.isSelected())
{
if(rbCircle.isSelected())
{
tfInfo.setText("Blue Circle");
}
}
else if(rbSquare.isSelected())
{
tfInfo.setText("Blue Square");
}
}
}
public class MApp extends JPanel implements MouseListener
{
private boolean clicked;
private Rectangle r;
public MApp()
{
clicked = false;
r = new Rectangle(10, 10, 50, 50);
addMouseListener(this);
}
public void paintComponent(Graphics g)
{
if(clicked)
{
g.setColor(Color.BLUE);
}
else
{
g.setColor(Color.RED);
}
g.fillRect((int)r.getX(), (int)r.getY(),
(int)r.getWidth(), (int)r.getHeight());
}
public void mouseClicked (MouseEvent e)
{
Point p = new Point(e.getX(),e.getY());
if(r.contains(p))
{
clicked = !clicked;
}
repaint();
}
public void mousePressed (MouseEvent evnt) {}
public void mouseReleased (MouseEvent evnt) {}
public void mouseEntered (MouseEvent evnt) {}
public void mouseExited (MouseEvent evnt) {}
}
}
actionPerformedmethod actually get called). The fact is, for the most part, you don't care. You know you can add anActionListenerto a control, like aJButtonand when prescribed event occurs (like the user clicking the button) it will call youractionPerformedmethod