用Java做的 label

2010-02-07 6:53 pm
以下是我的程式:

package TryOut;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TryOut1 extends JFrame {
private static final long serialVersionUID = 1L;
private String QualityAsEntered = new String();
private JTextField QualityEntry = null; // Quality

private javax.swing.JPanel jContentPane = null;

public static void main(String[] args) {

TryOut1 frame = new TryOut1();
frame.setVisible(true);
}
// constructor
public TryOut1() {

super();
initialize();
}

private void initialize() {
this.setSize(350, 250);
this.setContentPane(getJContentPane());
this.setTitle("Customer Data Entry");
}

private JPanel getJContentPane() {
jContentPane = new JPanel();
jContentPane.setLayout(new java.awt.FlowLayout());
jContentPane.add(makeLabel(" Quality *", 100, 20));// Quality
jContentPane.add(getQualityEntry(), null);//Quality
return jContentPane;
}

// build labels
private JLabel makeLabel(String labelName, int width, int height) {
JLabel label = new JLabel();
label.setText(labelName);
label.setPreferredSize(new Dimension(width, height));
return label;
}

private JTextField getQualityEntry() {
QualityEntry = new JTextField();
QualityEntry.setPreferredSize(new Dimension(200, 20));
QualityEntry.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
QualityAsEntered = QualityEntry.getText().trim();
QualityEntry.setText(QualityAsEntered);
if (QualityAsEntered.length() != 0)
System.out.println(QualityAsEntered);
}
});
return QualityEntry;
}
}
它會要我打Quality, 我的最大問題是, 點才能把 Quality 要求只輸入數量(如 int), 不能輸入 String.
謝謝幫忙
更新1:

我嘗試過將 private String QualityAsEntered = new String(); 轉做 private Int QualityAsEntered = new int(); 但唔得... 我係java 初學者, 請指教

更新2:

To Ninja: 謝謝你的解答 那我把 private String QualityAsEntered = new String(); 轉做 private Int QualityAsEntered = new int(); 的想法是錯的嗎? 依你的想法, 我是不能限制 input 只能輸入數字, 但我可以用某方法 去check input 是否數字 .. 對嗎? 因為我是學 C++的, 請問check input 是否數字, 可否用 if loop.還有 check JTextField 是否類似 javax.swing.InputVerifier (聽說的) 謝謝你

回答 (1)

2010-02-08 7:17 am
✔ 最佳答案
private JTextField getQualityEntry() {
QualityEntry = new JTextField ();
QualityEntry.setPreferredSize(new Dimension(200, 20));
QualityEntry.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 此處加入一個check JTextField 中是否數字的method
QualityAsEntered = QualityEntry.getText().trim();
QualityEntry.setText(QualityAsEntered);
if (QualityAsEntered.length() != 0)
System.out.println(QualityAsEntered);
}
});
return QualityEntry;
}
你現出的程式會左一個JTextField 取得一個字串,再把字串放到你的private member QualityAsEntered 中。但你需要的是數定,所以你應加多一個method去check你輸入的資料是否數字,是的就繼續原有的程序,不是則要求重新輸入。最簡單的方法是check ASCII code:




十進制
十六進制
圖形

48
30
0

49
31
1

50
32
2

51
33
3

52
34
4

53
35
5

54
36
6

55
37
7

56
38
8

57
39
9
如果字串的ASCII是48-57便是數字。
希望幫到你...如再有疑問可再留言。

2010-02-08 11:44:31 補充:
這裡不能那我把
private String QualityAsEntered = new String();
轉做
private Int QualityAsEntered = new int();
是因為JTextField 一定return String。

2010-02-08 11:44:36 補充:
C++是用if去check字串中每個字母,如:
string text="AB12";
for(int i=0;text[i]!='\0';i++)
{
cout<<<"\t"; //把文字轉成ASCII
}
個result就會係65 66 49 50
所以Java都可以同類似方法去逐個check字母check。
而javax.swing.InputVerifier 應該只能成個字串check而不能逐個字母check。

2010-02-08 12:33:15 補充:
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/InputVerifier.html
你可以去參考一下


收錄日期: 2021-04-29 22:15:33
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20100207000051KK00453

檢視 Wayback Machine 備份