gpt4 book ai didi

java - 在框架上定位 GUI 对象

转载 作者:行者123 更新时间:2023-11-29 09:54:57 26 4
gpt4 key购买 nike

我正在开发一个 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 上定位对象?

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

enter image description here

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

最佳答案

你永远不应该尝试用坐标来定位组件。而是使用适当的 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();
}
});
}
}

关于java - 在框架上定位 GUI 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16397609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com