大家好,我現在正在使用 java swing。我有一個這樣的問題:我有一段很長的文本并將它放在帶有標簽的<html>標簽中。
<html> My text </html>
如果文本太長,它會根據標簽的寬度換行。如何計算換行次數?或換行后顯示文本所需的高度
這是我的代碼
public Test() {
initComponents();
setPreferredSize(new Dimension(200, 200));
add(new JLabel("<html> "
"It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like)."
"</html>"));
}

如何知道顯示文本所需的確切高度???????
uj5u.com熱心網友回復:
Oracle 有一個有用的教程,
通常,您使用 aJTextArea表示很長的文本。您指定所需的行數和列數。通過調整行和列,您可以間接調整組件的大小。
我把它JTextArea放在 a 中JScrollPane,所以我的組件大小與文本的長度無關。我有時會在指令對話框中使用JTextAreain a JScrollPane。
這是完整的可運行代碼。
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class LongJLabel implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new LongJLabel());
}
@Override
public void run() {
JFrame frame = new JFrame("Long JLabel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
JTextArea textArea = new JTextArea(10, 40);
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setText("It is a long established fact that a reader will be "
"distracted by the readable content of a page when "
"looking at its layout. The point of using Lorem "
"Ipsum is that it has a more-or-less normal "
"distribution of letters, as opposed to using "
"'Content here, content here', making it look like "
"readable English. Many desktop publishing packages "
"and web page editors now use Lorem Ipsum as their "
"default model text, and a search for 'lorem ipsum' "
"will uncover many web sites still in their infancy. "
"Various versions have evolved over the years, "
"sometimes by accident, sometimes on purpose "
"(injected humour and the like).");
JScrollPane scrollPane = new JScrollPane(textArea);
textArea.setCaretPosition(0);
panel.add(scrollPane);
return panel;
}
}
uj5u.com熱心網友回復:
獲得所需的確切高度似乎并非易事。盡管您可以使用“有根據的猜測”方法來計算所需的高度。
String text = "Your text here";
JLabel label = new JLabel("<html> "
text
"</html>");
//You can get very useful Information via the Font Metrics class
FontMetrics fm = label.getFontMetrics(label.getFont());
//Here you need a reference to your window so you can get the width of it.
//Via the stringWidth function you get the width of the text when written as one line
//Subtracting 40 is to counter the effect, that the text is broken after each line
//The (int) (...) 1 is just to round up
int lines = (int) (fm.stringWidth(text) / (window.getWidth() - 40f)) 1;
//last you can set the window height however you like but watch out
//since the window top margin has to be minded
window.setSize(200, lines * fm.getHeight() 30);
uj5u.com熱心網友回復:
我查看了類的代碼javax.swing.JLabel并進行了一些實驗,發現當文本以JLabel開頭時<html>,設定了一個名為html的客戶端屬性,并且該屬性值的型別為javax.swing.text.View。
請注意,如果JLabel文本不是以 開頭,<html>則沒有html [client] 屬性。
View有getPreferredSpan回傳整個文本(的JLabel)的寬度和單行的高度的方法(取決于呼叫該方法時使用的引數——見下面的代碼)。
假設您希望分配的總寬度是任意的(在您問題的代碼中我相信寬度是 200),您可以計算需要多少行。從那里您可以計算出所需的高度。
我仍然沒有發現為什么計算出的行數比要求的行數小2,所以我在計算值上加了2。
另請注意,我設定了 的首選大小JLabel,而不是視窗(即JFrame),并且還呼叫pack了方法,以便視窗大小適合JLabel首選大小。
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.text.View;
public class Test {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
String text = "<html>It is a long established fact that a reader will be distracted "
"by the readable content of a page when looking at its layout. The "
"point of using Lorem Ipsum is that it has a more-or-less normal "
"distribution of letters, as opposed to using 'Content here, content "
"here', making it look like readable English. Many desktop publishing "
"packages and web page editors now use Lorem Ipsum as their default "
"model text, and a search for 'lorem ipsum' will uncover many web "
"sites still in their infancy. Various versions have evolved over the "
"years, sometimes by accident, sometimes on purpose (injected humour "
"and the like).";
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel(text);
View view = (View) label.getClientProperty("html");
float textWidth = view.getPreferredSpan(View.X_AXIS);
float charHeight = view.getPreferredSpan(View.Y_AXIS);
double lines = Math.ceil(textWidth / 200) 2;
double height = lines * charHeight;
label.setPreferredSize(new Dimension(200, (int) Math.ceil(height)));
frame.add(label);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
});
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/537321.html
標籤:爪哇摇摆文本标签高度
