当前位置: 首页 > 知识库问答 >
问题:

在主面板/框架中定位面板和部件

卫博雅
2023-03-14

我必须创建一个表单,它具有诸如用户名、密码和其他东西的输入值。这里有一个图片的链接,http://img196.imageshack.us/img196/1727/empdesign.png

我希望所有这些属性都是联盟到屏幕的边缘,而不是居中。我尝试使用jComponenet.setHorizontalAlignment(swingConstants.left)方法,但这也没有任何作用。我在JLabels和JPanels上尝试了这种方法,但都没有效果。

下面是我的代码:

public class tada extends GUIDesign{

  //making all the jlabels to be placed

JLabel usernameLabel = new JLabel("username");
JLabel passwordLabel = new JLabel("Name");
JLabel empIDLabel = new JLabel("empID");
JLabel SalaryLabel = new JLabel("Salary");
JLabel EmployerLabel = new JLabel(" Salary");
JLabel hoursLabel = new JLabel("Gender");

JLabel departmentLabel = new JLabel("Department");



 //making all the textfields, combo boxes, and etc. 
JTextField usernameField = new JTextField(20);
JTextField passwordField = new JTextField(20);
JTextField empIDField = new JTextField(20);


JTextField SalaryField = new JTextField(20);
JTextField EmployerField = new JTextField(20);
String[] hourss = {"Fall", "Spring", "Summer"};
JComboBox hoursBox = new JComboBox(hourss);

JCheckBox[] departmentCheckBoxesBoxs = {new JCheckBox("Department 1"), 
                                        new JCheckBox("Department 2"), 
                                        new JCheckBox("Department 3"), 
                                        new JCheckBox("Department 4"), 
                                        new JCheckBox("Department 5")};






String[] Salary = {"section", "combo", "box"};
JComboBox sectionBox = new JComboBox(Salary);


//making all the panels to be placed inside the main panel. 

JPanel usernamePanel = new JPanel();
JPanel passwordPanel = new JPanel();
JPanel empIDPanel = new JPanel();
JPanel SalaryPanel = new JPanel();
JPanel EmployerPanel = new JPanel();
JPanel hoursPanel = new JPanel();
JPanel departmentsPanel = new JPanel();

JPanel top, bottom;


JButton submitButton = new JButton("Submit");

public tada(){

//这将从超类初始化面板。没有什么真正重要的东西是从超类继承来的。超级(“雇主设计”,10);

    setPreferredSize(new Dimension(400,600));

//将所有标签、文本字段等添加到子面板中,并将子面板添加到底部的主面板中。setLayout(新建BoxLayout(this,BoxLayout.page_axis));top=新的JPanel();top.setSize(getWidth(),30);bottom=新的JPanel();setLayout(新建BoxLayout(bottom,BoxLayout.y_axis));

    top.setBackground(Color.red);
    title.setFont(new Font("Helvetica", 1,20));
    top.add(title);
    bottom.setBackground(Color.yellow);

    usernamePanel.add(usernameLabel);
    usernamePanel.add(usernameField);
    bottom.add(usernamePanel);

    passwordPanel.add(passwordLabel);
    passwordPanel.add(passwordField);
    bottom.add(passwordPanel);

    empIDPanel.add(empIDLabel);
    empIDPanel.add(empIDField);
    bottom.add(empIDPanel);

    SalaryPanel.add(SalaryLabel);
    SalaryPanel.add(SalaryField);
    bottom.add(SalaryPanel);

    EmployerPanel.add(EmployerLabel);
    EmployerPanel.add(EmployerField);
    bottom.add(EmployerPanel);

    hoursPanel.add(hoursLabel);
    hoursPanel.add(hoursBox);
    bottom.add(hoursPanel);

    departmentsPanel.add(departmentLabel);
    for(JCheckBox jbc: departmentCheckBoxesBoxs)
    {
        departmentsPanel.add(jbc);
    }
    bottom.add(departmentsPanel);




    add(top);
    add(bottom);


}

//另外,由于我使用的是boxLayout(),并且它与y_axis相连,我以为它会原子地进行y轴相连,但它没有。

共有1个答案

姜俊民
2023-03-14

在给定代码的情况下,我能看到的最简单的方法就是简单地更改主容器布局管理器。这里我使用了网格袋布局,因为它允许我根据需要更改每个组件的单独要求

public class BadLayout05 {

    public static void main(String[] args) {
        new BadLayout05();
    }

    public BadLayout05() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new tada());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class tada extends JPanel {

        //making all the jlabels to be placed
        JLabel usernameLabel = new JLabel("username");
        JLabel passwordLabel = new JLabel("Name");
        JLabel empIDLabel = new JLabel("empID");
        JLabel SalaryLabel = new JLabel("Salary");
        JLabel EmployerLabel = new JLabel(" Salary");
        JLabel hoursLabel = new JLabel("Gender");
        JLabel departmentLabel = new JLabel("Department");
        //making all the textfields, combo boxes, and etc.
        JTextField usernameField = new JTextField(20);
        JTextField passwordField = new JTextField(20);
        JTextField empIDField = new JTextField(20);
        JTextField SalaryField = new JTextField(20);
        JTextField EmployerField = new JTextField(20);
        String[] hourss = {"Fall", "Spring", "Summer"};
        JComboBox hoursBox = new JComboBox(hourss);
        JCheckBox[] departmentCheckBoxesBoxs = {new JCheckBox("Department 1"),
            new JCheckBox("Department 2"),
            new JCheckBox("Department 3"),
            new JCheckBox("Department 4"),
            new JCheckBox("Department 5")};
        String[] Salary = {"section", "combo", "box"};
        JComboBox sectionBox = new JComboBox(Salary);
//making all the panels to be placed inside the main panel.
        JPanel usernamePanel = new JPanel();
        JPanel passwordPanel = new JPanel();
        JPanel empIDPanel = new JPanel();
        JPanel SalaryPanel = new JPanel();
        JPanel EmployerPanel = new JPanel();
        JPanel hoursPanel = new JPanel();
        JPanel departmentsPanel = new JPanel();
        JPanel top = new JPanel();
        JPanel bottom = new JPanel();
        JButton submitButton = new JButton("Submit");

        public tada() {

//this initializes the panel from superclass. nothing really important inherited from superclass. super("employer design", 10);

        // This is bad idea...
//            setPreferredSize(new Dimension(400, 600));

//adding all the labels, text fields, etc to the sub-panels, and adding subpanels to main pannel, bottom. setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS)); top = new JPanel(); top.setSize(getWidth(), 30); bottom = new JPanel(); bottom.setLayout(new BoxLayout(bottom,BoxLayout.Y_AXIS));

            top.setBackground(Color.red);
            JLabel title = new JLabel("Employer Design");
            // This is a bad idea - IHMO
            title.setFont(new Font("Helvetica", 1, 20));
            top.add(title);
            bottom.setBackground(Color.yellow);

            usernamePanel.add(usernameLabel);
            usernamePanel.add(usernameField);
            bottom.add(usernamePanel);

            passwordPanel.add(passwordLabel);
            passwordPanel.add(passwordField);
            bottom.add(passwordPanel);

            empIDPanel.add(empIDLabel);
            empIDPanel.add(empIDField);
            bottom.add(empIDPanel);

            SalaryPanel.add(SalaryLabel);
            SalaryPanel.add(SalaryField);
            bottom.add(SalaryPanel);

            EmployerPanel.add(EmployerLabel);
            EmployerPanel.add(EmployerField);
            bottom.add(EmployerPanel);

            hoursPanel.add(hoursLabel);
            hoursPanel.add(hoursBox);
            bottom.add(hoursPanel);

            departmentsPanel.add(departmentLabel);
            for (JCheckBox jbc : departmentCheckBoxesBoxs) {
                departmentsPanel.add(jbc);
            }

            GridBagConstraints gbc = new GridBagConstraints();
            setLayout(new GridBagLayout());
            gbc.gridx = 0;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            add(top, gbc);
            gbc.weightx = 0;
            gbc.fill = GridBagConstraints.NONE;
            gbc.anchor = GridBagConstraints.WEST;
            add(usernamePanel, gbc);
            add(passwordPanel, gbc);
            add(empIDPanel, gbc);
            add(SalaryPanel, gbc);
            add(EmployerPanel, gbc);
            add(hoursPanel, gbc);
            add(departmentsPanel, gbc);
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            add(bottom, gbc);

        }
    }
}

更好的解决方案可能是将主字段添加到单个面板中,并使用GridBagLayout

public class BadLayout05 {

    public static void main(String[] args) {
        new BadLayout05();
    }

    public BadLayout05() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new tada());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class tada extends JPanel {

        //making all the jlabels to be placed
        JLabel usernameLabel = new JLabel("username");
        JLabel passwordLabel = new JLabel("Name");
        JLabel empIDLabel = new JLabel("empID");
        JLabel SalaryLabel = new JLabel("Salary");
        JLabel EmployerLabel = new JLabel(" Salary");
        JLabel hoursLabel = new JLabel("Gender");
        JLabel departmentLabel = new JLabel("Department");
        //making all the textfields, combo boxes, and etc.
        JTextField usernameField = new JTextField(20);
        JTextField passwordField = new JTextField(20);
        JTextField empIDField = new JTextField(20);
        JTextField SalaryField = new JTextField(20);
        JTextField EmployerField = new JTextField(20);
        String[] hourss = {"Fall", "Spring", "Summer"};
        JComboBox hoursBox = new JComboBox(hourss);
        JCheckBox[] departmentCheckBoxesBoxs = {new JCheckBox("Department 1"),
            new JCheckBox("Department 2"),
            new JCheckBox("Department 3"),
            new JCheckBox("Department 4"),
            new JCheckBox("Department 5")};
        String[] Salary = {"section", "combo", "box"};
        JComboBox sectionBox = new JComboBox(Salary);
        JPanel fields = new JPanel();
        JPanel departmentsPanel = new JPanel();
        JPanel top = new JPanel();
        JPanel bottom = new JPanel();
        JButton submitButton = new JButton("Submit");

        public tada() {

            top.setBackground(Color.red);
            JLabel title = new JLabel("Employer Design");
            title.setFont(new Font("Helvetica", 1, 20));
            top.add(title);
            bottom.setBackground(Color.yellow);

            fields.setLayout(new GridBagLayout());
            GridBagConstraints gbcLabels = new GridBagConstraints();
            GridBagConstraints gbcFields = new GridBagConstraints();

            gbcLabels.gridx = 0;
            gbcLabels.gridy = 0;
            gbcLabels.anchor = GridBagConstraints.WEST;
            gbcLabels.insets = new Insets(2, 2, 2, 2);

            gbcFields.gridx = 1;
            gbcFields.gridy = 0;
            gbcFields.anchor = GridBagConstraints.WEST;
            gbcFields.weightx = 1;
            gbcFields.insets = new Insets(2, 2, 2, 2);

            fields.add(usernameLabel, gbcLabels);
            fields.add(usernameField, gbcFields);

            gbcFields.gridy = ++gbcLabels.gridy;

            fields.add(passwordLabel, gbcLabels);
            fields.add(passwordField, gbcFields);

            gbcFields.gridy = ++gbcLabels.gridy;

            fields.add(empIDLabel, gbcLabels);
            fields.add(empIDField, gbcFields);

            gbcFields.gridy = ++gbcLabels.gridy;

            fields.add(SalaryLabel, gbcLabels);
            fields.add(SalaryField, gbcFields);

            gbcFields.gridy = ++gbcLabels.gridy;

            fields.add(EmployerLabel, gbcLabels);
            fields.add(EmployerField, gbcFields);

            gbcFields.gridy = ++gbcLabels.gridy;

            fields.add(hoursLabel, gbcLabels);
            fields.add(hoursBox, gbcFields);

            departmentsPanel.add(departmentLabel);
            for (JCheckBox jbc : departmentCheckBoxesBoxs) {
                departmentsPanel.add(jbc);
            }

            GridBagConstraints gbc = new GridBagConstraints();
            setLayout(new GridBagLayout());
            gbc.gridx = 0;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            add(top, gbc);
            gbc.weightx = 0;
            gbc.fill = GridBagConstraints.NONE;
            gbc.anchor = GridBagConstraints.WEST;
            add(fields, gbc);
//            add(passwordPanel, gbc);
//            add(empIDPanel, gbc);
//            add(SalaryPanel, gbc);
//            add(EmployerPanel, gbc);
//            add(hoursPanel, gbc);
            add(departmentsPanel, gbc);
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            add(bottom, gbc);

        }
    }
}
 类似资料:
  • 我想在同一框架中使用2个面板。但是button不起作用了?我该怎么做?我想在一个面板上放几个按钮,另一个面板会做一些其他的事情。

  • 我有2面板(2类,从JPanel扩展),1帧(1类,从JFrame扩展) 我的第一个小组——Welcome小组: 我的第二个面板-董事会面板: 我的主框架——主机 我的问题:帮我写代码,在面板的按钮上添加ActionListener(材料示例)。当我按下播放按钮(WelcomePanel)时,WelcomePanel被隐藏,BoardPanel被显示。当我退出BoardPanel(按下close按

  • 问题内容: 我正在将一个对象传递给模板,而我需要做的就是检查值。如果该值为正,我想用绿色为特定颜色上色。如果值为负,我想用红色将特定颜色上色。 我找不到定义变量的方法。可能吗?应该是我想的 无论如何,最简单的方法是什么? 谢谢 问题答案: 如Play文档中所述,您可以使用帮助器。 或者您可以使用

  • 使用指南 组件介绍 ActionSheet 底部面板,从底部向上拉起操作菜单列表。 引入方式 import { Actionsheet } from 'feart'; components:{ 'fe-actionsheet':Actionsheet, } data() { return { show1: false, show2: false

  • 我正在尝试构建一个基于按钮的动态应用程序,这些按钮从创建的面板内部切换JFrame的主面板(这是我的应用程序的顶部内容)。 这个问题更多的是设计问题,而不是开发问题。我不确定我的设计,但我会试着解释一下。 我有一个JFrame代表我的应用程序,其中包含一个JTabbedPane(和多个Tabs)。每个选项卡都包含一个默认的JPanel,在那个JPanel中,我调用一个正在呈现我的视图的控制器(我正

  • 颜色面板概述 “颜色”面板(“窗口”>“颜色”)显示当前前景色和背景色的颜色值。使用“颜色”面板中的滑块,可以利用几种不同的颜色模型来编辑前景色和背景色。也可以从显示在面板底部的四色曲线图中的色谱中选取前景色或背景色。“颜色”面板 A. 前景色 B. 背景色 C. 滑块 D. 四色曲线图  当您选择颜色时,“颜色”面板可能显示下列警告: 当选取不能使用 CMYK 油墨打印的颜色时,四色曲线图左上方