我需要有关Java温度转换GUI的一些帮助

所以这是我的结果代码

public class ResultsPanel extends JPanel
{
private JLabel result;
private JPanel panel;
final int WIDTH_CONST=120;
final int HEIGHT_CONST=60;

public ResultsPanel()
{
    setPreferredSize(new Dimension(WIDTH_CONST, HEIGHT_CONST));

    setBorder(BorderFactory.createTitledBorder("Result"));

    result = new JLabel();


    add(result);

}

public void setResultLabel(String str)
{
    result.setText(str);
}

public void setResultLabel()
{
    result.setText("");
}
}


所以我想做的是,当我单击转换按钮时,我想标记结果

这是我的按钮处理程序类:

private class ConvertButtonHandler implements ActionListener
{
    public void actionPerformed (ActionEvent e)
    {
       //calculate and convert temperatures
       //convert the temperature into String

        ResultsPanel result = new ResultsPanel();

        String temp; // temperature in string

        result.setResultLabel(temp);

    }
}


但似乎并没有打印出结果,

任何帮助表示赞赏,谢谢

更新:

here is my tempGUI class:

public class TempGUI extends JFrame
{
private BannerPanel banner;
private TypePanel type;
private TemperaturePanel temperature;
private ResultsPanel results;
private JPanel buttonPanel;

private JButton convert;
private JButton clear;
private JButton exit;
public TempGUI()
{
    setTitle("Temperature Concverter GUI");

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setLayout(new BorderLayout());

    banner = new BannerPanel();
    type = new TypePanel();
    temperature = new TemperaturePanel();
    results = new ResultsPanel();

    buildButtonPanel();

    add(banner, BorderLayout.NORTH);
    add(type, BorderLayout.WEST);
    add(temperature, BorderLayout.CENTER);
    add(results, BorderLayout.EAST);
    add(buttonPanel, BorderLayout.SOUTH);

    pack();
    setVisible(true);
}

public void buildButtonPanel()
{
    buttonPanel = new JPanel();

    convert = new JButton("Convert");
    clear = new JButton("Clear");
    exit = new JButton("Exit");

    convert.addActionListener(new ConvertButtonHandler());
    clear.addActionListener(new ClearButtonHandler());
    exit.addActionListener(new ExitButtonHandler());

    buttonPanel.add(convert);
    buttonPanel.add(clear);
    buttonPanel.add(exit);
}
private class ConvertButtonHandler implements ActionListener
{
    public void actionPerformed (ActionEvent e)
    {
        double celsius = 0, fahrenheit = 0;

        DecimalFormat decimalFormatter = new DecimalFormat("0,00");

        TypePanel type = new TypePanel();
        TemperaturePanel temp = new TemperaturePanel();
        ResultsPanel result = new ResultsPanel();

        String temp1 = "Test";
        result.setResultLabel(temp1);
        add(result);
    }
}

private class ClearButtonHandler implements ActionListener
{
    public void actionPerformed (ActionEvent e)
    {
        ResultsPanel results = new ResultsPanel();
        results.setResultLabel();
        TemperaturePanel temp = new TemperaturePanel();

    }
}

private class ExitButtonHandler implements ActionListener
{
    public void actionPerformed (ActionEvent e)
    {
        System.exit(0);
    }
}


}

最佳答案

您正在创建ResultsPanel,但没有将其添加到任何框架。因此,不会显示ResultsPanel。您将必须为ResultsPanel创建框架或将其添加到现有的框架中,例如yourFrame.add(result)

10-08 01:44