Help with referencing the top-pane from within an action event
Brand new to this, there is obviously an easy solution, I just can't find it.
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.*;
import java.util.Vector;
import java.util.Random;
import javax.swing.*;
publicclass BasicDrawextends JPanel
implements ActionListener{
protected JButton b1;
public BasicDraw()
{
b1 =new JButton("Disable middle button");
b1.addActionListener(this);
add(b1);
}
publicvoid actionPerformed(ActionEvent e)
{
JLabel x =new JLabel("sadf");
main().frame.getContentPane().add(x);
}
publicstaticvoid main(String[] args){
JFrame frame =new JFrame("BasicDraw");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
BasicDraw newContentPane =new BasicDraw();
newContentPane.setOpaque(true);//content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.setSize(300,300);
frame.setVisible(true);
}
}
I know this is probably a really odd bit of code, basically I am just trying to work out how to reference the pane so I can add things to it, or draw an object or something, when the button is pressed (or mouse click would be even better).
Thanks a lot for any help, I'm finding java thouroughly confusing :)
Alex

