Then, I am creating a new canvas (JPNL)
class: canvas canvas = New canvas ();
And then I'm calling a method on that class: canvas.addTextBox ();
Now, from within the canvas class, I want to add a new jTextArea to the canvas I tried to use the code given below but it is not visible That's what's wrong with me. Thanks!
Class canvas extends JPNL (public canvas) {this.SsetOpaque (true); //this.setBackground(Color.WHITE); } Public Zero addTextBox () {Last JTextArea Comment TextArea = New JTextArea (10, 10); CommentTextArea.setLineWrap (right); CommentTextArea.setLineWrap (right); CommentTextArea.setWrapStyleWord (true); CommentTextArea.setVisible (true); }}
Full code
Import java.awt.Color; Import java.awt.event.ActionEvent; Import java.awt.event.ActionListener; Import java.awt.event.KeyEvent; Import javax.swing.JFrame; Import javax.swing.JenuBar; Import javax.swing.JMenuItem; Import javax.swing.JPanel; Import javax.swing.JTextArea; Public class UMLEditor {public static zero main (string [] args) {JFrame frame = new UML window (); Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Frame Setbound (30, 30, 1000, 700); . Frame.getContentPane () setBackground (Color.white); Frame.setVisible (true); Frame.setLocationRelativeTo (zero); Frame.setVisible (true); }} Category UMLWindow offers JFrame {canvas canvas = new canvas (); Private static last long serial VERSIONUID = 1 L; Public UMLWindow () {addMenus (); } Public Zero addMenus () {getContentPane (). Add (canvas); JMURbar menubar = new jmur (); Jenemitem newtextbox = new gemenuitem ("new text box"); NewTextBox.setMnemonic (KeyEvent.VK_E); NewTextBox.setToolTipText ("Exit the app"); NewTextBox.addActionListener (New Exhibitist) {@ Override Public Zero Action Perfid (Action Event Event) {canvas.addTextBox ();}}); Menubar.add (newTextBox); SetJMenuBar (Menubar); Set size (300, 200); SetLocationRelativeTo (zero); SetDefaultCloseOperation (EXIT_ON_CLOSE); }} Expands class canvas jepnell (public canvas) {this.SsetOpaque (true); //this.setBackground(Color.WHITE); } Public Zero addTextBox () {Last JTextArea Comment TextArea = New JTextArea (10, 10); CommentTextArea.setLineWrap (right); CommentTextArea.setLineWrap (right); CommentTextArea.setWrapStyleWord (true); CommentTextArea.setVisible (true); }}
addTextBox
method only one JTextArea
. It adds it to JPNL
and you must add the following line to the addTextBox
method:
< Add code> (commentartArea);
If your components are present in the JFrame
, then the screen is already visible when the addTextBox
method is called , You must invalidate the container as well just by adding it
Modify (); Repaint ();
Comments
Post a Comment