1

我正在开发一个 Java 应用程序,希望在定位一些标签和文本字段方面得到一些帮助。

这是我的代码:

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 上定位对象?

这是一张图片,向您展示对象当前的显示方式:

在此处输入图像描述

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

4

2 回答 2

5
  • GridLayout, SpringLayout, GridBagLayout默认可以做到

  • 最简单的可以是GridLayout(2, 2, 10, 10)

    1. 但是每个 JComponent 都可以通过其容器调整大小

    2. 必须更改 JLabel (setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);setHorizontalAlignment(JLabel.RIGHT);)的水平对齐

    3. 确定 ( JTextField TextFieldUserName = new JTextField(20)) 任何初始维度LayoutManager

于 2013-05-06T11:23:15.503 回答
5

您永远不应该尝试使用坐标定位组件。而是使用适当LayoutManager's的逻辑条件和约束来定位您的组件。

这是一个使用示例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();
            }
        });
    }
}
于 2013-05-06T11:30:53.793 回答