如何正确定位 JButtons 和调整 JTextFields 的大小?
How to correctly position JButtons and size JTextFields?
我正在尝试创建一个密码保险库,但每当我尝试 运行 它时,JTextField
都非常小,面板有点被 JTable
覆盖.
我试过 GridBag
的尺寸和位置,但没有任何效果。
这是我的 IB 计算机科学 IA。任何帮助都会很棒。
JFrame passwordVault;
JTable passwordTable;
Object[] columnNames = {"Name of Application", "Application Password", "Description"};
JTextField appName, appPass, appDesc;
JButton add, delete, update;
JLabel nameOfApp, passOfApp, descOfApp;
passwordVault = new JFrame("Password Vault");
passwordTable = new JTable();
JPanel passwordPanel = new JPanel();
DefaultTableModel tableModel = new DefaultTableModel();
tableModel.setColumnIdentifiers(columnNames);
passwordTable.setModel(tableModel);
nameOfApp = new JLabel("App Name: ");
passOfApp = new JLabel("App Password: ");
descOfApp = new JLabel("Description: ");
appName = new JTextField();
appPass = new JTextField();
appDesc = new JTextField();
add = new JButton("Add");
delete = new JButton("Delete");
update = new JButton("Update");
appName.setBounds(400, 220, 100, 25);
appPass.setBounds(400, 250, 100, 25);
appDesc.setBounds(400, 280, 100, 25);
add.setBounds(530, 220, 100, 25);
update.setBounds(530, 250, 100, 25);
delete.setBounds(530, 280, 100, 25);
JScrollPane scrollPane = new JScrollPane(passwordTable);
scrollPane.setBounds(0, 0, 1000, 200);
passwordVault.add(scrollPane);
passwordPanel.add(add);
passwordPanel.add(update);
passwordPanel.add(delete);
passwordPanel.setLayout(new GridBagLayout());
passwordPanel.add(nameOfApp, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(appName, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(passOfApp, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(appPass, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(descOfApp, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(appDesc, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(add, new GridBagConstraints(3, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(update, new GridBagConstraints(4, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(delete, new GridBagConstraints(5, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
passwordVault.add(passwordPanel);
passwordVault.setSize(1000,500);
passwordVault.setLocationRelativeTo(null);
passwordVault.setVisible(true);
这是您的程序的编辑版本,它按照我认为您尝试的方式格式化布局:
JFrame passwordVault;
JTable passwordTable;
Object[] columnNames = {"Name of Application", "Application Password", "Description"};
JTextField appName, appPass, appDesc;
JButton add, delete, update;
JLabel nameOfApp, passOfApp, descOfApp;
passwordVault = new JFrame("Password Vault");
passwordTable = new JTable();
JPanel passwordPanel = new JPanel();
DefaultTableModel tableModel = new DefaultTableModel();
tableModel.setColumnIdentifiers(columnNames);
passwordTable.setModel(tableModel);
nameOfApp = new JLabel("App Name: ");
passOfApp = new JLabel("App Password: ");
descOfApp = new JLabel("Description: ");
appName = new JTextField();
appPass = new JTextField();
appDesc = new JTextField();
add = new JButton("Add");
delete = new JButton("Delete");
update = new JButton("Update");
// Used setPreferredSize and setLocation instead of setBounds
appName.setLocation(new Point(400, 220));
appName.setPreferredSize(new Dimension(100, 25));
appPass.setLocation(new Point(400, 250));
appPass.setPreferredSize(new Dimension(100, 25));
appDesc.setLocation(new Point(400, 280));
appDesc.setPreferredSize(new Dimension(100, 25));
add.setLocation(new Point(530, 220));
add.setPreferredSize(new Dimension(100, 25));
update.setLocation(new Point(530, 250));
update.setPreferredSize(new Dimension(100, 25));
delete.setLocation(new Point(530, 280));
delete.setPreferredSize(new Dimension(100, 25));
JScrollPane scrollPane = new JScrollPane(passwordTable);
scrollPane.setBounds(0, 0, 1000, 200);
// Create a JPanel to contain the scrollPane and passwordPanel
JPanel framePanel = new JPanel();
framePanel.setLayout(new GridLayout(2, 1));
framePanel.add(scrollPane);
passwordPanel.setLayout(new GridBagLayout());
passwordPanel.add(nameOfApp, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(appName, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(passOfApp, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(appPass, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(descOfApp, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(appDesc, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(add, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(update, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(delete, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
framePanel.add(passwordPanel);
// Add the framePanel (which contains the other 2 panels) to the JFrame
passwordVault.add(framePanel);
passwordVault.setSize(1000,500);
passwordVault.setLocationRelativeTo(null);
passwordVault.setVisible(true);
因此,主要变化是:
- 使用
setPreferredSize
和 setLocation
,而不是 setBounds
作为 JTextField
和 JButton
的
- 创建了一个主
JPanel
(称为 framePanel
)来包含 scrollPane
和 passwordPanel
,然后将 framePanel
添加到 JFrame
它有助于通过创建主 JPanel
来组织 JFrame
中的不同元素,而不是仅仅向 JFrame
本身添加多个 JPanel
。这样一来,主 JPanel
决定了框架的整体布局。
我正在尝试创建一个密码保险库,但每当我尝试 运行 它时,JTextField
都非常小,面板有点被 JTable
覆盖.
我试过 GridBag
的尺寸和位置,但没有任何效果。
这是我的 IB 计算机科学 IA。任何帮助都会很棒。
JFrame passwordVault;
JTable passwordTable;
Object[] columnNames = {"Name of Application", "Application Password", "Description"};
JTextField appName, appPass, appDesc;
JButton add, delete, update;
JLabel nameOfApp, passOfApp, descOfApp;
passwordVault = new JFrame("Password Vault");
passwordTable = new JTable();
JPanel passwordPanel = new JPanel();
DefaultTableModel tableModel = new DefaultTableModel();
tableModel.setColumnIdentifiers(columnNames);
passwordTable.setModel(tableModel);
nameOfApp = new JLabel("App Name: ");
passOfApp = new JLabel("App Password: ");
descOfApp = new JLabel("Description: ");
appName = new JTextField();
appPass = new JTextField();
appDesc = new JTextField();
add = new JButton("Add");
delete = new JButton("Delete");
update = new JButton("Update");
appName.setBounds(400, 220, 100, 25);
appPass.setBounds(400, 250, 100, 25);
appDesc.setBounds(400, 280, 100, 25);
add.setBounds(530, 220, 100, 25);
update.setBounds(530, 250, 100, 25);
delete.setBounds(530, 280, 100, 25);
JScrollPane scrollPane = new JScrollPane(passwordTable);
scrollPane.setBounds(0, 0, 1000, 200);
passwordVault.add(scrollPane);
passwordPanel.add(add);
passwordPanel.add(update);
passwordPanel.add(delete);
passwordPanel.setLayout(new GridBagLayout());
passwordPanel.add(nameOfApp, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(appName, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(passOfApp, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(appPass, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(descOfApp, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(appDesc, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(add, new GridBagConstraints(3, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(update, new GridBagConstraints(4, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(delete, new GridBagConstraints(5, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
passwordVault.add(passwordPanel);
passwordVault.setSize(1000,500);
passwordVault.setLocationRelativeTo(null);
passwordVault.setVisible(true);
这是您的程序的编辑版本,它按照我认为您尝试的方式格式化布局:
JFrame passwordVault;
JTable passwordTable;
Object[] columnNames = {"Name of Application", "Application Password", "Description"};
JTextField appName, appPass, appDesc;
JButton add, delete, update;
JLabel nameOfApp, passOfApp, descOfApp;
passwordVault = new JFrame("Password Vault");
passwordTable = new JTable();
JPanel passwordPanel = new JPanel();
DefaultTableModel tableModel = new DefaultTableModel();
tableModel.setColumnIdentifiers(columnNames);
passwordTable.setModel(tableModel);
nameOfApp = new JLabel("App Name: ");
passOfApp = new JLabel("App Password: ");
descOfApp = new JLabel("Description: ");
appName = new JTextField();
appPass = new JTextField();
appDesc = new JTextField();
add = new JButton("Add");
delete = new JButton("Delete");
update = new JButton("Update");
// Used setPreferredSize and setLocation instead of setBounds
appName.setLocation(new Point(400, 220));
appName.setPreferredSize(new Dimension(100, 25));
appPass.setLocation(new Point(400, 250));
appPass.setPreferredSize(new Dimension(100, 25));
appDesc.setLocation(new Point(400, 280));
appDesc.setPreferredSize(new Dimension(100, 25));
add.setLocation(new Point(530, 220));
add.setPreferredSize(new Dimension(100, 25));
update.setLocation(new Point(530, 250));
update.setPreferredSize(new Dimension(100, 25));
delete.setLocation(new Point(530, 280));
delete.setPreferredSize(new Dimension(100, 25));
JScrollPane scrollPane = new JScrollPane(passwordTable);
scrollPane.setBounds(0, 0, 1000, 200);
// Create a JPanel to contain the scrollPane and passwordPanel
JPanel framePanel = new JPanel();
framePanel.setLayout(new GridLayout(2, 1));
framePanel.add(scrollPane);
passwordPanel.setLayout(new GridBagLayout());
passwordPanel.add(nameOfApp, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(appName, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(passOfApp, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(appPass, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(descOfApp, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(appDesc, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(add, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(update, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
passwordPanel.add(delete, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
framePanel.add(passwordPanel);
// Add the framePanel (which contains the other 2 panels) to the JFrame
passwordVault.add(framePanel);
passwordVault.setSize(1000,500);
passwordVault.setLocationRelativeTo(null);
passwordVault.setVisible(true);
因此,主要变化是:
- 使用
setPreferredSize
和setLocation
,而不是setBounds
作为JTextField
和JButton
的 - 创建了一个主
JPanel
(称为framePanel
)来包含scrollPane
和passwordPanel
,然后将framePanel
添加到JFrame
它有助于通过创建主 JPanel
来组织 JFrame
中的不同元素,而不是仅仅向 JFrame
本身添加多个 JPanel
。这样一来,主 JPanel
决定了框架的整体布局。