score:1
the first thing is that you add both panels to window
, whose content pane has a borderlayout
.
so
window.add(universe);
window.add(ui;
is the same as
window.add(universe, borderlayout.center);
window.add(ui, borderlayout.center);
and both components get added to the same place (the buttons are probably hidden)
to avoid that, you may give the constraints indicating where in the borderlayout
you want to add your components, .eg :
window.add(universe, borderlayout.center);
window.add(ui, borderlayout.south);
the second thing is that you are using setsize()
, which won't be taken into account except if you have no layout manager .
you may use setpreferredsize
instead, its height
value will be honored by some layout manager, like borderlayout
for the south
or north
component . :
ui.setpreferredsize(new dimension(1000, 250));
now if you want it to adapt to the window's size, override the method instead :
jpanel ui = new jpanel() {
@override
public dimension getpreferredsize() {
return new dimension(window.getcontentpane().getsize().width, window.getcontentpane().getsize().height / 4);
}
};
putting it all together (as the preferred height of the south component is honored, no need in your case to give one to the center component, it will take the remaining space) :
//define main frame
final jframe window = new jframe("simulator");
window.setsize(1000, 1000);
window.setdefaultcloseoperation(jframe.exit_on_close);
window.setlocationrelativeto(null);
//define universe panel
jpanel universe = new jpanel();
universe.setbackground(color.black);
//define ui panel
jpanel ui = new jpanel() {
@override
public dimension getpreferredsize() {
return new dimension(window.getcontentpane().getsize().width, window.getcontentpane().getsize().height / 4);
}
};
//define buttons
jbutton femalespawnbutton = new jbutton("spawn female");
femalespawnbutton.setsize(250, 100);
jbutton malespawnbutton = new jbutton("spawn male");
malespawnbutton.setsize(250, 100);
//fill
ui.add(femalespawnbutton);
ui.add(malespawnbutton);
window.add(universe, borderlayout.center);
window.add(ui, borderlayout.south);
window.setvisible(true);
final important note :
despite this approach working in some cases, it should be discouraged to set sizes of the components by yourself (except for top level containers like windows) :
see : should i avoid the use of set(preferred|maximum|minimum)size methods in java swing?
score:0
i am sorry - currently i have not much time - but for now: its a problem with the layoutmanager. you should set one when initializing the jpanel. e.g. new jpanel(new gridlayout(1,2)); -> will work but is not nice ;) further information about layouts:
https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Source: stackoverflow.com
Related Query
- JButtons are shown outside of parent Panel (?)
- Maven jar dependencies are displayed outside `Maven Dependencies` view
- Open eclipse projects synced with git that are outside the workspace
- Imported Maven Project into Eclipse 3.7 (m2e 1.0) but src/main/java are not shown as source-package
- Installed JRE/JDKs are not shown on Eclipse Mars' Execution Environments
- Why are local variables not shown in debug view in eclipse?
- Eclipse RCP: Key binding for commands work, but shortcuts are not shown in menu
- When I use Mylyn with PyDev I see the context in the task but python files are not shown in any explorer
- Exporting Java Project from Eclipse, images are not shown
- Turkish characters are not shown properly on the HTML
- Changes of new Classes in Eclipse are not shown in the git repository
- NatTable Custom Spanning Data Provider, Cell will be only span if they are part of parent group
- All maven modules and submodules are shown in the root tree list
- Imported class files are shown under default package in Eclipse, how can i access them?
- JavaFX - Application: Only main stage is shown from exported jar file while in Eclipse all dialogs are shown, why this difference?
- Getting error in WindowBuilder in Eclipse because digits are being shown in different language
- Admob Ads are not shown at all
- Bitbucket code changes are not shown properly?
- Hadoop WordCount code, the following errors are shown
- Some tasks are not shown in the Tasks view in Eclipse (Spring Tool Suite)
- Two databases are shown when only one is created in Android using SQLite databases
- No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
- What are the pros and cons of the SVN plugins for Eclipse, Subclipse and Subversive?
- What are the best JVM settings for Eclipse?
- Several ports (8005, 8080, 8009) required by Tomcat Server at localhost are already in use
- Breakpoints are crossed out, how can I make them valid?
- Maven2: Missing artifact but jars are in place
- What vim plugins are available for Eclipse?
- The project cannot be built until the build path errors are resolved.
- What are the differences between plug-ins, features, and products in Eclipse RCP?
More Query from same tag
- Immediate project files synchronization for all users
- Can an Eclipse run configuration invoke Maven --resume-from on the selected project?
- Error to start in Apache Tomcat Server
- How do I show the rest of the errors in LogCat
- Adding existing local git repo - Zend studio
- How to recover Java file from Eclipse Project that damaged by getting power off?
- Implement build configurations in Java/Android under Eclipse
- Difference between src/folder and folder in Eclipse
- Exporting source of built plugins in PDE Build
- Spark 1.4.1 py4j.Py4JException: Method read([]) does not exist
- How to compile AndroidScreencast? (several errors)
- Using local json file in Android
- Can I specify a formatter for XML in Eclipse?
- Eclipse oxygen - package explorer empty
- How to create Renderscript scripts on Android Studio, and make them run?
- Restriction on J2SE inbuilt method
- Eclipse unable to load project properly in android?
- Enemy Spawning Android
- Eclipse,Hibernate tools, Is there any way to preview the sql equivalent query for criteria editor
- Strange behaviour extending abstract class (exercise)
- Eclipse ignores conditional breakpoint (java for hadoop)
- Folders mess up after import project into Eclipse
- Maven update only one project at a time
- Deploying a Alfresco application through Eclipse
- Smalltalk-style method browsing for eclipse Java?
- Eclipse keeps setting incorrect repository root
- How to activate Faces configuration editor in Eclipse?
- edit_text cannot be resolved or is not a field
- (Java Spring) NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found
- Error running Spring Boot application from command line