我是java新手,但我开始了解这门语言。我弄清楚了它是如何与我的 Javascript 背景一起工作的。我制作了一个带有文本标签的 JFrame。如果使用该程序,标签中的文本将会更长。所以我搜索了多行文本标签。如果您在标签中使用 HTML 标签,它将对其进行格式化,并且您将能够使用
标签。当我这样做时,程序的主体突然变空了。所以我放弃使用html并使用JTextArea的。同样的问题。整个 body 都空了。我不知道如何解决这个问题。我真的需要你的帮助。
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);
}
}
没有 html 的 JLabel 情况: 当我尝试在标签中使用 html 或 JTextArea 而不是 JLabel 时的情况。
发生这种情况时,不会有错误输出。 我希望你能帮助我。
最佳答案
JTextField 和 JLabel 组件不显示多行文本。您应该使用 JTextArea 来显示多行文本。请注意新行字符。每个操作系统都有自己的处理方式。最好的方法是首先创建一个表示特定操作系统的换行符的字符串常量。调用电话如下:
protected static final String NEWLINE = System.getParameter("line.separator");
然后在连接字符串时使用它。
示例:
JTextArea area = new JTextArea();
area.append("string1" + NEWLINE);
area.append("string2" + NEWLINE);
area.append("string3" + NEWLINE);
希望这有帮助。
关于java - 多行文本清空程序主体,jlabel 中的 html 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21189944/