Java 从另一个类调用JPanel到主类';面板
下午好 作为一个旨在帮助我练习使用JPanels和子类的个人项目,我正在编写一个包含我的教育、工作和志愿者经验的专业网络公文包,以及其他使用JApplets的著名作品 我将布局设置为BorderLayout,JButtons向西对齐。当点击一个按钮时,中间的JPanel应该与相应的面板一起切换。然而,我还没有走那么远 到目前为止,我的主页JPanel上只有一个JLabel,上面写着“Home”,因为我想在做更多的事情之前确保Home JPanel类中的方法正常工作。问题是小程序上没有显示JPanel 问题是,当我将所有代码从JPanel的类移动到主类时,它显示得很好。所以我知道问题在于如何构造方法,或者如何构造JPanel的类 我试着把它设为可见,但没用。我尝试将LayoutManager设置为类构造函数的参数,尝试添加paintComponent和super.paint(g),尝试使用this.home.addHomePanel-,但没有任何效果 我知道我错过了一些东西。如果有人能帮我一把,我将不胜感激。如果你需要更多信息,请告诉我。谢谢你的阅读 主要类别:Java 从另一个类调用JPanel到主类';面板,java,jpanel,japplet,Java,Jpanel,Japplet,下午好 作为一个旨在帮助我练习使用JPanels和子类的个人项目,我正在编写一个包含我的教育、工作和志愿者经验的专业网络公文包,以及其他使用JApplets的著名作品 我将布局设置为BorderLayout,JButtons向西对齐。当点击一个按钮时,中间的JPanel应该与相应的面板一起切换。然而,我还没有走那么远 到目前为止,我的主页JPanel上只有一个JLabel,上面写着“Home”,因为我想在做更多的事情之前确保Home JPanel类中的方法正常工作。问题是小程序上没有显示JPan
public class myWebFolio extends JApplet implements ActionListener
{
JButton[ ] menu =
{
new JButton("Home"),
new JButton("Education"),
new JButton("Work Experience"),
new JButton("Programming Projects"),
new JButton("Other")
};
//adds panel to memory
private JPanel buttonPanel = new JPanel();
private Home home;
public void init()
{
setLayout (new BorderLayout( ) ); //changes the layout of the appl
home = new Home();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
/*
* Adds an ActionListener to each button
* and then adds the button to the buttonPanel.
* Also adds an invisible componenet to give the buttons
* spacing.
*/
for (int i=0; i<menu.length; i++)
{
menu[i].addActionListener(this);
buttonPanel.add(Box.createVerticalStrut(100));
buttonPanel.add(menu[i]);
}
add(buttonPanel,BorderLayout.WEST);
home.addHomePanel();
}
public void actionPerformed(ActionEvent event)
{
}
public void paintComponent(Graphics g)
{
super.paint(g);
}
}
*“当点击一个按钮时,中间的JPanel应该通过相应的面板切换出去”-请看在你的超级类中,你从不向任何东西添加
home
。你没有正确使用术语“子类”和“超级类”。顺便说一下,我不知道home
添加到applet的哪里?谢谢你的反馈。很抱歉,如果我用错了任何术语,我只是最近才开始学习编程。我浏览了我的帖子,用(希望)正确的词替换了错误的词。很抱歉这么慢,但是当你说没有将home添加到applet时,你是什么意思?你可能不是指私人住宅;或家=新家();,正确的?
public class Home extends JPanel
{
JPanel homePanel = new JPanel(new FlowLayout());
JLabel label = new JLabel("Home");
/**
* Constructor for objects of class Home
*/
public Home()
{
}
public void addHomePanel()
{
homePanel.add(label, FlowLayout.LEFT);
homePanel.setVisible(true);
add(homePanel, BorderLayout.CENTER);
}
public void paintComponent(Graphics g)
{
super.paint(g);
}
}