In the main class's constructor i have this actionlistener attached to a button:
home.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
Home homepage = new Home();
homepage.setBounds(0, 100, 500, 400);
add(homepage);
}
});
This is the Home class:
package test;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Home extends JPanel{
public Home(){
JButton jb = new JButton("Back");
add(jb);
}
}
The jb button doesn't show up on the jframe... can you help me out?
EDIT: It's actually there but you need to mouse over it to see it ... how can we request focus? like i do jb.requestFocus() and you still need to mouse over it.
When adding components dynamically, you need to revalidate()
and repaint()
add(homepage);
revalidate();
repaint();
Side Note: Have a look at Laying out Components withing a Container to learn how to use layout managers. They are preferred over using null layouts, as you are doing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.