Java 如何在一个类中使用2个propertyChange方法?
因此,我有一个程序,它有3个元素:一个可重用的StepPanel,用于增加或减少图形的大小;一个可重用的JavaBean InitialField,用于显示当前大小;一个Picture类,用于扩展画布并保存一个矩形和一个圆。下面是它的外观: 目前,两个StepPanel都会更新(增加/减少)圆和正方形的大小,因为我的propertyChange方法将两者设置在一起。我想做的是让stepPanel1增加正方形,stepPanel2增加圆形。除了生成第二个propertyChange方法之外,我似乎什么都想不出来,但是在同一个类中不能这样做,我必须只使用一个类。这是我的图片画布课程:Java 如何在一个类中使用2个propertyChange方法?,java,swing,javabeans,Java,Swing,Javabeans,因此,我有一个程序,它有3个元素:一个可重用的StepPanel,用于增加或减少图形的大小;一个可重用的JavaBean InitialField,用于显示当前大小;一个Picture类,用于扩展画布并保存一个矩形和一个圆。下面是它的外观: 目前,两个StepPanel都会更新(增加/减少)圆和正方形的大小,因为我的propertyChange方法将两者设置在一起。我想做的是让stepPanel1增加正方形,stepPanel2增加圆形。除了生成第二个propertyChange方法之外,我似
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
public class Picture extends Canvas implements VetoableChangeListener, PropertyChangeListener {
private final int SIZE = 100;
private int radius = 1;
private int side = 1;
public Picture() {
setSize(SIZE,SIZE);
}
@Override
public void vetoableChange(PropertyChangeEvent pce) throws PropertyVetoException {
if ((pce.getPropertyName()).equals("value")) {
int v = (Integer)pce.getNewValue();
if ((v <=0)||(v > SIZE/2))
throw new PropertyVetoException ("Value out of bounds!", pce);
}
}
@Override
public void propertyChange(PropertyChangeEvent pce) {
if ((pce.getPropertyName()).equals("value")) {
setRadius((Integer)pce.getNewValue());
setSide((Integer)pce.getNewValue());
repaint();
}
}
public void setRadius(int radius) {
this.radius = radius;
}
public int getRadius() {
return this.radius;
}
public void setSide(int side) {
this.side = side;
}
public int getSide() {
return this.side;
}
@Override
public void paint (Graphics g) {
Dimension d = getSize();
g.setColor(Color.GREEN);
g.fillOval(d.width/2 - radius, d.height/2 - radius, radius*2, radius*2);
g.setColor(Color.BLUE);
g.drawRect(d.width/2 - side, d.height/2 - side, side*2, side*2);
}
}
导入java.awt.Canvas;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.beans.PropertyChangeEvent;
导入java.beans.PropertyChangeListener;
导入java.beans.PropertyVetoException;
导入java.beans.VetoableChangeListener;
公共类Picture extensed Canvas实现VetoableChangeListener、PropertyChangeListener{
私有最终整数大小=100;
私有整数半径=1;
私有内部侧=1;
公共图片(){
设置大小(大小、大小);
}
@凌驾
public void vetoableChange(PropertyChangeEvent pce)引发PropertyVetoException{
如果((pce.getPropertyName()).equals(“值”)){
int v=(整数)pce.getNewValue();
如果((v尺寸/2))
抛出新的PropertyVetoException(“值超出界限!”,pce);
}
}
@凌驾
公共无效属性更改(属性更改事件pce){
如果((pce.getPropertyName()).equals(“值”)){
setRadius((整数)pce.getNewValue());
setSide((整数)pce.getNewValue());
重新油漆();
}
}
公共空间设置半径(整数半径){
这个半径=半径;
}
公共int getRadius(){
返回这个.radius;
}
公共无效设置侧(内部侧){
这个边=边;
}
公共int getSide(){
把这面还给我;
}
@凌驾
公共空间涂料(图g){
维度d=getSize();
g、 setColor(Color.GREEN);
g、 圆角(d.宽度/2-半径,d.高度/2-半径,半径*2,半径*2);
g、 setColor(Color.BLUE);
g、 drawRect(d.宽度/2-侧面,d.高度/2-侧面,侧面*2,侧面*2);
}
}
这是我的StepPanel课程:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StepPanel extends javax.swing.JPanel implements ActionListener {
private int step = 0;
public StepPanel() {
initComponents();
btnUp.addActionListener(this);
btnDown.addActionListener(this);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btnDown = new javax.swing.JButton();
btnUp = new javax.swing.JButton();
btnDown.setText("<<");
btnUp.setText(">>");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(2, 2, 2)
.addComponent(btnDown)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnUp)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnUp)
.addComponent(btnDown))
);
}// </editor-fold>
public void setStep(int step){
int oldStep = this.step;
this.step = step;
firePropertyChange("step", oldStep, this.step);
this.step = 0;
}
public int getStep() {
return this.step;
}
public void actionPerformed(ActionEvent e) {
if ((e.getSource()).equals(btnUp))
setStep(1);
if ((e.getSource()).equals(btnDown))
setStep(-1);
}
// Variables declaration - do not modify
private javax.swing.JButton btnDown;
private javax.swing.JButton btnUp;
// End of variables declaration
}
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
公共类StepPanel扩展javax.swing.JPanel实现ActionListener{
私有整数步长=0;
公共事务委员会(){
初始化组件();
btnUp.addActionListener(此);
addActionListener(这个);
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//
私有组件(){
btnDown=newjavax.swing.JButton();
btnUp=newjavax.swing.JButton();
btnDown.setText(“”);
javax.swing.GroupLayout=newjavax.swing.GroupLayout(this);
这个.setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(2,2,2)
.addComponent(btnDown)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnUp)
.addContainerGap())
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnUp)
.addComponent(btnDown))
);
}//
公共无效设置步骤(内部步骤){
int oldStep=this.step;
这个步骤=步骤;
firePropertyChange(“步骤”,oldStep,this.step);
这一步=0;
}
公共int getStep(){
返回此.step;
}
已执行的公共无效操作(操作事件e){
如果((e.getSource()).equals(btnUp))
设置步骤(1);
如果((e.getSource()).equals(btnDown))
设置步骤(-1);
}
//变量声明-不修改
私有javax.swing.JButton btnDown;
私有javax.swing.JButton btnUp;
//变量结束声明
}
这里是InitialField:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyVetoException;
import javax.swing.JTextField;
public class InitialField extends JTextField implements ActionListener,
PropertyChangeListener{
private int value;
/** Creates a new instance of InitialField */
public InitialField() {
addActionListener(this);
}
public void setValue (int value) {
try {
int oldValue = this.value;
fireVetoableChange("value", oldValue, value); // Generates PropertyeChangeEvent
this.value = value;
firePropertyChange("value", oldValue, value); // Generates PropertyChangeEvent
}
catch (PropertyVetoException pve) {
pve.printStackTrace();
}
setText(getValue() + "");
}
public int getValue () {
return this.value;
}
// <Enter>
public void actionPerformed (ActionEvent e) {
try {
setValue(Integer.parseInt(getText())); // setValue()
}
catch (NumberFormatException ex) {
ex.printStackTrace();
}
}
public void propertyChange(PropertyChangeEvent pce) {
if (pce.getPropertyName().equals("step"))
setValue(getValue() + (Integer) pce.getNewValue());
}
}
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.beans.PropertyChangeEvent;
导入java.beans.PropertyChangeListener;
导入java.beans.PropertyVetoException;
导入javax.swing.JTextField;
公共类InitialField扩展JTextField实现ActionListener,
PropertyChangeListener{
私有int值;
/**创建InitialField的新实例*/
公共初始字段(){
addActionListener(此);
}
公共无效设置值(int值){
试一试{
int oldValue=此.value;
fireVetoableChange(“value”,oldValue,value);//生成PropertyChangeEvent
这个值=值;
firePropertyChange(“value”,oldValue,value);//生成PropertyChangeEvent
}
捕获(PropertyVetoException pve){
pve.printStackTrace();
}
setText(getValue()+);
}
公共int getValue(){
返回此.value;
}
//
已执行的公共无效操作(操作事件e){
试一试{
setValue(Integer.parseInt(getText());//setValue()
}
捕获(数字格式)
public class Form extends javax.swing.JFrame {
/**
* Creates new form Form
*/
public Form() {
initComponents();
initialField1.addVetoableChangeListener(picture1);
initialField1.addPropertyChangeListener(picture1);
initialField2.addVetoableChangeListener(picture1);
initialField2.addPropertyChangeListener(picture1);
stepPanel1.addPropertyChangeListener(initialField1);
stepPanel2.addPropertyChangeListener(initialField2);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
stepPanel1 = new test.StepPanel();
initialField1 = new test.InitialField();
picture1 = new test.Picture();
stepPanel2 = new test.StepPanel();
initialField2 = new test.InitialField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
initialField1.setText("0");
initialField2.setText("0");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(stepPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(initialField1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(37, 37, 37)
.addComponent(picture1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(stepPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(initialField2, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(46, 46, 46)
.addComponent(initialField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(stepPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(picture1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addComponent(initialField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(stepPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Form().setVisible(true);
}
});
}
// Variables declaration - do not modify
private test.InitialField initialField1;
private test.InitialField initialField2;
private test.Picture picture1;
private test.StepPanel stepPanel1;
private test.StepPanel stepPanel2;
// End of variables declaration
}
revalidate();
repaint();