Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble with static and non-static
    primarykey
    data
    text
    <p>I'm new at Java and following a beginners course right now. Very nice, i'm trying all kinds of stuff but now i'm stuck. This piece of code doesn't work. It should output the input in reverse order (by words).</p> <p>The code to flip it works in a piece of code i wrote without the GUI and now i'm trying to get it to work in a GUI with fixed buttons, labels etc. For that i've copied an example from the internet and trying to change it in such a way it'll work. But it doesn't seem to find the variables i use in actionPerformed and are setup in AddComponentsToPane. It has to do something with static and non-static, which i can't seem to get really clear</p> <p>Any help will greatly appreciated.</p> <p>Heres the code. </p> <pre><code>package flipit; import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.util.List; import java.util.*; //ArrayList; import javax.swing.*; public class FlipIt extends JFrame implements ActionListener { public static void addComponentsToPane(Container pane) { pane.setLayout(null); JLabel greetingLabel = new JLabel("Enter your array "); JTextField inField = new JTextField(); JTextField commentaryField = new JTextField(); JTextField strLen = new JTextField(); JButton button = new JButton("FlipIt"); pane.add(greetingLabel); pane.add(inField); pane.add(commentaryField); pane.add(button); Insets insets = pane.getInsets(); Dimension size = button.getPreferredSize(); greetingLabel.setBounds ( 5 + insets.left, 35 + insets.top,size.width + 40, size.height); inField.setBounds (120 + insets.left, 33 + insets.top,size.width + 200, size.height); //size = commentaryField.getPreferredSize(); commentaryField.setBounds(120 + insets.left, 80 + insets.top,size.width + 200, size.height); size = button.getPreferredSize(); button.setBounds ( 5 + insets.left, 80 + insets.top,size.width + 40, size.height); } private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("Reverse the input string."); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set up the content pane. addComponentsToPane(frame.getContentPane()); //Size and display the window. Insets insets = frame.getInsets(); frame.setSize(500 + insets.left + insets.right, 425 + insets.top + insets.bottom); frame.setVisible(true); } public static void main(String[] args) { Scanner input = new Scanner(System.in); //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } public void actionPerformed(ActionEvent event) { String InputStr = inField.getText(); int length = InputStr.length(); if (InputStr.compareTo("") == 0 || InputStr == null) { commentaryField.setText("Please enter some data, this array is empty"); } else { // Convert string variable to an ArrayList. List&lt;String&gt; list = new ArrayList&lt;String&gt;(Arrays.asList(InputStr.split(" "))); // Converting ArrayList to a String Array String [] InputList = list.toArray(new String[list.size()]); int i = 0 ; String ReverseOrder = "" ; // starting for loop to print the array in reversed order. for (i=InputList.length-1;i&gt;=0; i--) {ReverseOrder = ReverseOrder + " " + InputList[i] ; } // print result. commentaryField.setText(ReverseOrder); strLen.setText("Lengte string : "+ length); } } } </code></pre> <p>Thanks, Rob.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload