所以我有一个包含3个元素的程序:一个可重用的StepPanel,用于增加或减少图形的大小,一个可重用的JavaBean InitialField,用于显示当前大小;一个Picture类,用于扩展Canvas并保存一个Rectangle和一个Circle 。这是它的外观:
目前两个stepPanel都更新(增加/减少)Circle和Square的大小,因为我的propertyChange方法将它们设置在一起。我想要做的是使stepPanel1增加Square并使stepPanel2增加Circle。除了制作第二个propertyChange方法之外我似乎无法想到任何东西,但是你不能在同一个类中做到这一点,而且我必须只用1个类来做。这是我的Picture Canvas类:
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);
}
}
这是我的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
}
这是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());
}
}
最后,这是表格:
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
}
你能帮帮我,并指导我如何让stepPanel1增加Rectangle和stepPanel2增加Circle?我还必须做到这一点,以便Circle之后不会变得比Rectangle更大。感谢您的帮助!
答案 0 :(得分:0)
我想做的是让stepPanel1增加Square并使stepPanel2增加Circle。
然后您可以拥有不同的属性更改事件。所以你可能有一个“侧面”和一个“半径”事件。
这是正常的Swing组件的工作方式。它们为每个被更改的不同属性生成不同的事件。
此外,setSide(...)
和setRadius()
方法中的代码应为:
revalidate();
repaint();
set ???(...)方法负责知道属性已更改并导致重新绘制组件。 repaint()不应该是侦听器代码的一部分。
答案 1 :(得分:0)
感谢您的帮助和提示!我一定要解决这些问题,看看Swing中的事件是如何工作的。与此同时,我的做法是绕过这个: 我创建了第二个InitialField bean并将其用于Circle,更改了&#34;值&#34;到&#34; value2&#34;。之后,它在propertyChange方法中添加了第二个if语句。我知道这样做是一种俗气而又糟糕的方式,但现在它起作用了。现在将研究如何以正确的方式做到这一点。