Andrew
Andrew

Reputation: 283

\n Not Working For Linebreak -- Java

I am using the following code:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Finder extends JFrame
{
       JPanel jp = new JPanel();
       JLabel jl = new JLabel("Credits: Andrew / JellyBellyFred");
       JTextField jt = new JTextField("Enter A Player Name, Then Press 'Find Skin'!", 30);
       JButton jb = new JButton("Find Skin");
       JLabel lnbreak = new JLabel("\n");

       public Finder()
       {
              setTitle("Minecraft Skin Finder");
              setVisible(true);
              setSize(500, 200);
              setDefaultCloseOperation(EXIT_ON_CLOSE);
              setResizable(false);

              jp.add(jt);
              jp.add(lnbreak);
              jp.add(jb);
              jb.addActionListener(new ActionListener()
              {
                      public void actionPerformed(ActionEvent e)
                      {
                             String input = jt.getText();
                             jl.setText("http://minecraft.aggenkeech.com/player/" + input);
                      }
              });

              jp.add(jl);
              add(jp);

       }


       public static void main(String[] args)
       {
             Finder t = new Finder();
       }
}

To try to get a new window that will look like this:

[Input Box]

[Button]

--TEXT--

But for some reason it always turns out like this:

[Input Box] [Button]

--TEXT--

Upvotes: 0

Views: 1528

Answers (3)

Arun
Arun

Reputation: 2572

I'm not sure if the following works in GUI. But there is no harm in trying it out.

System.getProperty("line.separator")

Use the above code in place of "\n". In labels, I have tried few html tags which worked like a charm. Try this one and provide your feedback.

Upvotes: 1

Martin Perry
Martin Perry

Reputation: 9527

You cant use "linebreaker" in GUI. GUI are designed different way. If you want to output as suggested, use Layouts. You can put more panels inside each other to get desired effect, each with different layout.

Or second solution, but not sure if it works in Java, is to use absolute positioning. You set coordinates of "boxes" manually. But this is not really good solution.

Upvotes: 0

Mena
Mena

Reputation: 48404

Why don't you use layouts instead? This way you can organize your UI elements exactly how you want.

Upvotes: 1

Related Questions