Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中将值传递到外部方法_Java_Swing_External - Fatal编程技术网

在Java中将值传递到外部方法

在Java中将值传递到外部方法,java,swing,external,Java,Swing,External,所以我有一个程序,可以根据JSlider的位置动态更新温度计条(使用drawRect)。我试图做的是将该值传递给一个外部方法,以便使用它来更新矩形的大小。我是一个VB的家伙,所以我很难想出怎么做 这是我的主要课程: import javax.swing.*; import java.awt.*; import javax.swing.event.*; public class ThermoProject extends JApplet { JSlider mySlider; J

所以我有一个程序,可以根据JSlider的位置动态更新温度计条(使用drawRect)。我试图做的是将该值传递给一个外部方法,以便使用它来更新矩形的大小。我是一个VB的家伙,所以我很难想出怎么做

这是我的主要课程:

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

public class ThermoProject extends JApplet
{
    JSlider mySlider;
    JPanel sliderPanel;
    JPanel northernPanel = new NorthPanel();
    JLabel printLabel = new JLabel("");
    static int fillSize = 0;
    int topSize = 100;


    public void init() //init is the "main" for an applet
    {
        buildSliderPanel();
        sliderPanel.setBackground(Color.gray);
        add(northernPanel, BorderLayout.NORTH); //separate class
        add(printLabel, BorderLayout.WEST); //add label to top
        add(sliderPanel, BorderLayout.SOUTH); //add the panel to the south of the borderLayout
    }



    private void buildSliderPanel()
    {
        sliderPanel = new JPanel();
        //JSlider(direction, beginning, end, initial location)
        mySlider = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);
        mySlider.setMajorTickSpacing(10);
        mySlider.setPaintTicks(true);
        mySlider.setPaintLabels(true);
        mySlider.setSnapToTicks(true);

        //add listener
        mySlider.addChangeListener(new SliderListener());

        //add to panel
        sliderPanel.add(mySlider);

    }


    public class SliderListener implements ChangeListener
    {

        public void stateChanged(ChangeEvent e)
        {
            //change when slider is moved
            updateThermo(mySlider.getValue());
            //topSize = 100 - fillSize;
            //drawRect(x, y, width, height)
            //fillRect(x, y, width, height)
            repaint();
        }
    }
}
这是我的子类,它主要用于构建一个单独的JPanel

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

public class NorthPanel extends JPanel
{
    JPanel theNorthernPanel;
    int fillSize = 0;

    public void paint(Graphics g)
    {
        //drawRect() and fillRect() args are x, y, width, height
        g.drawRect(90, 90, 200, 30);
        g.setColor(Color.red);
        g.fillRect(90, 90, fillSize, 30);
    }

    public NorthPanel()
    {
        JPanel theNorthernPanel = new JPanel();
        setPreferredSize(new Dimension(200, 200));
        JLabel printLabel = new JLabel(String.valueOf(fillSize));
        theNorthernPanel.add(printLabel);
        add(theNorthernPanel);

    }

    public void updateThermo(int temperature)
    {
        fillSize = temperature;
    }

}
试试这个:

NorthPanel northernPanel=新建NorthPanel();//申报

然后呢,


updateernmo(mySlider.getValue())//要调用

您绘制的代码已损坏,请首先查看和以了解更多详细信息

要调用方法,需要对要调用的类的实例进行引用

从改变开始

JPanel northernPanel = new NorthPanel();

然后用它来调用方法

northernPanel.updateThermo(mySlider.getValue());

我认为您应该将
SliderListener
的内容更改为:

public class SliderListener implements ChangeListener
{

    public void stateChanged(ChangeEvent e)
    {
        //change when slider is moved
        ((NorthPanel)northernPanel).updateThermo(mySlider.getValue());  
        //topSize = 100 - fillSize;
        //drawRect(x, y, width, height)
        //fillRect(x, y, width, height)
        northernPanel.repaint();
    }
}
在您展示的代码片段中,您正试图调用
ThermoProject
的成员。 还需要考虑强制转换到
northernPanel
的概念,这是必要的,因为您将
northernPanel
声明为更通用的类型:

JPanel northernPanel = new NorthPanel();

谢谢,只是我把northenPanel说错了。
JPanel northernPanel = new NorthPanel();