如何将值从公共类传递到另一个公共类?

时间:2013-04-08 07:12:34

标签: java

我试图将值从一个类传递到另一个类。当我通过AdjustmentListener更新这些变量时,类subPanel1读取全局变量bu它不会更改值。我试图将rc,gc和bc变量从subPanel2类传递给subPanel1类,它只接受全局变量的初始值,但是当我移动jscrollbar变量时不会改变。这是我的课程

  import java.awt.event.ItemListener;
  import java.awt.event.AdjustmentEvent;
  import java.awt.event.AdjustmentListener;

  public class subPanel2 extends JPanel
  { 
   JScrollBar redbar,greenbar,bluebar;
   JRadioButton decimal,octal,binary,hex;

   int rc, gc, bc;
   int count;

   public subPanel2 ()
   {    
    setPreferredSize (new Dimension(150,500));
    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

    JLabel red=new JLabel("Red");
    JLabel blue=new JLabel("Blue");
    JLabel green=new JLabel("Green");
    JLabel numsys=new JLabel("Numeral Systems");

    numsys.setForeground(Color.gray);

    JLabel colormix=new JLabel("Color Mix");
    colormix.setForeground(Color.gray);

    decimal=new JRadioButton("Decimal",true);
    octal=new JRadioButton("Octal");
    binary=new JRadioButton("Binary");
    hex=new JRadioButton("Hex");

    ButtonGroup group = new ButtonGroup();
    group.add(decimal);
    group.add(octal);
    group.add(binary);
    group.add(hex);

    decimal.addItemListener(new itemListener());
    octal.addItemListener(new itemListener());
    binary.addItemListener(new itemListener());
    hex.addItemListener(new itemListener());

    redbar= new JScrollBar(Adjustable.HORIZONTAL, 0, 0, 0, 255);
    redbar.setBlockIncrement(1);
    redbar.addAdjustmentListener(new adjustmentListener());

    greenbar= new JScrollBar(Adjustable.HORIZONTAL, 0, 0, 0, 255);
    greenbar.setBlockIncrement(1);
    greenbar.addAdjustmentListener(new adjustmentListener());

    bluebar= new JScrollBar(Adjustable.HORIZONTAL, 0, 0, 0, 255);
    bluebar.setBlockIncrement(1);
    bluebar.addAdjustmentListener(new adjustmentListener());

    add(numsys);
    add (Box.createRigidArea (new Dimension (0, 10)));
    add(decimal);
    add(octal);
    add(binary);
    add(hex);
    add (Box.createRigidArea (new Dimension (0, 30)));
    add(colormix);
    add (Box.createRigidArea (new Dimension (0, 10)));
    add(red);
    add(redbar);
    add(green);
    add(greenbar);
    add(blue);
    add(bluebar);
    } 

    public class adjustmentListener implements AdjustmentListener
    {
     public void adjustmentValueChanged(AdjustmentEvent adjustmentEvent) 
    {
      rc=redbar.getValue();
      gc=greenbar.getValue();
      bc=bluebar.getValue();
    }
    }

     private class itemListener implements ItemListener
     {
      public void itemStateChanged(ItemEvent itemEvent)
     {
      if (octal.isSelected()) count=8;
      else if (decimal.isSelected()) count=0;
      else if (binary.isSelected()) count=2;
      else if (hex.isSelected()) count=16;
     }
    }
    }




import javax.swing.JPanel;
import java.awt.*;

public class subPanel1 extends JPanel {

subPanel2 np;
public subPanel1 ()
{
 setPreferredSize (new Dimension(450,500));
}
 public void paintComponent (Graphics page)
{    
 np=new subPanel2();
 super.paintComponent (page);
 int p=10;
 String str="",str1="", str2="";
 String by="",by1="",by2="";

 if (np.count==2)
 {
  by = Integer.toBinaryString(np.rc);
  by1= Integer.toBinaryString(np.gc);
  by2 = Integer.toBinaryString(np.bc);
  str=by; 
  str1=by1;
  str2=by2; 
  }
  else if (np.count==16)
  {
by = Integer.toHexString(np.rc);
by1= Integer.toHexString(np.gc);
by2 = Integer.toHexString(np.bc);
str=by; 
str1=by1;
    str2=by2; 
   } 
   else
   { 
str=Integer.toString(np.rc,np.count);
str1=Integer.toString(np.gc,np.count);
    str2=Integer.toString(np.bc,np.count);
   }
    page.setColor (new Color(np.redbar.getValue(),np.gc,np.bc));
    page.fillOval(100, 340, 250, 150);
page.setColor(Color.red);
page.fillRect(85,260-np.rc,60,p+np.rc);
page.drawString("RED",100,310);
    page.drawString(str,105,325);    

page.setColor(Color.green);
page.fillRect(200,260-np.gc,60,p+np.gc);
page.drawString("GREEN",210,310);
page.drawString(str1,215,325);   

page.setColor (Color.blue);
page.fillRect(305,260-np.bc,60,p+np.bc);
page.drawString("BLUE",320,310);
page.drawString(str2,325,325);   
    }
    }`

3 个答案:

答案 0 :(得分:2)

创建一个返回变量的方法。谷歌是你的朋友,从长远来看,它可以更好地学习你自己的东西

答案 1 :(得分:0)

使用setter方法并确保必须调用setter的类类型的对象具有对提供setter的类类型的对象的引用。

更简洁的方法是使用Observer设计模式。

答案 2 :(得分:0)

您还必须显示其余代码以确定,但我怀疑您正在使用类subPanel2的两个不同的实例(这应该是Java编码约定的大写,方式)。

您可以尝试使用静态变量,这可能会解决您当前的问题。

public static int rc, gc, bc;

以这种方式访问​​它,例如:

by = Integer.toBinaryString(subPanel2.rc); 

但是:这不是一个很好的解决方案,所以你可能想重新考虑你的架构。

您真正想要做的是将subPanel2的原始实例的引用传递给subPanel1的实例,以便您可以使用方法来访问该实例的值。

相关问题