java - Learning Swing and Jframe and stuff - also would like some tips and tricks on organization -
I'm learning swings for my Java program. I am very new on this and I'm slowly hanging it up.
My code is very basic here; I am using an action listener who is reading a text recorded in a JTextField, but my code is giving me an error which is "Credit Graphics. Java 32: Error: Non-static Variable Can not be referred to by a static reference text .addActionListener (this) ", I can not find a lot of useful solution on the forum, at least there is no solution which fixes my style of coding. It would error Why am, so I can save it for future reference?
In addition to this, I like any organizational suggestions that you want people to give me.
Trying to create a graphic application that can take the credit card application through JETextfield and then runs through my pre-built program that checks the validity of the card.
Thank you!
Here's my code:
import java.awt. *; Import java.awt.event. *; Import javax.swing *; Import java.awt.event.ActionEvent; Import java.awt.event.ActionListener; // If the text is entered, then incorrectly reset the textfield ("") by the text field; Public class credit graphics ActionListener {public static String cardNum applies; Public stable JFrame frame; Public static zeppel panel; Public static JLabel label; Public static JTextField text; Public static zero main (string [] args) {frame = new JFrame ("HI"); Panel = new zpn (); Label = new JLabel ("Welcome to MES Banking!"); Text = new JTextField (); Panel.add (label); Panel.add (text); Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Panel.setPreferredSize (new dimension (500,500)); . Frame.getContentPane () add (panel); Frame.pack (); Frame.setVisible (true); Text.addActionListener (this); } Public Zero Impact (Action EVE) {cardNum = text.getText (); }}
main problem:
< P> this
class means, is not static
, and it can not be since it is the main square. Therefore it can not be accessed with a static context, which is the main
method do not write your code in the main
method Have to face a lot in Constructor, or build your UI in an init method. Then just call that method or the constructor in
other points:
-
Your swing Play Apps on Events Dispatch Thread (ADT) See more (See also for example below).
-
Personally, I can not implement the class implementer. I will be an example of a listener for the button (see below)
< / Ul>
UPDATE
Here is a reactor with numbers
import java.awt.Dimension; Import java.awt.event.ActionEvent; Import java.awt.event.ActionListener; Import javax.swing.JFrame; Import javax.swing.JLabel; Import javax.swing.JPanel; Import javax.swing.JTextField; Import javax.swing.SwingUtilities; Public category credit graphics {public string cardnom; Public jeffrey frame; Public JP panel; Public JLabel label; Public JTextField text; Public Credit Graphics () {frames = new Gefram ("HI"); Panel = new zpn (); Label = new JLabel ("Welcome to MES Banking!"); Text = new JTextField (); Panel.add (label); Panel.add (text); Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Panel.setPreferredSize (new dimension (500, 500)); . Frame.getContentPane () add (panel); Frame.pack (); Frame.setVisible (true); Text.addActionListener (new executable () {public zero functioning (action name e) {cardNum = text.getText ();}}); } Public Static Zero Main (string [] Args) {SwingUtilities.invokeLater (New Runnabal) (Public Zero Run) {New Credit Graphics ();}}); }}
Comments
Post a Comment