我有一个选项卡需要在程序运行时使用其类中的两种方法之一进行更新,因此我在方法中添加了组件,但我无法显示其中的任何一个。据我所知,这是因为它们在构造函数之外,我过去常常在方法中声明它们,但移动它以尝试解决问题。任何帮助将不胜感激。
抱歉,这有点混乱,我一直在尝试解决问题,有点盲目。
代码也可以正常编译,并且代码中的 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);
}
}