Reputation: 179
Hi guys/girls i am trying to make a box which will take a user input of a number. Now i have made a text field but how do i change the size of the text feild.
This is what i got so far
public class example extends JFrame {
private JTextField example1;
example() {
box1 = new JTextField(10);
add(example1);
Now its just iam trying 2 get like a square size text feild which will allow the user to write a number in there. It will be a one diget number. Any help would be great. Thanks
Upvotes: 1
Views: 1214
Reputation: 285460
You state:
iam trying to make a caluclater. So i am using the box to hold a number.
Most calculators that I know of don't have a small field for displaying the current number but rather they have a text component that covers the entire width of the top of their GUI, and the key to achieving this with Java Swing is in using the right layout managers and the right placement of your components. For instance, this GUI design could be solved by using a container that uses BorderLayout, and placing your display JTextField (not JTextArea) in the BorderLayout.PAGE_START position and the buttons in another JPanel held in the main container's BorderLayout.CENTER position.
For example, please look at the code in my answer here. It creates a GUI that looks like so: . This shows two calculators that only differ based on the size of fonts chosen.
Key code for this is:
textField.setFont(textField.getFont().deriveFont(BTN_FONT_SIZE));
mainPanel.setLayout(new BorderLayout(gap, gap));
mainPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
mainPanel.add(textField, BorderLayout.PAGE_START);
mainPanel.add(buttonPanel, BorderLayout.CENTER);
Upvotes: 4
Reputation: 186
JTextField
uses the number of characters(# of columns) rather than a pixel size. If you're looking for a text box which you can specify the size of, use JTextArea
.
Upvotes: 1