如何在JTextField中对齐文本?

时间:2016-09-09 15:40:46

标签: java jtextfield

这是我的代码:

try{
    Font font1=new Font("SansSerif",Font.ITALIC,20);
    JTextField levi=new JTextField(20);
    levi.setBounds(107,10,246,100);
    levi.setForeground(Color.black);
    levi.setHorizontalAlignment(SwingConstants.BOTTOM);
    levi.setFont(font1);
    levi.setBorder(nue);
    lin.add(levi);
} catch(Exception ex) {
    JOptionPane.showMessageDialog(null, ex);
}

当我运行该程序时,我得到了这个异常exception

1 个答案:

答案 0 :(得分:2)

如果您检查Sub firstCoord() Dim fpath As String, fname As String Dim dateCount As Integer, strDate As Date Dim i As Integer, j As Integer, k As Integer, lastRow As Integer, lastRow2 As Integer Dim ws As Worksheet, allws As Worksheet Dim seg As String Dim strNum As String Dim strRow As Integer lastRow = Sheet1.Range("A" & Sheet1.Rows.Count).End(xlUp).Row seg = Mid(ThisWorkbook.Name, 34, 1) With Application.WorksheetFunction For i = 2 To lastRow fpath = "_______\" strDate = Sheet1.Range("B" & i) strNum = seg & Format(Mid(Sheet1.Range("A" & i), 4, 3), "000") & "000" dateCount = 0 Do While Len(Dir(fpath & "_____-" & Format(strDate - dateCount, "ddmmmyyyy") & ".xlsx")) = 0 And dateCount < 35 dateCount = dateCount + 1 Loop fname = "____-" & Format(strDate - dateCount, "ddmmmyyyy") & ".xlsx" Workbooks.Open (fpath & fname) For Each ws In Workbooks(fname).Worksheets If ws.Name Like "*all*" Then Set allws = Workbooks(fname).Worksheets(ws.Name) ws.Activate End If Next ws lastRow2 = ActiveSheet.Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row ThisWorkbook.Activate k = 1 Do While (.CountIf(Sheet1.Range("C" & i & ":" & "E" & i), "") <> 0 Or Sheet1.Range("F" & i) = "") And k <= lastRow2 If Left(allws.Range("A" & k), 7) = strNum Then Sheet1.Range("C" & i) = allws.Range("D" & k) Sheet1.Range("D" & i) = allws.Range("C" & k) Sheet1.Range("E" & i) = allws.Range("E" & k) ElseIf k = lastRow2 And Sheet1.Range("C" & i) = "" Then Sheet1.Range("F" & i) = "Not Found" End If k = k + 1 Loop Workbooks(fname).Close Next i End With End Sub JTextField,则setHorizontalAlignment()方法的唯一有效密钥为:

JTextField.LEFT
JTextField.CENTER
JTextField.RIGHT
JTextField.LEADING
JTextField.TRAILING 

传入SwingConstants.BOTTOM会抛出异常。