当前位置: 技术问答>java相关
请帮我看看这个程序有什么问题,我实在不知道错在哪?
来源: 互联网 发布时间:2015-04-03
本文导语: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TestScrollBar extends JFrame implements AdjustmentListener { public TestScrollBar() { setSize(300, 200); JPanel p=new JPanel(); p.setLayout(new GridLayout(3, 2)); add_Label_Scrol...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestScrollBar extends JFrame
implements AdjustmentListener
{
public TestScrollBar()
{
setSize(300, 200);
JPanel p=new JPanel();
p.setLayout(new GridLayout(3, 2));
add_Label_ScrollBar(p, redLabel, "Red 0", red);
add_Label_ScrollBar(p, greenLabel, "Green 0", green);
add_Label_ScrollBar(p, blueLabel, "Blue 0", blue);
Container contentPane=getContentPane();
contentPane.add(p, "South");
contentPane.add(colorPanel=new JPanel());
}
private void add_Label_ScrollBar(JPanel p, JLabel label,
String text, JScrollBar
scrollBar)
{
p.add(label=new JLabel(text));
p.add(scrollBar=new JScrollBar(Adjustable.HORIZONTAL,
0, 0, 0, 255));
scrollBar.setBlockIncrement(16);
scrollBar.addAdjustmentListener(this);
}
public void adjustmentValueChanged(AdjustmentEvent evt)
{
int r, g, b;
r=red.getValue(); // 执行这些语句时发生
g=green.getValue(); // 很多异常!
b=blue.getValue(); //
}
public static void main(String[] args)
{
JFrame frm=new TestScrollBar();
frm.show();
}
private JLabel redLabel;
private JLabel greenLabel;
private JLabel blueLabel;
private JScrollBar red;
private JScrollBar green;
private JScrollBar blue;
private JPanel colorPanel;
}
import java.awt.*;
import java.awt.event.*;
public class TestScrollBar extends JFrame
implements AdjustmentListener
{
public TestScrollBar()
{
setSize(300, 200);
JPanel p=new JPanel();
p.setLayout(new GridLayout(3, 2));
add_Label_ScrollBar(p, redLabel, "Red 0", red);
add_Label_ScrollBar(p, greenLabel, "Green 0", green);
add_Label_ScrollBar(p, blueLabel, "Blue 0", blue);
Container contentPane=getContentPane();
contentPane.add(p, "South");
contentPane.add(colorPanel=new JPanel());
}
private void add_Label_ScrollBar(JPanel p, JLabel label,
String text, JScrollBar
scrollBar)
{
p.add(label=new JLabel(text));
p.add(scrollBar=new JScrollBar(Adjustable.HORIZONTAL,
0, 0, 0, 255));
scrollBar.setBlockIncrement(16);
scrollBar.addAdjustmentListener(this);
}
public void adjustmentValueChanged(AdjustmentEvent evt)
{
int r, g, b;
r=red.getValue(); // 执行这些语句时发生
g=green.getValue(); // 很多异常!
b=blue.getValue(); //
}
public static void main(String[] args)
{
JFrame frm=new TestScrollBar();
frm.show();
}
private JLabel redLabel;
private JLabel greenLabel;
private JLabel blueLabel;
private JScrollBar red;
private JScrollBar green;
private JScrollBar blue;
private JPanel colorPanel;
}
|
同志:
你的red等等都只是有名无实,尚未new呀!昏头了吗?
你的red等等都只是有名无实,尚未new呀!昏头了吗?
|
先初始化
int r=0, g=0, b=0;
r=red.getValue();
g=green.getValue();
b=blue.getValue();
还有就是楼上说的变量需要先NEW出来啊。
int r=0, g=0, b=0;
r=red.getValue();
g=green.getValue();
b=blue.getValue();
还有就是楼上说的变量需要先NEW出来啊。
|
怎么说你好呢?