我正在编写一个程序,该程序应该有一个窗口,其中包含容器内所有组的各种组件。当您单击“获取入门”按钮时,它应该打开另一个窗口,其中包含在case2:在GlobalTeenagerProject类中列出的容器。问题是当你点击它时,它会弹出一个新窗口,但窗口是完全空白的。有谁知道它有什么问题吗?源代码是:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class GlobalTeenagerProject extends Frame implements ActionListener, ItemListener{
private static final long serialVersionUID = 1L;
int ACTSATOp;
TextField ApNumberField=new TextField(20),
ApScoreField=new TextField(20),
ACTField=new TextField(20),
SATField=new TextField(20),
leadershipNumberField=new TextField(20),
leadershipHoursField=new TextField(20),
leadershipLengthField =new TextField(20),
serviceHoursField = new TextField(20),
GPAField = new TextField(20);
Label ApNumberLabel = new Label ("Please enter the number of AP tests you have taken");
Label APScoreLabel = new Label ("Please input your first AP test score with no decimal point");
Label leadershipNumberLabel = new Label ("Please input the number of Leadership positions you have held");
Label serviceHoursLabel = new Label ("Please enter the number of community service hours you have done since starting high school");
Label leadershipLengthLabel = new Label ("How long did you serve in your 1st leadership position");
Label GPALabel = new Label ("What is your GPA?");
Button submitButton = new Button ("Submit");
Button getStartedButton = new Button ("Get Started");
Button seeStatsButton = new Button("Compare yourself");
Button goButton = new Button("Go");
CheckboxGroup ACTSATgroup;
Checkbox ACTBox = new Checkbox ("ACT",false,ACTSATgroup);
Checkbox SATBox = new Checkbox("SAT", false, ACTSATgroup);
Checkbox hiddenBox = new Checkbox("",true,ACTSATgroup);
Checkbox bothBox = new Checkbox("Both", false, ACTSATgroup);
double ACT, SAT, serviceHours, leadershipNumber, LNumberLength, APLength, GPAnumber;
double leadershipLengthArray[], APTestNumber[], ApScore[];
Button button[] = {submitButton, getStartedButton, seeStatsButton, goButton};
Panel ACTSATpanel = new Panel();
Panel GPAPanel = new Panel();
Panel APpanel = new Panel();
Panel servicePanel = new Panel();
Panel leadershipPanel = new Panel();
Panel buttonPanel = new Panel();
Panel mainPanel = new Panel();
int j=0;
public GlobalTeenagerProject(int type) {
switch(type){
case 1:
this.setLayout(new BorderLayout());
buttonPanel.setLayout(new GridLayout(2,1,10,10));
add(buttonPanel,BorderLayout.CENTER);
buttonPanel.add(button[1]);
button[1].addActionListener(this);
buttonPanel.add(button[2]);
button[2].addActionListener(this);
addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
break;
case 2:
this.setLayout(new GridLayout(6,1,10,10));
ACTSATpanel.setLayout(new GridLayout(2,3,10,10));
APpanel.setLayout(new GridLayout(2,2,10,10));
servicePanel.setLayout(new GridLayout(5,2,10,10));
leadershipPanel.setLayout(new GridLayout(1,2,10,10));
GPAPanel.setLayout(new FlowLayout());
buttonPanel.setLayout (new GridLayout (2,1,10,10));
GPAPanel.add(GPALabel);
GPAPanel.add(GPAField);
ACTSATpanel.add(ACTBox);
ACTSATpanel.add(SATBox);
APpanel.add(ApNumberLabel);
APpanel.add(ApNumberField);
APpanel.add(APScoreLabel);
APpanel.add(ApScoreField);
servicePanel.add(serviceHoursLabel);
servicePanel.add(serviceHoursField);
leadershipPanel.add(leadershipNumberLabel);
leadershipPanel.add(leadershipNumberField);
leadershipPanel.add(goButton);
goButton.addActionListener(this);
buttonPanel.add(submitButton);
submitButton.addActionListener(this);
addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
break;
case 3:
// put in graphing component
break;
}
}
public static void main(String[]args){
GlobalTeenagerProject k = new GlobalTeenagerProject(1);
k.setVisible(true);
k.setBounds(200, 200, 600, 600);
k.setTitle("CollegeConnections");
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()== button[1]){
GlobalTeenagerProject i = new GlobalTeenagerProject(2);
i.setVisible(true);
i.setBounds(200,200, 600, 600);
}
else{
if(e.getSource()==button[0]){
remove(GPAPanel);
remove(ACTSATpanel);
remove(APpanel);
remove(servicePanel);
remove(leadershipPanel);
buttonPanel.remove(submitButton);
this.setLayout(new BorderLayout());
buttonPanel.setLayout(new GridLayout(2,1,10,10));
buttonPanel.add(getStartedButton);
buttonPanel.add(seeStatsButton);
add(buttonPanel,BorderLayout.CENTER);
}
else{
if(e.getSource()=="Submit"){
GlobalTeenagerProject j= new GlobalTeenagerProject(3);
j.setVisible(true);
j.setBounds(200, 200, 600, 800);
}
else{
if(e.getSource()==button [3]){
int leadershipNumber = 0;
leadershipPanel.setLayout(null);
}
}
}
}
}
public void itemStateChanged(ItemEvent f) {
if (ACTBox.getState()){
ACTSATOp = 1;
ACTSATpanel.add(ACTField);
}
else{
if(SATBox.getState()){
ACTSATOp =2;
ACTSATpanel.add(SATField);
}
else{
if(bothBox.getState()){
ACTSATOp =3;
ACTSATpanel.add(ACTField);
ACTSATpanel.add(SATField);
}
}
}
}
}
答案 0 :(得分:0)
您是否尝试调整窗口大小?如果组件显示在您之后调整大小,则意味着您必须在程序结束时(创建窗口时)调用方法WHATEVER_FRAME.repaint()
。
答案 1 :(得分:0)
我想您忘了将所有面板添加到相框中。请尝试将此添加到您的代码中:
this.add(ACTSATpanel);
this.add(APpanel);
this.add(servicePanel);
this.add(leadershipPanel);
this.add(GPAPanel);
this.add(buttonPanel);