程序报错 frame.getContentPane().add(panel);

xinuaile 2003-10-10 02:45:37
程序报错 :---------------------Configuration: JDK version <Default>--------------------
E:\niit\Applicant.java:40: cannot resolve symbol
symbol : variable frame
location: class Applicant
frame.getContentPane().add(panel);
=====================

String []C ={"China","india","English","American"};
JList l=new JList (C);
l.setSelectionMode(2);//0only single,1 continuous-shift key,2 any sequence- ctrl key

String position[] = { "Manager","Executive", "Associate"};
comboAppPosition = new JComboBox(position);

frame.getContentPane().add(panel); ====这里报错
panel.add(labelAppID);
panel.add(textAppID);

//Adding controls for Applicant name
panel.add(labelAppName);
panel.add(textAppName);
//Adding controls for Applicant Address
panel.add(labelAppAddress);
panel.add(textAppAddress);

//Adding controls for Applicant Position
panel.add(labelAppPosition);
panel.add(comboAppPosition);
panel.add(l);

//Create a panel


//Create and add the appropriate controls

}
public static void main(String args[])
{
//Creating the JFrame object
JFrame frame = new JFrame("Applicant Details");
//making the frame visible
Applicant appObject;
appObject = new Applicant();
frame.setVisible(true);
frame.setSize(300,300);
}
...全文
423 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangFrame 2003-10-10
  • 打赏
  • 举报
回复

import javax.swing.*;

public class Applicant
{ //Variable for frame window
//main is static,it will call this
public static void main(String args[])
{
//Creating the JFrame object
ApplicantFrame frame = new ApplicantFrame();
//making the frame visible
frame.setVisible(true);
frame.setSize(300,300);
frame.setTitle("Applicant Details");
}
}

class ApplicantFrame extends JFrame
{
JPanel panel;
//variables of labels
JLabel labelAppID;
JLabel labelAppName;
JLabel labelAppAddress;
JLabel labelAppPosition;

//variables for data entry controls
JTextField textAppID;
JTextField textAppName;
JTextField textAppAddress;
JComboBox comboAppPosition;

public ApplicantFrame()
{
//Create a panel
panel = new JPanel();

labelAppID = new JLabel("Applicant ID");
labelAppName = new JLabel("Name");
labelAppAddress = new JLabel("Address");
labelAppPosition = new JLabel("Position");

//Initializing JTextField
textAppID = new JTextField(5);
textAppName = new JTextField(30);
textAppAddress = new JTextField(30);

String []C ={"China","india","English","American"};
JList l=new JList (C);
l.setSelectionMode(2);//0only single,1 continuous-shift key,2 any sequence- ctrl key

String position[] = { "Manager","Executive", "Associate"};
comboAppPosition = new JComboBox(position);


panel.add(labelAppID);
panel.add(textAppID);

//Adding controls for Applicant name
panel.add(labelAppName);
panel.add(textAppName);
//Adding controls for Applicant Address
panel.add(labelAppAddress);
panel.add(textAppAddress);

//Adding controls for Applicant Position
panel.add(labelAppPosition);
panel.add(comboAppPosition);
panel.add(l);
getContentPane().add(panel);
//Create a panel
}
}
Yanbin_Q 2003-10-10
  • 打赏
  • 举报
回复
注意你的frame的作用域是全局还是某个局部。
你的frame是在main()方法里申明的,不能在main()方法引用

你应该申明为类的全局对象。
xinuaile 2003-10-10
  • 打赏
  • 举报
回复
xinuaile 2003-10-10
  • 打赏
  • 举报
回复
getContentPane().add(panel);或add(panel); 都不行


cannot resolve symbol
symbol : method getContentPane ()
location: class Applicant
getContentPane().add(panel);

===========全部代码

import javax.swing.*;
public class Applicant
{ //Variable for frame window
//main is static,it will call this
JPanel panel;

//variables of labels
JLabel labelAppID;
JLabel labelAppName;
JLabel labelAppAddress;
JLabel labelAppPosition;

//variables for data entry controls
JTextField textAppID;
JTextField textAppName;
JTextField textAppAddress;
JComboBox comboAppPosition;

public Applicant()
{ //Create a panel
panel = new JPanel();

labelAppID = new JLabel("Applicant ID");
labelAppName = new JLabel("Name");
labelAppAddress = new JLabel("Address");
labelAppPosition = new JLabel("Position");

//Initializing JTextField
textAppID = new JTextField(5);
textAppName = new JTextField(30);
textAppAddress = new JTextField(30);

String []C ={"China","india","English","American"};
JList l=new JList (C);
l.setSelectionMode(2);//0only single,1 continuous-shift key,2 any sequence- ctrl key

String position[] = { "Manager","Executive", "Associate"};
comboAppPosition = new JComboBox(position);


panel.add(labelAppID);
panel.add(textAppID);

//Adding controls for Applicant name
panel.add(labelAppName);
panel.add(textAppName);
//Adding controls for Applicant Address
panel.add(labelAppAddress);
panel.add(textAppAddress);

//Adding controls for Applicant Position
panel.add(labelAppPosition);
panel.add(comboAppPosition);
panel.add(l);
add(panel);
//Create a panel


//Create and add the appropriate controls

}
public static void main(String args[])
{
//Creating the JFrame object
JFrame frame = new JFrame("Applicant Details");
//making the frame visible
Applicant appObject;
appObject = new Applicant();
frame.setVisible(true);
frame.setSize(300,300);
}

}
207 2003-10-10
  • 打赏
  • 举报
回复
如果不行去掉frame,即getContentPane().add(panel);或add(panel);
207 2003-10-10
  • 打赏
  • 举报
回复
..........


Panel panel=new Panel();
panel.add(labelAppID);
panel.add(textAppID);

//Adding controls for Applicant name
panel.add(labelAppName);
panel.add(textAppName);
//Adding controls for Applicant Address
panel.add(labelAppAddress);
panel.add(textAppAddress);

//Adding controls for Applicant Position
panel.add(labelAppPosition);
panel.add(comboAppPosition);
panel.add(l);
frame.getContentPane().add(panel);

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧