Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class Window extends JFrame {
- /**
- *
- */
- private static final long serialVersionUID = -8255319694373975038L;
- public static void main(String[] args){
- new Window();
- }
- public Window(){
- // Adds the custom panel
- Panel Panel = new Panel();
- this.add(Panel);
- // Basic Window Features
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setSize(800,800);
- this.setLocationRelativeTo(null);
- this.setVisible(true);
- }
- }
- public class Panel extends JPanel{
- public Panel(){
- Button testButton = new Button("Test");
- DrawBoard drawBoard = new DrawBoard();
- Listener listener = new Listener();
- this.add(testButton);
- this.add(drawBoard);
- }
- }
- import java.awt.Dimension;
- import javax.swing.JButton;
- public class Button extends JButton{
- public Button(String name) {
- this.setText(name);
- this.setName((String) name);
- buttonSettings();
- }
- private void buttonSettings(){
- Listener listener = new Listener();
- this.addActionListener(listener);
- int width = 200;
- int height = 50;
- Dimension dim = new Dimension(width,height);
- this.setPreferredSize(dim);
- }
- }
- import java.awt.Color;
- import java.awt.Dimension;
- import javax.swing.JPanel;
- public class DrawBoard extends JPanel{
- public DrawBoard(){
- DrawBoardSettings();
- }
- private void DrawBoardSettings(){
- int width = 600;
- int height = 600;
- Dimension dim = new Dimension(width,height);
- this.setPreferredSize(dim);
- this.setBackground(Color.WHITE);
- }
- }
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import javax.swing.JComponent;
- public class Listener implements ActionListener, MouseListener {
- @Override
- public void mouseClicked(MouseEvent e) {
- **// Draw a dot at the mouse location of the DrawBoard JPanel that was added by the Panel JPanel that was added by the Window JFrame**
- }
- @Override
- public void mousePressed(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseExited(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- String name = ((JComponent) e.getSource()).getName();
- **// Draw a rectangle on the DrawBoard JPanel that was added by the Panel JPanel that was added by the Window JFrame**
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement