[英]Is there any way to display text without JLabel or JTextField in java swing?
我正在創建Swing項目,但我想知道是否有任何方法可以在不使用JLabel
或JTextField
情況下打印文本。
我發現文本字段可能是一個,但它不像JLabel
那樣整潔。 (文本周圍有邊框)。
可以刪除文本字段中的邊框嗎? 或者我可以使用其他方法嗎?
您似乎在尋找JComponent.setBorder(null)
。 然而,有很多方法可以達到你想要的效果..
JLabel
,BG顏色。 以下是兩個標准組件的示例。
import java.awt.*;
import javax.swing.*;
class TextFieldNoBorder {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
String s = "Text without borders..";
JPanel gui = new JPanel(new GridLayout(0,1,5,5));
JTextField tf = new JTextField(s, 20);
tf.setBorder(null);
gui.add(tf);
JLabel l = new JLabel(s);
l.setOpaque(true);
l.setBackground(Color.YELLOW);
gui.add(l);
JOptionPane.showMessageDialog(null, gui);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);
}
}
“我想知道是否有辦法在不使用JLable或JtextField的情況下打印文本”
您始終可以在JPanel中繪制文本。 需要做更多的工作,但你可以用你的文字做更多的事情。
public class TextPanel extends JPanel{
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawString("Hello, Stackoverflow!", xLocation, yLocation);
}
}
請參見使用圖形繪制文本
你可以用這個!
JTextField textField = new JTextField();
textField.setBorder(javax.swing.BorderFactory.createEmptyBorder());
這會將空邊框設置為TextField
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.