我的这个嵌套类有问题。由于某些原因,我无法在构造函数中将我的JComponents添加到我的面板中。它给出了错误
The method of add(JComponent, String) is undefined for the HomePanel.inputFrame
HomePanel是外部类,而inputFrame是我的内部类。我的方法返回JComponents,我可以将它添加到我的外部类,但我的内部类只是不允许它所以我决定只使用add.(enterButton(), BorderLayout.SOUTH)
没有给出任何错误但是当我尝试使用mainWindow类中的这个内部类,它说
The method add(JComponent) in the type container is not applicable for the arguments (HomePanel.inputFrame)
但问题是我可以使用mainWindow类的外部类,这使得它出了什么问题令人困惑。是因为它是内部阶级还是我的命令错了?
mainWindow类
public class mainWindow extends JFrame{
CardLayout cl = new CardLayout();
private JFrame mainPage = new JFrame("Food Expiry");
private JPanel mainPanel = new JPanel(cl);
private static final String homePage = "HomePage";
private static final String expiryDateEntry = "Expiry Date Entry";
private JPanel card1 = new JPanel();
public JPanel card2 = new JPanel();
HomePanel getGui = new HomePanel();
HomePanel.inputFrame nextPane = getGui.new inputFrame();
CardLayout cardLayout = (CardLayout) mainPanel.getLayout();
public mainWindow(){
mainPage.setSize(500, 300);
mainPage.setLocationRelativeTo(null);
mainPage.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPage.add(mainPanel);
mainPage.setVisible(true);
}
protected void mainPanel(){
card1.add(getGui);
}
protected void getInput(){
card2.add(nextPane);
}
}
HomePanel
public class HomePanel extends JPanel{
private JButton inputButton, storeButton;
private JLabel label, itemLabel;
private JPanel textAreaPanel, buttonPanel, labelPanel;
private JTextField itemField, itemCategory, expiryDate;
public HomePanel(){
setLayout(new BorderLayout());
this.add(getLabelPanel("Home"), BorderLayout.NORTH);
this.add(getButtonPanel(), BorderLayout.CENTER);
}
protected JComponent getButtonPanel(){
//add some buttons to a buttonPanel
return buttonPanel;
}
protected JComponent getLabelPanel(String topText){
//labelPanel
return labelPanel;
}
class inputFrame {
private JLabel categoryLabel, expiryLabel;
private JButton enter, back;
protected JComponent getInputFrame(){
textAreaPanel = new JPanel();
textAreaPanel.setLayout(new BoxLayout(textAreaPanel,
BoxLayout.Y_AXIS));
itemLabel = new JLabel("Item Name: ");
itemField = new JTextField(15);
textAreaPanel.add(itemLabel);
textAreaPanel.add(itemField);
categoryLabel = new JLabel("Category Name: ");
itemCategory = new JTextField(33);
textAreaPanel.add(categoryLabel);
textAreaPanel.add(itemCategory);
expiryLabel = new JLabel("Expiry Date: ");
expiryDate = new JTextField(33);
textAreaPanel.add(expiryLabel);
textAreaPanel.add(expiryDate);
return textAreaPanel;
}
protected JComponent enterButton(){
buttonPanel = new JPanel();
enter = new JButton("Enter");
back = new JButton("Back");
enter.addActionListener(new getInput());
back.addActionListener(new previousFrame());
buttonPanel.add(back);
buttonPanel.add(enter);
return buttonPanel;
}
public inputFrame(){
setLayout(new BorderLayout());
add(getLabelPanel("Expiry Date Entry"),BorderLayout.NORTH);
add(getInputFrame(), BorderLayout.CENTER);
add(enterButton(), BorderLayout.SOUTH);
}
}
我无法在此问题上找到任何重复项,如果有人能够找到任何副本,请在评论中将其链接或回答。谢谢。
答案 0 :(得分:0)
所以,基于......
public class HomePanel extends JPanel{
//...
class inputFrame {
//...
}
}
inputFrame
从Object
扩展(默认情况下),使其与Container#add
兼容,Component
期望某些类从inputFrame
扩展。
根据当前的实现,HomePanel
实际上正在修改实例化public class HomePanel extends JPanel{
//...
class inputFrame extends JPanel {
//...
}
}
实例的状态,而不是向自身添加组件
像...一样的东西。
docker run -d -p 9001:8081 --name nexus -v /Users/user.name/dockerVolume/nexus:/nexus-data sonatype/nexus3
应该缓解最紧迫的问题