java - JPanel, instantiate an array of JButtons -
i having trouble putting panel class main class. can't seem instantiate , register buttons actionperformed method. project suppose use grid layout 9 input buttons (3 other buttons enter, space, , clear) display the input on jtextarea. believe have panel class set correctly, having problems putting jbutton array, , registering actionperformed method. pointers appreciated. (side question, how copy , paste code being taken in code block?)
import java.awt.dimension; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtextarea; public class textbuttons extends jframe implements actionlistener { private jbutton[] buttons; private jtextarea textarea; private final int enter; //index of enter button in buttons private final int space; //index of space button in buttons private final int clear; //index of clear button in buttons public textbuttons(string title) { super(title); jframe frame = new jframe("text button"); //todo: instantiate jbuttons, add them buttons array, // , register "this" actionlistener each button. (int = 0; < buttons.length; i++) { buttons[i] = new jbutton(); //this.buttons.addactionlistener(e); } enter = 9; space = 10; clear = 11; buttons[enter] = new jbutton("\n"); buttons[space] = new jbutton(" "); buttons[clear] = new jbutton("clear"); jtextarea textarea = new jtextarea(); textarea.seteditable(false); textbuttonspanel mainpanel = new textbuttonspanel(buttons, textarea); this.getcontentpane().add(mainpanel); this.pack(); this.setvisible(true); } /* (non-javadoc) * @see java.awt.event.actionlistener#actionperformed(java.awt.event.actionevent) */ @override public void actionperformed(actionevent e) { // ?? } public static void main(string[] args) { final textbuttons f = new textbuttons("text buttons"); f.setdefaultcloseoperation(jframe.exit_on_close); f.setlocationrelativeto(null); f.setvisible(true); }
panelclass
public class textbuttonspanel extends jpanel { public textbuttonspanel(jbutton[] buttons, jtextarea textarea) { int enter = 11; jpanel mainpanel = new jpanel(new gridlayout(4, 3)); jbutton b1 = new jbutton("a"); jbutton b2 = new jbutton("b"); jbutton b3 = new jbutton("c"); jbutton b4 = new jbutton("1"); jbutton b5 = new jbutton("2"); jbutton b6 = new jbutton("3"); jbutton b7 = new jbutton("x"); jbutton b8 = new jbutton("y"); jbutton b9 = new jbutton("z"); add(b1); mainpanel.add(b2); mainpanel.add(b3); mainpanel.add(b4); mainpanel.add(b5); mainpanel.add(b6); mainpanel.add(b7); mainpanel.add(b8); mainpanel.add(b9); jscrollpane scrollpane = new jscrollpane(textarea); scrollpane.setpreferredsize(new dimension(80, 120)); mainpanel.add(scrollpane); } } }
i can't seem instantiate , register buttons actionperformed method.
you organize code this:
import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; class textbuttonpanel extends jpanel implements actionlistener { public textbuttonpanel(string[] labels) { setlayout(new gridlayout(0, 3)); //same self.setlayout(..) jbutton button; (string label : labels) { button = new jbutton(label); button.addactionlistener(this); add(button); //same self.add(button) } } public void actionperformed(actionevent e) { jbutton button = (jbutton) e.getsource(); system.out.println(button.gettext()); } } class mygui { public mygui() { jframe frame = new jframe("name"); frame.setdefaultcloseoperation(jframe.dispose_on_close); frame.setbounds(200, 100, 500, 300); container cpane = frame.getcontentpane(); string[] labels = {"a", "b", "c", "d"}; textbuttonpanel panel = new textbuttonpanel(labels); cpane.add(panel); frame.setvisible(true); } } public class swingprog { private static void createandshowgui() { new mygui(); } public static void main(string[] args) { javax.swing.swingutilities.invokelater(new runnable() { public void run() { createandshowgui(); } }); } }
Comments
Post a Comment