gpt4 book ai didi

java - 通过内部类更改外部类成员的值

转载 作者:行者123 更新时间:2023-12-01 14:21:06 25 4
gpt4 key购买 nike

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

MyColorChooser有一个私有(private)成员sliderColorsliderColor 的默认值在构造函数中设置为黑色。

内部类用于通过来自 JSliders 的用户输入来处理新颜色的构造。然后,我通过外部类中的 setter 方法将 sliderColor 的值设置为这个新颜色。

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

已更新

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

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

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

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

* 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 方法的类实例获取的:

上述声明和应用

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 类的实例绘制通过以下 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

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

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

最佳答案

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

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

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

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

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

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

如果您发布代码,我们就可以针对您的问题得出更具体的答案。

关于java - 通过内部类更改外部类成员的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17559147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com