java - How to bring JLabels in GridBagConstrains at top -


i trying code, , reason, appears in middle. here's code:

        string errormsg = "something went wrong.";      final string title = "wall game";      this.setsize(400, 500); //sets screen     this.setdefaultcloseoperation(jframe.exit_on_close);     this.settitle(title);     this.setvisible(true);     this.setsize(401,501);     try {         font font1 = new font("comic sans ms", font.bold, 15);         jpanel panel1 = new jpanel(new gridbaglayout()); //makes panels         jlabel label1 = new jlabel("welcome wall game!"); //labels         jlabel label2 = new jlabel("click button read instructions!");         jbutton button1 = new jbutton("start");//buttons         button1.settext("start!");         label1.setfont(font1);         button1.setlayout(new boxlayout(button1, boxlayout.y_axis));          gridbagconstraints gbc = new gridbagconstraints();         gbc.insets = new insets(15,10,10,10);          gbc.gridx = 0;         gbc.gridy = 0;         panel1.add(label1, gbc);         gbc.gridx = 0;         gbc.gridy = 1;         panel1.add(label2, gbc);         gbc.gridx = 0;         gbc.gridy = 2;         panel1.add(button1, gbc);         this.add(panel1);     }     catch(exception e) {          joptionpane.showmessagedialog(null, "something went wrong. try restarting.", "sorry", joptionpane.information_message);     } } 

where text shows in middle. not starting top. here's mini example of like:

enter image description here

does know how make starting point top? can help. before try helping, try code first. you, need import "javax.swing.*" fyi.

thumbs if paint drawing did lol :d drew in window's paint.

read section swing tutorial on how use gridbag layout, section on weightx/y constraints.

you need have non-zero value set 1 of components otherwise components centered.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -