swing - Java GUI is launching but nothing appears, no errors in stack trace -
i'm trying play around actionlisteners , inner classes through simple gui user press button cause message displayed.
my code compiles - however, upon launch, icon appear in tray (osx), immediate terminate. no errors appear in console.
here code:
import javax.swing.*; import java.util.*; import java.awt.*; import java.awt.event.*; public class gui { private jframe myframe; private jpanel messagepanel; private jpanel buttonpanel; private jlabel mymessage; public gui() { myframe = new jframe("my gui"); messagepanel = new jpanel(); buttonpanel = new jpanel(); mymessage = new jlabel(); myframe.setlayout(new borderlayout()); myframe.setsize(500, 500); myframe.add(messagepanel, borderlayout.south); myframe.add(buttonpanel, borderlayout.center); messagepanel.add(mymessage); addbuttons(); } public void addbuttons() { jbutton button1 = new jbutton(new imageicon("circle.png")); buttonpanel.add(button1); button1.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { mymessage.settext("button pressed!"); } }); } public static void main(string[] args) { gui mygui = new gui(); } } what's going on here?
you need create jframe, myframe, visible via myframe.setvisible(true);
also, should not run swing gui related code outside event dispatch thread (edt). right way start programme be:
public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { seek { gui mygui = new gui(); } grab (exception e) { e.printstacktrace(); } } }); } java swing user-interface
No comments:
Post a Comment