当前位置: 首页 > 面试题库 >

GUI:如何正确设置布局

唐繁
2023-03-14
问题内容

我在设置布局时遇到一些问题。(不要介意按钮的大小,我只想正确地了解布局)。

Here’s my code:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

class Grid extends JFrame
{
    private JTextField t1;
    private JTextField t2;
    private JCheckBox c1;
    private JCheckBox c2;
    private JButton b1;
    private JButton b2;
    private JButton b3;
    private JPanel ButtonPanel1;
    private JPanel ButtonPanel2;
    private JPanel ButtonPanel3;
    private JPanel CheckPanel1;
    private JPanel CheckPanel2;
    private JPanel TextPanel1;
    private JPanel TextPanel2;
    private JPanel EastPanel;
    private JPanel CenterPanel;
    private JPanel WestPanel;
    private JLabel l1;
    private JLabel l2;

    public Grid()
    {
        //CheckBoxes
         c1 = new JCheckBox("Snap to Grid");
         c2 = new JCheckBox("Show Grid");

         CheckPanel1 = new JPanel();
         CheckPanel1.add(c1);

         CheckPanel2 = new JPanel();
         CheckPanel2.add(c2);
         WestPanel = new JPanel(new GridLayout(2,1));
         WestPanel.add(c1);
         WestPanel.add(c2);
         add(WestPanel,BorderLayout.WEST);

          //I don't get how to arrange the labels aligned with the textfields.

         //TextFields
         l1 = new JLabel("X:");
         t1 = new JTextField("8",3);
         l2 = new JLabel("Y:");
         t2 = new JTextField("8",3);

         TextPanel1 = new JPanel();
         TextPanel1.add(l1);
         TextPanel1.add(t1);

         TextPanel2 = new JPanel();
         TextPanel2.add(l2);
         TextPanel2.add(t2);

         CenterPanel = new JPanel(new GridLayout(2,1));
         CenterPanel.add(l1);
         CenterPanel.add(l2);
         CenterPanel.add(t1);
         CenterPanel.add(t2);
         add(CenterPanel,BorderLayout.CENTER);

         // Buttons
         b1 = new JButton("Ok");
         b2 = new JButton("Cancel");
         b3 = new JButton("Help");

         ButtonPanel1 = new JPanel();
         ButtonPanel1.add(b1);

         ButtonPanel2 = new JPanel();
         ButtonPanel2.add(b2);

         ButtonPanel3 = new JPanel();
         ButtonPanel3.add(b3);

         EastPanel = new JPanel(new GridLayout(3,1));
         EastPanel.add(ButtonPanel1);
         EastPanel.add(ButtonPanel2);
         EastPanel.add(ButtonPanel3);

         add(EastPanel, BorderLayout.EAST);

    }
}

public class oottest {

    public static void main(String[] args) {

        Grid app = new Grid();

        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        app.setSize(300,150);
        app.setVisible(true);
    }

}

谁能帮助我了解布局的工作原理?

我一直在观看很多视频,并尝试过不同的布局,但仍然无法正确完成。告诉我是否使用了正确的布局,或者是否应该更改布局


问题答案:

GridBagLayout
该答案中所建议的那样,可以为左部分提供所需的全部控制。如果您愿意为简单起见而牺牲一些控制权,则可以FlowLayout在a里面放两个面板GridLayout

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

class Grid extends JFrame
{
    private JTextField t1, t2;
    private JCheckBox c1, c2;
    private JButton b1, b2, b3;
    private JPanel topPanel, bottomPanel;
    private JPanel eastPanel, centerPanel;
    private JLabel l1, l2;

    public Grid()
    {
         c1 = new JCheckBox("Snap to Grid");
         l1 = new JLabel("X:");
         t1 = new JTextField("8",3);

         topPanel = new JPanel();//uses flow layout by default
         topPanel.add(c1); topPanel.add(l1); topPanel.add(t1);

         c2 = new JCheckBox("Show Grid");
         l2 = new JLabel("Y:");
         t2 = new JTextField("8",3);

         bottomPanel = new JPanel();
         bottomPanel.add(c2);  bottomPanel.add(l2); bottomPanel.add(t2);

         centerPanel = new JPanel(new GridLayout(2,1));
         centerPanel.add(topPanel);
         centerPanel.add(bottomPanel);
         add(centerPanel,BorderLayout.CENTER);

         b1 = new JButton("Ok");
         b2 = new JButton("Cancel");
         b3 = new JButton("Help");

         eastPanel = new JPanel(new GridLayout(3,1));
         eastPanel.add(b1);
         eastPanel.add(b2);
         eastPanel.add(b3);
         add(eastPanel, BorderLayout.EAST);
    }

    public static void main(String[] args) {

        Grid app = new Grid();
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        app.setSize(300,150);
        app.setVisible(true);
    }
}


 类似资料:
  • 问题内容: 我正在尝试运行Java程序,但是它采用默认的GMT时区而不是OS定义的时区。我的JDK版本是1.5,操作系统是Windows Server Enterprise(2007) Windows指定了中央时区,但是当我运行以下程序时,它给了我GMT时间。 这是输出 请注意,我不想从应用程序中设置时区。我希望JVM使用的时区应该是操作系统中指定的时区。(对于具有JDK 1.4版和Microso

  • 问题内容: 我不时看到有关连接数据库的问题。 大多数答案不是我做的方式,否则我可能只是无法正确获得答案。无论如何; 我从未考虑过,因为我的工作方式对我有效。 但是这里有个疯狂的想法;也许我做错了所有,如果是这样的话;我真的很想知道如何使用PHP和PDO正确连接到MySQL数据库并使其易于访问。 这是我的做法: 首先,这是我的文件结构 (向下精简) : index.php 在最顶部,我有。 load

  • 问题内容: 我有一个关于mysql时区的怪异问题。 在我的网站配置文件中,我这一行设置了时区: 有趣的是,如果我在此之后添加另一行,例如: 执行该代码后,时间将正确显示。 但是,在其他一些查询中,我在表中插入行,这些表的列名为date,默认为CURRENT_TIMESTAMP。 这样插入行: (“会话”表的列默认为CURRENT_TIMESTAMP) 但是插入到数据库中的值仍指向服务器的时区:((

  • 问题内容: 遵循OnlyOffice帮助中心的说明,将创建由浏览器声明为无效的安全证书,因为它是自签名的。 目的是在适用于NextCloud的Docker上使用OnlyOffice的服务器,该服务器已在另一台服务器上正常运行。 当前,已在说明建议的目录中创建证书: 我已经按照所有给定的步骤进行操作,但是它不起作用。 有没有办法使用LetsEncrypt代替自签名证书? 我不是IT管理人员,我是一个

  • 我目前正在做一个游戏,你必须避免小行星。我开始使用任意单位。 此外,我正在使用一个: 在游戏屏幕中,小行星看起来变形了:

  • 我正在尝试使用改型2创建天气应用程序,现在我很难正确设置通话。 以下是有效的URL: 所以,我有我的API密钥和BASE URL是:http://api.openweathermap.org...这是我的Retrofit服务中的方法: 我得到的错误是: Java语言lang.IllegalArgumentException:URL查询字符串“q={city}/ 所以我试着这样做: 我得到了同样的错