how to specify x and y co-ordinate of textfield in java?
i'm making a calculator using java for which the position of tex开发者_如何学运维tfield has to be specified. all i know is to specify the position using border layout but that can't be used here as the position of other buttons have to be specified.. so kindly help me
Ok so in Swing you typically don't layout things by their x,y coordinates. Instead you use a layout manager and use the API provided by the LayoutManager to put things on the screen. LayoutManager operate at a higher-level and provide things like flexible UIs and respond to changes in size for you. Different layout managers layout things differently depending on their model of layout. BoxLayout vs. BorderLayout vs. SpringLayout, etc.
Personally, as a long time Swing developer, I think the best layout manager is TableLayout. It's simple and straightforward to learn. It works on the grid system (similar to GridBagLayout without all of the headaches and gotchas). It's vasty easier to use and produces very small code.
It only takes 15 minutes to pick up and you can layout most complex UIs in a matter of minutes.
https://tablelayout.dev.java.net/
If you set the layout manager to null
you can call the JTextField.setBounds(int x, int y, int width, int height)
method specifying the size and position on the canvas.
If your layout manager is null
you'll have to specify all the components positions in absolute coordinates. See also Component java doc.
An alternative is to use a more user-friendly layout manager such as MiG Layout Manager, or even a combination of the other layout managers and JPanel's.
EDIT: You can use a GridLayout and BorderLayout:
JFrame frame = // the frame your adding to, could also be any other component
JTextField textField ? // your textfield
JPanel wrapperPanel = new JPanel(new BorderLayout());
JPanel grid = new JPanel(new GridLayout(4,4));
// Make the number grid
wrapperPanel.add(textField, BorderLayout.PAGE_START);
wrapperPanel.add(grid, BorderLayout.CENTER);
I would suggest GroupLayout. It is more complicated to put together - you have to understand how it works. I believe this layout was designed to work with visual layout tools, but I find myself using it manually in practically every panel I build -- especially user interface panels with common form elements. It would seem perfect for a calculator application with this layout:
+---------------+ | Display | +---+---+---+---+ | 7 | 8 | 9 | / | +---+---+---+---+ | 4 | 5 | 6 | x | +---+---+---+---+ | 1 | 2 | 3 | - | +---+---+---+---+ | 0 | . | = | + | +---+---+---+---+
Here is the constructor:
public CalculatorPanel() { GroupLayout layout = new GroupLayout(this); layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); this.setLayout(layout); layout.setVerticalGroup(layout.createSequentialGroup() .addComponent(this.displayTextField) .addGroup(layout.createParallelGroup() .addComponent(this.sevenButton) .addComponent(this.eightButton) .addComponent(this.nineButton) .addComponent(this.divideButton)) .addGroup(layout.createParallelGroup() .addComponent(this.fourButton) .addComponent(this.fiveButton) .addComponent(this.sixButton) .addComponent(this.multiplyButton)) .addGroup(layout.createParallelGroup() .addComponent(this.oneButton) .addComponent(this.twoButton) .addComponent(this.threeButton) .addComponent(this.minusButton)) .addGroup(layout.createParallelGroup() .addComponent(this.zeroButton) .addComponent(this.decimalButton) .addComponent(this.equalsButton) .addComponent(this.plusButton))); layout.setHorizontalGroup(layout.createParallelGroup() .addComponent(this.displayTextField) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup() .addComponent(this.sevenButton) .addComponent(this.fourButton) .addComponent(this.oneButton) .addComponent(this.zeroButton)) .addGroup(layout.createParallelGroup() .addComponent(this.eightButton) .addComponent(this.fiveButton) .addComponent(this.twoButton) .addComponent(this.decimalButton)) .addGroup(layout.createParallelGroup() .addComponent(this.nineButton) .addComponent(this.sixButton) .addComponent(this.threeButton) .addComponent(this.equalsButton)) .addGroup(layout.createParallelGroup() .addComponent(this.divideButton) .addComponent(this.multiplyButton) .addComponent(this.minusButton) .addComponent(this.plusButton)))); }
精彩评论