The black is JFrame c开发者_开发百科ontent and the red one is JPanel's. This happens a lot when resizing the JFrame. A little less, when resizing the frame from upper left corner, but still happens. The panel is not able to keep up with the frame change.
Why, is it "fixable" ?
Thanks
The following lines provide the desired behavior on a JFrame:
public class MyFrame extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MyFrame().setVisible(true);
}
});
}
public MyFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBackground(Color.black);
JPanel panel = new JPanel();
panel.setBackground(Color.red);
getContentPane().add(panel);
pack();
}
}
精彩评论