I'm making a GUI. In which user enter year in integers form and that year display on GUI. But the problem is i want to only enter 4 integers from user. If user enter 5 integers value then it show JOptionPane
message please type again. But i don't know how to do it.
Code:
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);
}
}
Main
public class Main {
public static void main(String[] args) {
A obj = new A();
}
}
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");
}
}
});
Guys does this thing is good for pratice