【问题标题】:GridBagLayout is not positioning my JTextArea where I want it toGridBagLayout 没有将我的 JTextArea 定位在我想要的位置
【发布时间】:2014-11-22 16:54:45
【问题描述】:

我正在尝试使用GridBagLayoutJFrame 内放置一个JTextArea。我无法调整文本区域的大小,也无法根据我的gridxgridy 坐标放置它。在弄清楚缺少什么或我做错了什么时遇到了一些麻烦。

我正在尝试构建一个带有 GUI 界面的 ATM。文本区域将是我的屏幕。对 GUI 来说有点新,所以任何帮助都将不胜感激。

ATM.java

代表自动柜员机

    import java.awt.Color;
    import javax.swing.JFrame;
    import java.awt.FlowLayout;
    import java.awt.BorderLayout;
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
    import java.awt.Component;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    import javax.swing.JTextArea;
    import javax.swing.JButton;
    import java.awt.Container;

    //import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    public class ATM  extends JFrame 
    {  
        private JFrame atmContainer;
        private GridBagConstraints constraints;
        private GridBagLayout layout;
        private JTextArea atmScreen;
        private Container container;
        // constants corresponding to main menu options
        /*private static final int BALANCE_INQUIRY = 1;
        private static final int WITHDRAWAL = 2;
        private static final int DEPOSIT = 3;
        private static final int EXIT = 4;*/

        // no-argument ATM constructor initializes instance variables
        public ATM() 
        {
          atmContainer = new JFrame("ATM");
          atmScreen = new JTextArea(5,15);
          atmContainer.setLayout(new GridBagLayout());
          constraints = new GridBagConstraints();
          atmContainer.setSize(600, 400);
          atmContainer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //completely closes the      program
       
       
          atmScreen.setText("Welcome user");
          atmScreen.setEnabled(false);
          // atmScreen.setSize(100,100);
          constraints.gridx = 1;
          constraints.gridy = 0;
          atmContainer.add(atmScreen, constraints);
        
          atmContainer.setVisible(true); //makes atm visible
          atmContainer.setLocationRelativeTo(null);
       } 
   }

【问题讨论】:

  • 不使用 GridBagLayout 会为自己拯救一个充满伤害的世界。
  • The jTextArea will be my screen. - 然后只需使用 BorderLayout,它是框架的默认布局管理器。然后只需将文本区域添加到框架中,它将占用框架的所有可用空间。
  • 之所以会这样,是因为你不了解 Layout Managers 的工作原理。最好的办法是通过阅读Java Layout Manager Tutorial 了解什么是布局管理器以及它们是如何工作的,然后根据您的需要选择合适的布局管理器。之后,如果您认为没有适合您的工作的布局管理器,您必须选择:1)创建自己的布局管理器,或 2)使用Absolute Positioning
  • @hfontanez 强烈反对绝对定位。总有比手动操作更好的 LayoutManager。在极少数情况下,可能会实现自定义 LayoutManager。
  • 提供 GUI 布局的 ASCII 艺术或简单绘图。

标签: java swing layout-manager gridbaglayout


【解决方案1】:

鉴于您是 GUI 新手,我建议您使用 GUI 构建器;它们使 GUI 设计变得更加容易和高效。这个问题列出了几个选项:

Best GUI designer for eclipse?

另外,您使用 GridBagLayout 是否有原因?对于初学者来说,可能很难理解和使用。正如 camickr 所指出的,使用 BorderLayout 就可以了。

根据我的个人经验,首先了解 FlowLayout 对我来说更容易。它是所有布局中最简单的。您应该通过一些实验来获得一个很好的理解。

Java 教程也有丰富的课程;这是可能的布局管理器的可视化概览:

https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html#gridbag

这演示了如何使用 FlowLayout:

https://docs.oracle.com/javase/tutorial/uiswing/layout/flow.html

【讨论】:

  • 鉴于您是 GUI 新手,我建议您使用 GUI 构建器 有史以来最糟糕的建议。创建最丑陋的代码(和 UI)而不了解 GUI 实际工作方式的最佳方法。
猜你喜欢
  • 2022-11-29
  • 1970-01-01
  • 2022-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多