简体   繁体   中英

JFrame vs JPanel and then JFrame

I have a program which displays one ellipses (Ellipse2D) .

Should I directly add the ellipse to a JFrame or should I add it to a JPanel, which ultimately is added to the JFrame? (Adding a JPanel is more work)

Which one will help me in the long run? (I might consider putting keybindings.)

To make things clearer:

Should I do:

public class Test extends JFrame{ // This is a JFrame

Ellipse ellipse = new Ellipse(); // I have an ellipse class

Test(){

...
add(ellipse);
...

}

}

Or should I do:

public class Test extends JFrame{

Test2 test2 = new Test2();

Test(){
...
add(test2)
...
}
}

public class Test2 extends JPanel{ // This is a JPanel

Ellipse ellipse = new Ellipse(); // I have an ellipse class

Test2(){

...
add(ellipse);
...

}

}

Is Eclipse a JPanel (or extends some Java Swing container component) or not. If not, then go with JPanel solution it will be somewhat treated as a component. If it is then first add a layout manager (BorderLayout perhaps) to JFrame and then add the panel to it may be in CENTER (make your own choice).

My approach for desktop application development has been to:

Add a layout manager to JFrame then add JPanels to JFrame based on the layout. This makes GUI more manageable and easy to update/change in future.

您可能总是只在BufferedImage绘制椭圆,将图像添加到ImageIcon并将图标添加到JLabel

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM