在构造函数外部添加的组件不会出现

时间:2012-06-01 12:55:12

标签: java swing layout-manager

我有一个标签需要在程序运行时使用其中的两个方法之一进行更新,因此我在方法中添加了组件,我无法显示它们中的任何一个。据我所知,因为它们在构造函数之外,我曾经在方法中声明它们,但是移动它来尝试修复问题。任何帮助将不胜感激。

我道歉它有点乱,我一直试图解决这个问题,有点盲目。

此外,代码编译良好,并且代码中的println输出按预期显示。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package assgui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.io.*;
/**
 *
 * @author Hugh
 */
public class ResultPanel extends JPanel {

    static String[][] activityString = new String[31][2];
    static String[][] foodString = new String[36][2];

        String weighstr, foodstr, servstr, kjstr, actstr, hourstr, minstr, metstr;

        JLabel uw = new JLabel ("User Weight:  ");
        JLabel weigh = new JLabel (weighstr);
        JLabel kg = new JLabel("  kg");
        JLabel sel1 = new JLabel("Food:  ");
        JLabel sel2 = new JLabel(foodstr);
        JLabel sel3 = new JLabel("  -  kj  ");
        JLabel sel4 = new JLabel(kjstr);
        JLabel sel5 = new JLabel("  ,  Servings:  ");
        JLabel sel6 = new JLabel(servstr);
        JLabel sel7 = new JLabel("Activity for comparison:  ");
        JLabel sel8 = new JLabel(actstr);
        JLabel sel9 = new JLabel("  Time required to balance:  ");
        JLabel sel10 = new JLabel(hourstr);
        JLabel sel11 = new JLabel("  hours");
        JLabel sel12 = new JLabel(minstr);
        JLabel sel13 = new JLabel("  minutes");
        JLabel smlspace = new JLabel("          ");
        JLabel medspace = new JLabel("               ");
        JLabel lrgspace = new JLabel("                         ");

        JLabel auw = new JLabel("User Weight:  ");
        JLabel aweigh = new JLabel(weighstr);
        JLabel akg = new JLabel("  kg");
        JLabel asel1 = new JLabel("Activity:  ");
        JLabel asel2 = new JLabel(actstr);
        JLabel asel3 = new JLabel("  -  MET  ");
        JLabel asel4 = new JLabel(metstr);
        JLabel asel5 = new JLabel("  ,  Duration:  ");
        JLabel asel6 = new JLabel(hourstr);
        JLabel asel7 = new JLabel("  hour  ");
        JLabel asel8 = new JLabel(minstr);
        JLabel asel9 = new JLabel("  minutes  ");
        JLabel asel10 = new JLabel("Food for comparison:  ");
        JLabel asel11 = new JLabel(foodstr);
        JLabel asel12 = new JLabel("  Servings to balance:  ");
        JLabel asel13 = new JLabel(servstr);
        JLabel asmlspace = new JLabel("          ");
        JLabel amedspace = new JLabel("               ");
        JLabel alrgspace = new JLabel("                         ");

        Public ResultPanel() {
        setBackground(Color.GREEN);
        setPreferredSize(new Dimension(650, 600));
        {

    public void activityPaint (String[][] actstring, String[][] foodstring, double        weight, int activity, int hour, int min, int food, double servings) {

        System.out.println("act1");
        weighstr = Double.toString(weight);
        actstr = activityString[activity][0];
        hourstr = Integer.toString(hour);
        minstr = Integer.toString(min);
        metstr = activityString[activity][1];
        foodstr = foodString[food][0];
        servstr = Double.toString(servings);

        add(lrgspace);
        add(uw);
        add(weigh);
        add(kg);
        add(medspace);
        add(sel1);
        add(sel2);
        add(sel3);
        add(sel4);
        add(sel5);
        add(sel6);
        add(sel7);
        add(sel8);
        add(sel9);
        add(sel10);
        add(sel11);
        add(sel12);
        add(sel13);

    }

    public void foodPaint(String[][] foodstring, String[][] actstring, double weight, int food, int servings, int activity, int hour, int min) {

        System.out.println("food1");
        weighstr = Double.toString(weight);
        foodstr = foodString[food][0];
        servstr = Integer.toString(servings);
        kjstr = foodString[food][1];
        actstr = activityString[activity][0];
        hourstr = Integer.toString(hour);
        minstr = Integer.toString(min);


        add(lrgspace);
        add(uw);
        add(weigh);
        add(kg);
        add(medspace);
        add(sel1);
        add(sel2);
        add(sel3);
        add(sel4);
        add(sel5);
        add(sel6);
        add(sel7);
        add(sel8);
        add(sel9);
        add(sel10);
        add(sel11);
        add(sel12);
        add(sel13);

    }

}

1 个答案:

答案 0 :(得分:1)

创建此JPanel的主方法(或任何方法)是否也调用activityPaint(...)和foodPaint(...)方法?如果没有调用这些方法,那么它们似乎永远不会被添加到面板中。如果你想在创建JPanel时添加所有内容,那么你必须让你的构造函数调用activityPaint()和foodPaint()。

如果在发生其他事情之前无法设置JLable的值(如用户输入),则可以在创建JPanel时添加组件,然后再调用setText(...)方法。只要确定你是否这样做也设置了大小,因为更改文本会影响它。我更喜欢使用 nameofjpanel .setSize( nameofjpanel .getPreferredSize())。

希望这有助于你,我并没有完全错过你在这里的目标。