Reputation: 3272
I'm new to java but I am beginning to understand the language. I figured out how it works with my Javascript background. I made a JFrame with labels for text. The text in the labels will be longer if the program is used. So I searched for multiline textlabels. If you use HTML tags in the label it will format it and you'll be able to use
tags. When I did this the body of the program was suddenly empty. So I gave up using html and used JTextArea's. Same problem. The full body went empty. I don't know how to fix this. I really need your help.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import org.w3c.dom.Document;
public class MainMenu extends JFrame{
public JPanel header = new JPanel();
public JPanel body = new JPanel();
public JPanel bodyRight = new JPanel(new FlowLayout(FlowLayout.LEFT));
public JPanel bodyLeft = new JPanel();
public JPanel bodyLeftC = new JPanel(new GridBagLayout());
public JLabel logo = new JLabel();
public JLabel context = new JLabel();
public JLabel vraaglabel = new JLabel();
public JLabel vraag = new JLabel();
public JLabel uitwerking = new JLabel();
public JLabel contentLeft = new JLabel();
public JLabel goedfout = new JLabel();
public JTextField inputText = new JTextField();
public JButton submit = new JButton();
public void startMenu(Document doc)
{
setTitle("myTitle");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
setSize(760,540);
setResizable(false);
setVisible(true);
header.setBounds(0,0,760,50);
header.setBackground(new Color(65,155, 217));
add(header);
body.setBounds(0,50,760,490);
body.setLayout(null);
body.setBackground(Color.WHITE);
add(body);
logo.setText("myTitle");
logo.setForeground(Color.WHITE);
logo.setFont(new Font(Font.SANS_SERIF, 1, 35));
header.add(logo);
bodyRight.setBackground(Color.white);
bodyRight.setBounds(10,10,370,440);
bodyLeft.setBackground(Color.white);
bodyLeft.setBounds(370,10,370,440);
bodyLeft.setBorder(new EmptyBorder(10, 10, 10, 10) );
body.add(bodyRight);
body.add(bodyLeft);
bodyLeft.add(bodyLeftC);
//<<<<<<<<<<<<<<<<<<--REAL-CONTENT-->>>>>>>>>>>>>>>>>>>>>>>>>
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.WEST;
c.insets = new Insets(2,2,2,2);
c.gridx = 0;
c.gridy = 1;
context.setText("Here is some text.");
context.setFont(new Font(Font.SANS_SERIF, 4, 13));
bodyLeftC.add(context, c);
c.gridx = 0;
c.gridy = 2;
vraaglabel.setBorder(new EmptyBorder(15, 0, 0, 0) );
vraaglabel.setText("Question:");
bodyLeftC.add(vraaglabel, c);
c.gridx = 0;
c.gridy = 3;
vraag.setText("This is a question?");
vraag.setFont(new Font(Font.SANS_SERIF, 4, 13));
bodyLeftC.add(vraag, c);
c.gridx = 0;
c.gridy = 4;
inputText.setColumns(13);
bodyLeftC.add(inputText, c);
c.gridx = 0;
c.gridy = 5;
submit.setText("Ok");
bodyLeftC.add(submit, c);
c.gridx = 0;
c.gridy = 6;
goedfout.setText("Well done!");
goedfout.setBorder(new EmptyBorder(10, 0, 0, 0) );
bodyLeftC.add(goedfout, c);
c.gridx = 0;
c.gridy = 7;
uitwerking.setText("More text");
uitwerking.setFont(new Font(Font.SANS_SERIF, 4, 13));
uitwerking.setBorder(new EmptyBorder(10, 0, 0, 0) );
bodyLeftC.add(uitwerking, c);
}
}
Situation with JLabel without html:
Situation when I try using html in labels or a JTextArea instead of a JLabel.
When this happens there is no error output. I hope you can help me.
Upvotes: 1
Views: 262
Reputation: 4347
A JLabel will use html tags if you enclose the text within <html> and </html> tags. For example:
JLabel label = new JLabel("<html> first line <br> second line </html>");
The label will display as:
first line
second line
JTextField is designed for single-line text input, so no multi-line text there.
Upvotes: 0
Reputation: 822
The JTextField and JLabel components do not display multiple lines of text. You should use a JTextArea to display multiple lines of text instead. Be careful about the new line character. Each operating system has their own way of dealing with it. The best way is to first create a String constant that represents the newline character for the specific operating system. Make a call like:
protected static final String NEWLINE = System.getParameter("line.separator");
Then use it when concatenating your Strings.
Example:
JTextArea area = new JTextArea();
area.append("string1" + NEWLINE);
area.append("string2" + NEWLINE);
area.append("string3" + NEWLINE);
Hope this helps.
Upvotes: 1