如何在java中指定文本字段的x和y坐标?

发布于 2024-09-14 12:49:42 字数 85 浏览 4 评论 0原文

我正在使用java制作一个计算器,必须为其指定文本字段的位置。我所知道的是使用边框布局指定位置,但不能在这里使用,因为必须指定其他按钮的位置..所以请帮助我

i'm making a calculator using java for which the position of textfield 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独夜无伴 2024-09-21 12:49:42

好吧,在 Swing 中,您通常不会通过 x,y 坐标来布局事物。相反,您可以使用布局管理器并使用 LayoutManager 提供的 API 将内容显示在屏幕上。 LayoutManager 在更高级别上运行,并提供灵活的 UI 等功能,并为您响应尺寸的变化。不同的布局管理器根据其布局模型对事物进行不同的布局。 BoxLayout vs. BorderLayout vs. SpringLayout 等。

就我个人而言,作为一个长期的 Swing 开发者,我认为最好的布局管理器是 TableLayout。它简单易学。它适用于网格系统(类似于 GridBagLayout,没有所有令人头疼的问题和陷阱)。它使用起来非常容易,并且生成的代码非常小。

只需 15 分钟即可上手,您可以在几分钟内布局最复杂的 UI。

https://tablelayout.dev.java.net/

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/

小帐篷 2024-09-21 12:49:42

如果将布局管理器设置为 null,则可以调用 JTextField.setBounds(int x, int y, int width, int height) 方法指定布局上的大小和位置帆布。

如果您的布局管理器为null,您必须以绝对坐标指定所有组件的位置。另请参阅组件 java 文档

另一种方法是使用更用户友好的布局管理器,例如 MiG 布局管理器,甚至是其他布局管理器和 JPanel 的。

编辑:您可以使用 GridLayout 和 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);

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);
吝吻 2024-09-21 12:49:42

我建议使用 GroupLayout。组合起来比较复杂——你必须了解它是如何工作的。我相信这种布局是为与视觉布局工具一起使用而设计的,但我发现自己在几乎构建的每个面板中都手动使用它 - 特别是具有常见表单元素的用户界面面板。对于具有这种布局的计算器应用程序来说,这似乎是完美的:

+---------------+
|    Display    |
+---+---+---+---+
| 7 | 8 | 9 | / |
+---+---+---+---+
| 4 | 5 | 6 | x |
+---+---+---+---+
| 1 | 2 | 3 | - |
+---+---+---+---+
| 0 | . | = | + |
+---+---+---+---+

这是构造函数:

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))));
}

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))));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文