我正在开发一个Java应用程序,并希望在定位一些标签和TextField方面提供一些帮助。
这是我的代码:
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上的对象?
这是一张图片,向您展示当前如何显示对象:
答案 0 :(得分:5)
最简单的是GridLayout(2, 2, 10, 10),
但每个JComponents都可以使用其容器
必须更改JLabel的水平对齐方式(setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
或setHorizontalAlignment(JLabel.RIGHT);
)
确定(JTextField TextFieldUserName = new JTextField(20
))任何LayoutManager
的初始维度
答案 1 :(得分: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();
}
});
}
}