我正在尝试通过内部类事件处理方法来操作外部类成员。
一个类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 构造函数中的颜色初始化值....
希望这有点用......