动态 JPanel 对象不显示
有人可以看一下我的代码的这一部分并告诉我为什么它不会返回 JPanel 内的对象吗?它肯定进入循环内部,因为我尝试在内部打印语句。另外,这个 JPanel 对象被放入 TabbedPane 中只是为了澄清。如果我需要更详细地解释或显示更多代码来找到解决方案,请告诉我。谢谢。
JPanel createTipTailoringPanel(TipCalcModel model)
{
JPanel content = new JPanel();
int size = model.getNumOfPeople();
content.removeAll();
content.updateUI();
content.setLayout(new GridLayout(0,4));
JTextField text[] = new JTextField[size];
JSlider slider[] = new JSlider[size];
JLabel label[] = new JLabel[size];
JLabel cash[] = new JLabel[size];
if(size == 0)
{
return content;
}
else
{
for(int i=0; i<size; i++)
{
text[i] = new JTextField();
slider[i] = new JSlider();
label[i] = new JLabel("$");
cash[i] = new JLabel();
content.add(text[i]);
content.add(slider[i]);
content.add(label[i]);
content.add(cash[i]);
}
return content;
}
}
这是我的调用方法和用于传递 numberofpeople 的动作侦听器:
TipCalcView(TipCalcModel model)
{
setTitle("Tip Calculator");
JTabbedPane tabbedPane = new JTabbedPane();
getContentPane().add(tabbedPane);
tabbedPane.addTab("Main Menu", createMainPanel());
tabbedPane.setSelectedIndex(0);
tabbedPane.addTab("Tip Tailoring", createTipTailoringPanel(model));
tabbedPane.addTab("Config Panel", createConfigPanel());
}
class GuestsListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int userInput = 0;
try{
userInput = m_view.getGuests();
m_model.setNumOfPeople(userInput);
m_view.createTipTailoringPanel(m_model);
}
catch(NumberFormatException nfex)
{
m_view.showError("Bad input: '" + userInput + "'");
}
}
}
Can someone take a look at this part of my code and tell me why it won't return the objects inside the JPanel? It definitely goes inside the loop since I tried printing statements inside. Also this JPanel object is being put inside a TabbedPane just for clarification. Let me know if I need to explain in more detail or show more code to find a solution. Thanks.
JPanel createTipTailoringPanel(TipCalcModel model)
{
JPanel content = new JPanel();
int size = model.getNumOfPeople();
content.removeAll();
content.updateUI();
content.setLayout(new GridLayout(0,4));
JTextField text[] = new JTextField[size];
JSlider slider[] = new JSlider[size];
JLabel label[] = new JLabel[size];
JLabel cash[] = new JLabel[size];
if(size == 0)
{
return content;
}
else
{
for(int i=0; i<size; i++)
{
text[i] = new JTextField();
slider[i] = new JSlider();
label[i] = new JLabel("$");
cash[i] = new JLabel();
content.add(text[i]);
content.add(slider[i]);
content.add(label[i]);
content.add(cash[i]);
}
return content;
}
}
Here is my calling method and the actionlistener that I use to pass in the numberofpeople:
TipCalcView(TipCalcModel model)
{
setTitle("Tip Calculator");
JTabbedPane tabbedPane = new JTabbedPane();
getContentPane().add(tabbedPane);
tabbedPane.addTab("Main Menu", createMainPanel());
tabbedPane.setSelectedIndex(0);
tabbedPane.addTab("Tip Tailoring", createTipTailoringPanel(model));
tabbedPane.addTab("Config Panel", createConfigPanel());
}
class GuestsListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int userInput = 0;
try{
userInput = m_view.getGuests();
m_model.setNumOfPeople(userInput);
m_view.createTipTailoringPanel(m_model);
}
catch(NumberFormatException nfex)
{
m_view.showError("Bad input: '" + userInput + "'");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑您的问题超出了您列出的代码范围。这是一个简化的工作示例:
I suspect that your problem is outside of the code you listed. Here's a simplified working example:
首先,由于您不在任何地方使用数组,因此可以将其缩短为:
其次,似乎您向面板添加了一个空组件,也许这就是您实际得到的?
第三,添加一旦从上面的方法返回,您需要将
content
面板添加到 JFrame(或其他容器)。First, since you don't use the arrays anywhere, it could be shortened to:
Second, seems like you add an empty components to the panel, maybe that's what you actually get?
Third, add you need to add the
content
panel to the JFrame (or other container) once it returns from the method above.