1

我正在尝试通过内部类事件处理方法来操作外部类成员。

一个类MyColorChooser有一个私有成员sliderColor。的默认值sliderColor在构造函数中设置为黑色。

内部类用于通过来自JSliders. sliderColor然后我通过外部类中的 setter 方法将值设置为这个新颜色。

问题是外部类的实例使用它自己的getColor方法,该方法总是返回构造函数设置的值(黑色),而不是分配给它的新值。如何使内部类方法能够直接更改外部成员变量中的值?

更新

是的,它似乎应该可以工作,但让我很困惑为什么它不......我已经发布了一些我相信的相关位 f 代码:

注意如果代码不合适,请提前道歉,因为不确定要插入多少....

调试:我已经调试并注意到颜色是它应该在内部类和 setColour 外部类方法中的颜色......所以我怀疑它已被更改回默认构造函数值......只是不确定在哪里或通过什么...我只是继续调试

MyColorChooser 类允许通过由内部类事件处理程序处理的 JSlider 的输入创建颜色对象:

* Part 4 
* JPanel subclass that alloews the user define the color attributes for shapes 
* utlising 3 JSliders 
*/ 
package Assignment2; 

import java.awt.Color; 
import java.awt.GridLayout; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JSlider; 
import javax.swing.JTextField; 
import javax.swing.SwingConstants; 
import javax.swing.event.ChangeEvent; 
import javax.swing.event.ChangeListener; 

/** 
* 
* @author Ciaran Mooney dancingbush@gmail.com 
*/ 
public class MyColorChooser4 extends JPanel { 

//declare instance varibles 
//JSlider colors 
private JSlider blueSlider; 
private JSlider greenSlider; 
private JSlider redSlider; 
//display color chossen by user varibles 
private JTextField redChoice; 
private JTextField greenChoice; 
private JTextField blueChoice; 
private DrawPanel shapeColor = new DrawPanel(); //set color of drawpanel onbject 
//prompts for user input 
private JLabel redInput; 
private JLabel blueInput; 
private JLabel greenInput; 
//color object created 
private Color colorChoice ; ; 
private JTextField theColor; 

//no arg GUI construtor gets user input and constructs color 
public MyColorChooser4() { 

//default layout of panel is set to GridLayout 4 cols 4 rows 
this.setLayout(new GridLayout(4, 4)); 


greenInput = new JLabel("Green: "); 
greenSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 1); 
greenChoice = new JTextField("0", 4);//default text 
greenChoice.setEditable(false); 

add(greenInput); 
add(greenSlider); 
add(greenChoice); 

redInput = new JLabel("Red: "); 
redSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 1); 
redChoice = new JTextField("0", 4);//default text 
redChoice.setEditable(false); 

add(redInput); 
add(redSlider); 
add(redChoice); 

blueInput = new JLabel("Blue: "); 
blueSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 1); 
blueChoice = new JTextField("0", 4);//default text 
blueChoice.setEditable(false); 

add(blueInput); 
add(blueSlider); 
add(blueChoice); 

//set default color black and declare inner class event handlers 
//sliders utlise ChengeListeners 

greenSlider.addChangeListener(new SlideHandler()); 
redSlider.addChangeListener(new SlideHandler()); 
blueSlider.addChangeListener(new SlideHandler()); 

colorChoice = Color.RED; 

//label to diplay color chosen, and blnk JLabel to move color to center 
theColor = new JTextField(4); 
JLabel preview = new JLabel(" "); 
add(preview); 
add(theColor); 
theColor.setBackground(colorChoice); 


}//end GUI constructor 

//set color 
public void setSlideColor(Color colorSlide) { 
colorChoice = colorSlide; 

}//end set colorChoice 

//get methods for fields 
public Color getColor() { 

return colorChoice; 

}//end getColor 

//return slider for red value 
public JSlider getRedSlider() { 
return redSlider; 
}//edn get red slider 

//retirn slider for green value 
public JSlider getGreenSlider() { 
return greenSlider; 
}//edn getGreenSlider 

//return slider for blue value 
public JSlider getBlueSlider() { 
return blueSlider; 
}//end getGreenSlider 

//private inner class for event handliing on sliders & implement abstract class 
private class SlideHandler implements ChangeListener { 

private Color colorSlide; 

@Override 
public void stateChanged(ChangeEvent e) { 

//return integers from sliders 
int blue = blueSlider.getValue(); 
int green = greenSlider.getValue(); 
int red = redSlider.getValue(); 

//now construct a new color based on these values 

colorSlide = new Color(red, green, blue); 
setSlideColor(colorSlide); 


//display integer value of color chosen in relative textfield 
//use String method valueOf to return string rep of integer 
//display chossen color in text area 
redChoice.setText(String.valueOf(red)); 
redChoice.setBackground(new Color(red, 0, 0)); 
greenChoice.setText(String.valueOf(green)); 
greenChoice.setBackground(new Color(0, green, 0)); 
blueChoice.setText(String.valueOf(blue)); 
blueChoice.setBackground(new Color(0, 0, blue)); 

//set chosen color preview 
theColor.setBackground(colorChoice); 
theColor.setText("Preview"); 


}//end stateChanged method 
}//end inner class SlideHandler 
}//end class MyColorChooser4

然后是类 ShapePanel,它是一个菜单,用户可以从中选择要绘制的形状的各种属性,包括颜色。通过颜色对象数组的 JComobobox 选择来确定颜色。MyColorChooser4 的颜色是通过调用其 getColor 方法的 teh 类的实例获取的:

上述声明和intilisation

public DrawPanel draw = new DrawPanel(); 
private MyColorChooser4 sliderColor = new MyColorChooser4(); 

//array holding color objects 
private Color colors[] = {Color.BLACK, Color.BLUE, Color.CYAN, 

Color.DARK_GRAY, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY, 
Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, 
Color.YELLOW, sliderColor.getColor()}; 

//array carrying color strings 
private String colorNames[] = {"Black", "Blue", "Cyan", 
"Dark Gray", "Gray", "Green", "Light Gray", "Magenta", 
"Orange", "Pink", "Red", "White", "Yellow", "Slider Color"};

允许选择颜色的类 ShapePanel 的事件处理程序方法....

color = new JComboBox(colorNames); 
color.setMaximumRowCount(6); 
color.addItemListener(new ItemListener() { 

@Override 
public void itemStateChanged(ItemEvent e) { 

if (e.getStateChange() == ItemEvent.SELECTED) { 
draw.setCurrentColor((colors[color.getSelectedIndex()]));; 

} //end if 
}//end itemStateChnaged 
});//end annmonouys class and action event method 

componments.add(color); 

DrawPanel 类的实例 draw 设置通过以下 set 方法绘制形状的颜色.....

//set current color 
public void setCurrentColor(Color color) { 


//set currentColor from JCombox input handler 
currentColor = color; 


//generate a random startColor for Gradient color 
int red = (int) (Math.random() * 256); 
int green = (int) (Math.random() * 256); 
int blue = (int) (Math.random() * 256); 

//cyclic vs acylic fill 
colorFillStyle = new Random(); 
boolean gradientShift = colorFillStyle.nextBoolean(); 



 //create gradient object 
 Color startColor = new Color(red, green, blue); 
 gradientColor = new GradientPaint(10f, 10f, startColor, 350f, 350f, currentColor,         gradientShift); 


}//end set color method

但是,当绘制形状时,颜色始终为红色,反映了 MyColorChooser4 构造函数中的颜色初始化值....

希望这有点用......

4

2 回答 2

1

如果没有看到您的代码,就不可能确定问题是什么,但有两种可能性:

1:你的外部类中的set方法不正确。似乎不太可能,但总是值得检查,因为它可能写成:

public void setColor(Color color)
{
    // Should be this.color =
    color = color;
}

2:您的内部类没有调用外部类的 set 方法,也许内部类已经声明了自己的 set 方法,该方法在您的调用中具有优先权。

3:内部类实际上从未被调用,可能是因为您的事件没有被触发。

最终,您的 setMethod 没有被调用,它没有做您认为它应该做的事情,或者在您调用 set 方法后其他事情正在重置值。据我所知,这些是您的值无法按预期设置的唯一可能性。

如果您发布代码,那么我们可以为您的问题提供更具体的答案。

于 2013-07-09T23:00:00.487 回答
0

@increment1 我避免了单例策略,并通过在一个类中创建所有实例 req 并通过将侦听器附加到事件处理程序来实现所需的更改。.

我的问题是我期望我的 drawPanel 类的两个单独实例具有相同的行为,但没有经过深思熟虑和缺乏经验。

再次感谢 C

于 2013-07-12T15:02:09.703 回答