java GUI JLabel

时间:2014-12-14 02:18:10

标签: java user-interface jlabel

我需要一点关于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);
    }
}

}

1 个答案:

答案 0 :(得分:1)

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