在框架上放置GUI对象 [英] Positioning GUI objects on a Frame

查看:83
本文介绍了在框架上放置GUI对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Java应用程序,希望在定位一些Labels和TextFields方面获得帮助.

I am developing a Java application and would like some help in positioning some Labels and TextFields.

这是我的代码:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextField; 
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;

public class AuctionClient
{
public AuctionClient()
{
    JFrame GUIFrame = new JFrame();
    JPanel GUIPanel = new JPanel();
    JLabel LabelUserName = new JLabel("UserName:");
    JTextField TextFieldUserName = new JTextField("                               ");

    JLabel LabelPassword = new JLabel("Password:");
    JTextField TextFieldPassword = new JTextField("                               ");        

    GUIFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GUIFrame.setTitle("Auction Client");
    GUIFrame.setSize(500,250);

    GUIFrame.setLocationRelativeTo(null);

    GUIPanel.add(LabelUserName);
    GUIPanel.add(TextFieldUserName);        

    GUIPanel.add(LabelPassword);
    GUIPanel.add(TextFieldPassword);         

    GUIFrame.add(GUIPanel, BorderLayout.NORTH);
    GUIFrame.setVisible(true);
}  
}

使用上面的代码,LabelPassword和TextFieldPassword与LabelUsername和TextFieldUsername在同一行.我可以帮忙将LabelPassword和TextFieldPassword放在新行上吗?是否可以指定X,Y坐标在JFrame上定位对象?

With the above code, the LabelPassword and TextFieldPassword is on the same line as the LabelUsername and TextFieldUsername. Can I please have some help to position the LabelPassword and TextFieldPassword on a new line. Is it possible to specify X,Y coordinates to position objects on a JFrame?

这是一张图片,向您展示当前如何显示对象:

Here is an image to show you how the objects are currently being shown:

http://canning.co.nz/Java/Positioning_Image.png

推荐答案

永远不要尝试使用坐标定位组件.宁可使用适当的LayoutManager's并使用逻辑条件和约束来定位组件.

You should never try to position components with coordinates. Rather use appropriate LayoutManager's and use logical conditions and constraints to position your components.

以下是使用GridBagLayout的一个示例:

Here is one example using GridBagLayout:

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class AuctionClient {
    public AuctionClient() {
        JFrame guiFrame = new JFrame();
        JPanel guiPanel = new JPanel(new GridBagLayout());
        JLabel userNameLabel = new JLabel("UserName:");
        JTextField userNameTextField = new JTextField(30);

        JLabel passwordLabel = new JLabel("Password:");
        JTextField passwordTextField = new JPasswordField(30);

        guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        guiFrame.setTitle("Auction Client");
        guiFrame.setSize(500, 250);

        guiFrame.setLocationRelativeTo(null);
        GridBagConstraints labelGBC = new GridBagConstraints();
        labelGBC.insets = new Insets(3, 3, 3, 3);
        GridBagConstraints fieldGBC = new GridBagConstraints();
        fieldGBC.insets = new Insets(3, 3, 3, 3);
        fieldGBC.gridwidth = GridBagConstraints.REMAINDER;
        guiPanel.add(userNameLabel, labelGBC);
        guiPanel.add(userNameTextField, fieldGBC);

        guiPanel.add(passwordLabel, labelGBC);
        guiPanel.add(passwordTextField, fieldGBC);

        guiFrame.add(guiPanel, BorderLayout.NORTH);
        guiFrame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new AuctionClient();
            }
        });
    }
}

这篇关于在框架上放置GUI对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆