JavaBeans

Arquivo: JavaH.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JavaH extends JFrame {

public JavaH() {

super("Formulario");
this.setSize(250,100);
this.setLocation(50, 100);

Container ct = this.getContentPane();
ct.setLayout(null);

MyBean p = new MyBean();
p.setProperty1(111);
p.setProperty2(true);

JLabel label_ = new JLabel();

label_.setText("<html>p.getProperty1() = "+p.getProperty1()+"<br>p.isProperty2() = "+p.isProperty2()+"</html>");

label_.setVerticalAlignment(JLabel.TOP);
label_.setBounds(10,10,150,100);
// new Color(vermelho, verde, azul);
label_.setForeground(new Color(125, 32, 155));
ct.add(label_);

Image Icone = Toolkit.getDefaultToolkit().getImage("icon.gif");
setIconImage(Icone);

this.setVisible(true);

this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}});

}
public static void main(String[] args) {
new JavaH();
}
}

Arquivo: MyBean.java

import java.io.Serializable;
public class MyBean implements Serializable {

private int property1;
private boolean property2;

public MyBean() {

}

public void setProperty1(int property1) {
this.property1 = property1;
}

public void setProperty2(boolean property2) {
this.property2 = property2;
}

public int getProperty1() {
return property1;
}

public boolean isProperty2() {
return property2;
}
}

Arquivo: JavaH.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JavaH extends JFrame {

private objeto p1;
private objeto p2;

public JavaH() {

super("Formulario");
this.setSize(370,120);
this.setLocation(50, 100);

Container ct = this.getContentPane();
ct.setLayout(null);

p1 = new objeto();
ct.add(p1.setTeste(0,0));
p2 = new objeto();
ct.add(p2.setTeste(172,0));

Image Icone = Toolkit.getDefaultToolkit().getImage("icon.gif");
setIconImage(Icone);

this.setVisible(true);

p1.botao1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
p1.getTeste();
}});

p2.botao1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
p2.getTeste();
}});

this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}});

}
public static void main(String[] args) {
new JavaH();
}
}

Arquivo: objeto.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.io.Serializable;
public class objeto implements Serializable {

public JLabel label1;
public JButton botao1;
public JPanel painel1;

public objeto() {
label1 = new JLabel();
botao1 = new JButton();
painel1 = new JPanel();
}

public JPanel setTeste(int esquerda,int topo) {
painel1.setBorder(BorderFactory.createTitledBorder("Teste"));
painel1.setBounds(esquerda, topo, 170, 72);
painel1.setLayout(null);
label1.setText("Oi");
label1.setBounds(10,10,150,20);
painel1.add(label1);
botao1.setText("Clique aqui");
botao1.setBounds(10,36,150,25);
painel1.add(botao1);
return painel1;
}

public void getTeste(){
if(label1.getText().equals("Oi")){
label1.setText("Hello");
} else {
label1.setText("Oi");
}
}
}