class MyPanel extends JPanel {

northcarolinawrySoftware and s/w Development

Nov 3, 2013 (3 years and 7 months ago)

79 views

import java.awt.BorderLayout;

import java.awt.Dimension;


import javax.swing.JFrame;

import javax.swing.JPanel;

import java.awt.*;

import javax.swing.JButton;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;


class MyPanel extends J
Panel {


int x,y,w,h;


Color c;



MyPanel(){


x=y=w=h=50;


c= Color.red;


}


public void setVariables(int xx, int yy, int ww, int hh, Color cc){


x=xx;


y=yy;


w=ww;


h=hh;


c=cc;


}


pu
blic void paintComponent(Graphics g){


super.paintComponent(g);


g.setColor(c);


g.fillRect(x,y,w,h);


}

}


public class Frame3 extends JFrame {


JPanel contentPane;


MyPanel jPanel1 = new MyPanel();


JButton jButton1 = new

JButton();


JButton jButton2 = new JButton();


public Frame3() {


try {


setDefaultCloseOperation(EXIT_ON_CLOSE);


jbInit();


} catch (Exception exception) {


exception.printStackTrace();


}


}



private void jbInit() throws Exception {


contentPane = (JPanel) getContentPane();


contentPane.setLayout(null);


setSize(new Dimension(400, 300));


setTitle("Frame Title");


jPanel1.setBackground(Color.red);



jPanel1.setBounds(new Rectangle(53, 41, 307, 207));


jPanel1.setLayout(null);jButton1.setBounds(new Rectangle(66, 10, 80, 30));


jButton1.setText("change 1");


jButton1.addActionListener(new Frame3_jButton1_actionAdapter(this));



jButton2.setBounds(new Rectangle(161, 10, 77, 30));


jButton2.setText("change 2");


jButton2.addActionListener(new Frame3_jButton2_actionAdapter(this));


contentPane.add(jPanel1);


jPanel1.add(jButton1);


jPanel1.ad
d(jButton2);


}



public void jButton1_actionPerformed(ActionEvent e) {


jPanel1.setVariables(100,50,200,50, Color.green);


jPanel1.repaint();


}



public void jButton2_actionPerformed(ActionEvent e) {


jPanel1.setVariables
(50,50,50,200, Color.blue);


jPanel1.repaint();


}

}


class Frame3_jButton2_actionAdapter implements ActionListener {


private Frame3 adaptee;


Frame3_jButton2_actionAdapter(Frame3 adaptee) {


this.adaptee = adaptee;


}



publi
c void actionPerformed(ActionEvent e) {


adaptee.jButton2_actionPerformed(e);


}

}


class Frame3_jButton1_actionAdapter implements ActionListener {


private Frame3 adaptee;


Frame3_jButton1_actionAdapter(Frame3 adaptee) {


this.adapt
ee = adaptee;


}


public void actionPerformed(ActionEvent e) {


adaptee.jButton1_actionPerformed(e);


}

}