i have jpanel in class , want run when click "enter" button. reason wont that, tried write simple system.out.println in code , works when want run jpanel, wont run it, know problem is?
import inf45.spring2010.examples.gui3.filechooser; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class clientgui extends jframe { /** * variables */ private jlabel namelabel; private jlabel iplabel; private jlabel portlabel; private jtextfield nametext; private jtextfield iptext; private jtextfield porttext; private jbutton enterbutton; private jbutton cancelbutton; /** * constructor frame */ public clientgui () { jframe frame = new jframe (); frame.setdefaultcloseoperation(windowconstants.dispose_on_close); buildui(); } private void buildui(){ // setup gridbaglayout gridbaglayout layout = new gridbaglayout(); getcontentpane().setlayout(layout); /** * set jlabels */ namelabel = new jlabel ("name:"); getcontentpane().add(namelabel); layout.setconstraints( namelabel, new gridbagconstraints( 0, 1, 2, 1, 1.0, 1.0, gridbagconstraints.center, gridbagconstraints.both, new insets(5, 10, 5, 10), 0, 0)); iplabel = new jlabel ("ip address:"); getcontentpane().add(iplabel); layout.setconstraints( iplabel, new gridbagconstraints( 0, 2, 2, 1, 1.0, 1.0, gridbagconstraints.center, gridbagconstraints.both, new insets(5, 10, 5, 10), 0, 0)); portlabel = new jlabel("port:"); getcontentpane().add(portlabel); layout.setconstraints( portlabel, new gridbagconstraints( 0, 3, 2, 1, 1.0, 1.0, gridbagconstraints.center, gridbagconstraints.both, new insets(5, 10, 5, 10), 0, 0)); /** * setup jtextfields */ nametext = new jtextfield (15); getcontentpane().add(nametext); layout.setconstraints( nametext, new gridbagconstraints( 2, 1, 2, 1, 1.0, 1.0, gridbagconstraints.center, gridbagconstraints.both, new insets(5, 10, 5, 10), 0, 0)); iptext = new jtextfield (15); getcontentpane().add(iptext); layout.setconstraints( iptext, new gridbagconstraints( 2, 2, 2, 1, 1.0, 1.0, gridbagconstraints.center, gridbagconstraints.both, new insets(5, 10, 5, 10), 0, 0)); porttext = new jtextfield(15); getcontentpane().add(porttext); layout.setconstraints( porttext, new gridbagconstraints( 2, 3, 2, 1, 1.0, 1.0, gridbagconstraints.center, gridbagconstraints.both, new insets(5, 10, 5, 10), 0, 0)); /** * setup jbuttons */ enterbutton = new jbutton ("enter"); getcontentpane().add(enterbutton); layout.setconstraints( enterbutton, new gridbagconstraints( 0, 4, 2, 1, 1.0, 1.0, gridbagconstraints.center, gridbagconstraints.both, new insets(5, 10, 5, 10), 0, 0)); //action listener enter button prompts user send gui enterbutton.addmouselistener(new mouseadapter() { public void mousepressed(mouseevent e) { filechooser enterfile = new filechooser(); enterfile.setvisible(true); enterfile.setsize(150,150); system.out.println("osama"); } }); cancelbutton = new jbutton ("cancel"); getcontentpane().add(cancelbutton); layout.setconstraints( cancelbutton, new gridbagconstraints( 2, 4, 2, 1, 1.0, 1.0, gridbagconstraints.center, gridbagconstraints.both, new insets(5, 10, 5, 10), 0, 0)); //actionlistener cancel button should clear fields. cancelbutton.addactionlistener( new actionlistener() { public void actionperformed(actionevent e) { //cancelpressed(); } }); } }
import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class filechooser extends jpanel implements actionlistener { static private final string newline = "\n"; jbutton openbutton, savebutton; jtextarea log; jfilechooser fc; public filechooser() { super(new borderlayout()); //create log first, because action listeners //need refer it. log = new jtextarea(5,20); log.setmargin(new insets(5,5,5,5)); log.seteditable(false); jscrollpane logscrollpane = new jscrollpane(log); //create file chooser fc = new jfilechooser(); openbutton = new jbutton("select file..."); openbutton.addactionlistener(this); //create save button. use image jlf //graphics repository (but extracted jar). savebutton = new jbutton("send selected file"); savebutton.addactionlistener(this); //for layout purposes, put buttons in separate panel jpanel buttonpanel = new jpanel(); //use flowlayout buttonpanel.add(openbutton); // buttonpanel.add(savebutton); //add buttons , log panel. add(buttonpanel, borderlayout.page_start); add(logscrollpane, borderlayout.center); } public void actionperformed(actionevent e) { //handle send button action. if (e.getsource() == openbutton) { int returnval = fc.showopendialog(filechooser.this); if (returnval == jfilechooser.approve_option) { file file = fc.getselectedfile(); //this real application open file. log.append("selected: " + file.getname() + "." + newline); system.out.println("sent " + file.getname()); } else { log.append("open command cancelled user." + newline); system.out.println("file selection canceled."); } log.setcaretposition(log.getdocument().getlength()); } } /** returns imageicon, or null if path invalid. */ protected static imageicon createimageicon(string path) { java.net.url imgurl = filechooser.class.getresource(path); if (imgurl != null) { return new imageicon(imgurl); } else { system.err.println("couldn't find file: " + path); return null; } } /** * create gui , show it. thread safety, * method should invoked * event dispatch thread. */ public static void createandshowfilechooser() { //create , set window. jframe frame = new jframe("filechooserdemo"); frame.setdefaultcloseoperation(jframe.exit_on_close); //add content window. frame.add(new filechooser()); //display window. frame.pack(); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { //turn off metal's use of bold fonts uimanager.put("swing.boldmetal", boolean.false); //displays , creates file chooser createandshowfilechooser(); } }); } }
public class mainclient { /** * @param args */ public static void main(string[] args) { // todo auto-generated method stub clientgui client = new clientgui(); client.settitle("client gui"); client.setvisible(true); client.setsize(200,200); } }
a jpanel
cannot displayed on own, has inside jframe
. make filechooser
extend jframe
(and add what's needed), or add new panel (enterfile) current frame (whatever fit needs).
Comments
Post a Comment