我试图将值从一个类传递到另一个类。类 subPanel1 读取全局变量 bu 当我通过 AdjustmentListener 更新这些变量时,它不会更改值。我试图将 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);
}
}`