2016-12-29 146 views
1

我想使我的JTextfield不可見和只讀,但該值必須顯示在窗口框架中。我在Eclipse中使用窗口生成器。如何使JTextField(Eclipse窗口生成器)只讀

JLabel lblLabel1 = new JLabel("Default DITA-OT File :"); 
lblLabel1.setBounds(10, 79, 123, 14); 
frmPdfPublisher.getContentPane().add(lblLabel1); 

JSeparator separator = new JSeparator(); 
separator.setBounds(10, 140, 414, 2); 
frmPdfPublisher.getContentPane().add(separator); 

JSeparator separator_1 = new JSeparator(); 
separator_1.setBounds(10, 257, 414, 2); 
frmPdfPublisher.getContentPane().add(separator_1); 

textField_1 = new JTextField(); 
textField_1.setBounds(138, 76, 286, 20); 
frmPdfPublisher.getContentPane().add(textField_1); 
textField_1.setColumns(10); 
textField_1.setVisible(false); 
+0

嘗試在該[線程]該溶液(http://stackoverflow.com/questions/3081713/how刪除邊框要麼 - 設置文本框不可見 - 框架內) –

+0

@DataPro我可以設置可見和不可見。但是會顯示任何價值?值顯示可見。例如: - 就像我們在右鍵單擊 - >屬性 - >文件位置中看到的一樣。 ? –

+0

1)爲了更好地提供幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 2)Java GUI必須在不同的語言環境中使用不同的PLAF來處理不同的操作系統,屏幕大小,屏幕分辨率等。因此,它們不利於像素的完美佈局。請使用佈局管理器或[它們的組合](http://stackoverflow.com/a/5630271/418556)以及[white space]的佈局填充和邊框(http://stackoverflow.com/a/17874718/ 418556)。 ..「 –

回答

2

如果你想讓它只讀JLable是更好的選擇做這項工作。

但是,如果要使用JTextfield而不是使文本字段成爲私有成員並且使用一種方法將文本字段設置爲隱藏。

class Example { 

private JTextField tf; 

public void hideTextField(){ 
    tf.setVisible(false); 
} } 

更改文本字段的顏色並使其與面板或框架的顏色相同。

setBackground(Color.white); 

而且通過重寫setBorder方法或通過使「空」,以

setBorder(BorderFactory.createLineBorder(Color.white)); 

or 

txt.setBorder(new LineBorder(Color.white,0)); 
+0

我可以做到這一點,但我想讓它在我的價值visibile,這是沒有發生。 –

+0

此代碼未顯示可見值。它也是無形的。 –

+0

@RohitGhosh,更改文本字段的背景顏色,並使其與Panel或Frame的顏色相同。 –