Re: How to creat a jButton
What have you tried? Break the problem down...how would you do it for 1 button, then work off of that with a loop up to the number you want.
Re: How to creat a jButton
it's easy do to it for a single jButton, but how may i initiate the index of the jButton. for example, i want to write "45" as a text for jButton1 and "94" for jButton2 and so on. how may i address the index of jButtons in a "for" loop to place these numbers as their texts?
Re: How to creat a jButton
Like I said, break the problem down. First, how would you store those values (array)? Second, how would you retrieve the values from the user? Next, how would you loop through that array and create the JButtons? Try to answer these questions, and post what you have so far.
Re: How to creat a jButton
See, i want to ask the user to enter a number; eg. 5. and then i have to create 5 jButtons in the GUI form of Java. How may i create a jButton in the GUI form and get the numbers one by one from the user and place them as jButton's text. I work with NetBeans IDE 6.8 in "Java Desktop Application" part. The following is the code i expect to write
Code :
int i, k;
i = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of jButtons you need"));
for ( int j = 0; j < 5; j++ ) {
//Now, i want to create 5 jButtons
k = Integer.parseInt(JOptionPane.showInputDialog("Enter the numbers you want to set as the jButton"+ i + "'s text"));
//Now, numbers the user enters must be set as the ith jButton's text!
}
Re: How to creat a jButton
You are pretty close. All you need is to create the JButton based upon user input, then add that button to the user interface.
Code :
i = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of jButtons you need"));
for ( int j = 0; j < 5; j++ ) {
//Now, i want to create 5 jButtons
String k = JOptionPane.showInputDialog("Enter the numbers you want to set as the jButton"+ i + "'s text");//no direct need to parse the int
JButton button = new JButton(k);
//add the button to the GUI
//Now, numbers the user enters must be set as the ith jButton's text!
}
If you want the button to do something, you will also need to add a Listener to the button. If you are unfamiliar with adding components to a GUI and/or using swing component Listeners, I recommend reading the online tutorials at Sun (such as How to use Buttons). GUI builders are convenient, but IMO one should know the fundamentals of arranging a GUI and actions associated with that GUI.