使用BorderLayout的我的JPanel運行良好,直到我決定添加一個在其文本中有HTML的JLabel。我花了好幾個小時仔細檢查了有同樣問題的人,但沒有解決。當我評論處理這個JLabel的線時,一切正常。我嘗試使用JTextArea,但我有同樣的問題。JLabel中的HTML顯示不正確
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Gui extends JFrame implements KeyListener {
JFrame jFrame;
JPanel backgroundPanel;
JLabel multiLineTextLabel;
JLabel statusLabel;
int xValue = 1000;
Gui() {
jFrame = new JFrame("Frame Name");
jFrame.setSize(1200, 600);
jFrame.setLayout(new BorderLayout());
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
backgroundPanel = new JPanel();
backgroundPanel.setBackground(Color.BLACK);
String htmlString = "<html>Word1<br>Word2</html>";
multiLineTextLabel = new JLabel(htmlString);
statusLabel = new JLabel("Some Text");
jFrame.add(backgroundPanel, BorderLayout.CENTER);
jFrame.add(multiLineTextLabel, BorderLayout.EAST);
jFrame.add(statusLabel, BorderLayout.SOUTH);
jFrame.setFocusable(true);
jFrame.addKeyListener(this);
}
public void keyTyped(KeyEvent e)
{
//Won't be using
}
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_LEFT)
{
xValue -= 20;
statusLabel.setText("You are clicking left");
}
else if(keyCode == KeyEvent.VK_RIGHT)
{
xValue += 20;
statusLabel.setText("You are clicking right"); //You are "pressing" right
}
else if(keyCode == KeyEvent.VK_A)
{
statusLabel.setText("You are clicking A"); //You are "pressing" A
}
else if(keyCode == KeyEvent.VK_S)
{
statusLabel.setText("You are clicking S");
}
else if(keyCode == KeyEvent.VK_ESCAPE)
{
jFrame.setVisible(false);
jFrame.dispose();
}
else if(keyCode == KeyEvent.VK_P)
{
String positionString = new String("Your position/XValue = " + xValue);
statusLabel.setText(positionString);
}
}
public void keyReleased(KeyEvent e)
{
//Won't be using
}
public static void main(String[] args)
{
Gui go = new Gui();
}
}
不幸的是不能在JLabels裏面工作。 – user3750325 2014-08-31 22:31:22
什麼是私人組件controlKeyPanel;做什麼?你添加到你的佈局,但沒有像JPanel或任何其他任何對象?你是這個標籤multiLineTextLabel,但你沒有把它添加到你的佈局? – 2014-08-31 22:38:20
當我添加multiLineTextLabel到你的佈局,它完美的作品 – 2014-08-31 22:43:23