我正在制作GUI。用户以整数形式输入年份,并在GUI上显示该年份。但问题是我只想从用户输入4个整数。如果用户输入5个整数值,则显示JOptionPane
消息,请再次输入。但我不知道该怎么办。
代码:
public class A extends JFrame{
private JTextField tx;
private JLabel year;
private JButton bt;
private JLabel at;
public A(){
super("Frame");
getContentPane().setLayout(null);
tx= new JTextField();
tx.setBounds(150, 165, 128, 27);
getContentPane().add(tx);
year= new JLabel("Enter Year :");
year.setBounds(178, 133, 69, 27);
getContentPane().add(year);
at= new JLabel();
at.setFont(new Font("Tahoma", Font.PLAIN, 17));
at.setBounds(165, 295, 189, 27);
getContentPane().add(at);
bt= new JButton("Submit");
bt.setBounds(178, 203, 84, 27);
getContentPane().add(bt);
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String w = tx.getText();
int p = Integer.parseInt(w);
at.setText(""+p);
}
});
setSize(450,450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
}
主要
public class Main {
public static void main(String[] args) {
A obj = new A();
}
}
答案 0 :(得分:2)
以下是代码,供您快速查看,但请在将来尝试自行实施。
import java.awt.*;
import javax.swing.*;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.DocumentFilter.FilterBypass;
public class MyDocumentFilterExample {
private static final int MAX_CHARACTERS = 4;
private JTextField tField;
private void displayGUI () {
JFrame frame = new JFrame("Input Integer Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
tField = new JTextField(10);
((AbstractDocument)tField.getDocument()).setDocumentFilter(
new MyDocumentFilter ( MAX_CHARACTERS ));
contentPane.add(tField);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main ( String[] args ) {
Runnable runnable = new Runnable () {
@Override
public void run () {
new MyDocumentFilterExample ().displayGUI ();
}
};
EventQueue.invokeLater ( runnable );
}
}
class MyDocumentFilter extends DocumentFilter {
private int max_Characters;
private boolean DEBUG;
public MyDocumentFilter(int max_Chars) {
max_Characters = max_Chars;
DEBUG = false;
}
public void insertString(FilterBypass fb
, int offset
, String str
, AttributeSet a)
throws BadLocationException {
if (DEBUG) {
System.out.println("In DocumentSizeFilter's insertString method");
}
int length = fb.getDocument ().getLength () + str.length ();
if (length <= max_Characters && isValid ( str ) )
super.insertString(fb, offset, str, a);
else
Toolkit.getDefaultToolkit().beep();
}
public void replace(FilterBypass fb
, int offset, int length
, String str, AttributeSet a)
throws BadLocationException {
if (DEBUG) {
System.out.println("In DocumentSizeFilter's replace method");
}
int len = fb.getDocument ().getLength () + str.length ();
if (len - length <= max_Characters && isValid ( str ) )
super.replace(fb, offset, length, str, a);
else
Toolkit.getDefaultToolkit().beep();
}
private boolean isValid ( String string ) {
int len = string.length();
boolean isValidInteger = true;
for (int i = 0; i < len; i++)
{
if (!Character.isDigit(string.charAt(i)))
{
isValidInteger = false;
break;
}
}
return isValidInteger;
}
}
答案 1 :(得分:1)
只是不要让他们在第一时间输入超过4个字符
tx.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
if (tx.getText().length() >= 4) {
e.consume();
}
}
});
然后你也可以展开它以确保它们也只输入数字
tx.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
if (tx.getText().length() >= 4 || !Character.isDigit(e.getKeyChar())) {
e.consume();
}
}
});
答案 2 :(得分:1)
简而言之,您正在检查用户是否输入了有效年份。 在这种情况下,逻辑方法会好得多。在实际情况中,每个应用程序都有一定的用户年份范围。你可以这样做。
String w = tx.getText();
int p = Integer.parseInt(w);
int lower_limit = 1960;
int upper_limit = 2050;
if(p>lower_limit && p<uppper_limit){
at.setText(""+p);
}
但最好的方法是使用JSpinner。它符合目的。
注意:如果您真的想要所有4位数年份,请分别将upper_limit
和lower_limit
替换为10000
和999
。
答案 3 :(得分:0)
String w = tx.getText();
int p = Integer.parseInt(w);
String e=String.valueOf(w);
int m = e.length();
System.out.println(m);
if(e.length()==4){
at.setText(""+p);
}
else{
JOptionPane.showMessageDialog(null, "Wrong");
}
}
});
伙计们这件事有益于实践