如何让我的 GUI 显示与 JTextFields 一致的 JLabel?
How do I get my GUI to display my JLabels in line with JTextFields?
This is what the passwordVault GUI currently looks like
我想要三行,每行包含一个 JLabel 和 JTextField,然后是底部的三个按钮。我当前的代码无法完成此操作,但有人知道我该怎么做吗?
我试过将每个元素添加到 JFrame 的顺序,但到目前为止,没有任何效果。请帮忙!这是我的 IB 计算机科学 IA。
import javax.swing.*;
import javax.swing.table.*;
import java.awt.FlowLayout;
import java.awt.event.*;
public class PasswordVault implements ActionListener
{
static String masterPassword, enteredPassword;
JFrame masterPasswordFrame, passwordVault;
JPasswordField masterPasswordField;
JTable passwordTable;
Object[] columnNames = {"Name of Application", "Application Password", "Description"};
Object[] rowData = new Object[3];
JTextField appName, appPass, appDesc;
JButton add, delete, update;
JLabel nameOfApp, passOfApp, descOfApp;
public static void main(String[] args)
{
String name = JOptionPane.showInputDialog("What is your name?");
masterPassword = JOptionPane.showInputDialog("Hello, " + name + ". " + "What would you like your master password to be?");
//The master password will be used to access the Password Vault
new PasswordVault();
}
public PasswordVault()
{
//This is the frame that will ask the user to input their password
masterPasswordFrame = new JFrame("Master Password");
masterPasswordFrame.setSize(400,100);
masterPasswordFrame.setLocationRelativeTo(null);
masterPasswordFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel masterPasswordPanel = new JPanel();
//Accepts the master password from the user; when the user types the password, rather than words it will show asterisks
masterPasswordField = new JPasswordField(10);
masterPasswordField.setEchoChar('*');
JLabel passwordLabel = new JLabel("Enter Password: ");
JButton okayButton = new JButton("Check");
okayButton.addActionListener(this);
masterPasswordPanel.add(passwordLabel);
masterPasswordPanel.add(masterPasswordField);
masterPasswordPanel.add(okayButton);
masterPasswordFrame.add(masterPasswordPanel);
masterPasswordFrame.setVisible(true);
masterPasswordFrame.pack();
enteredPassword = new String(masterPasswordField.getPassword());
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);
FlowLayout flowLayout = new FlowLayout();
JScrollPane scrollPane = new JScrollPane(passwordTable);
scrollPane.setBounds(0, 0, 1000, 200);
passwordVault.setLayout(flowLayout);
passwordVault.add(scrollPane);
//These labels don't appear in the vault
passwordPanel.add(nameOfApp);
passwordPanel.add(passOfApp);
passwordPanel.add(descOfApp);
passwordVault.add(passwordPanel);
passwordVault.add(appName);
passwordVault.add(appPass);
passwordVault.add(appDesc);
passwordVault.add(add);
passwordVault.add(update);
passwordVault.add(delete);
add.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
rowData[0] = appName.getText();
rowData[1] = appPass.getText();
rowData[2] = appDesc.getText();
tableModel.addRow(rowData);
appName.setText("");
appPass.setText("");
appDesc.setText("");
}
});
update.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int i = passwordTable.getSelectedRow();
if(i >= 0)
{
tableModel.setValueAt(appName.getText(), i, 0);
tableModel.setValueAt(appPass.getText(), i, 1);
tableModel.setValueAt(appDesc.getText(), i, 2);
appName.setText("");
appPass.setText("");
appDesc.setText("");
}
else
{
JOptionPane.showMessageDialog(null, "Update Error");
}
}
});
delete.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int i = passwordTable.getSelectedRow();
if(i >= 0)
{
tableModel.removeRow(i);
appName.setText("");
appPass.setText("");
appDesc.setText("");
}
else
{
JOptionPane.showMessageDialog(null, "Delete Error");
}
}
});
passwordTable.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
int i = passwordTable.getSelectedRow();
appName.setText(tableModel.getValueAt(i, 0).toString());
appPass.setText(tableModel.getValueAt(i, 1).toString());
appDesc.setText(tableModel.getValueAt(i, 2).toString());
}
});
passwordVault.setSize(1000,500);
passwordVault.setLocationRelativeTo(null);
passwordVault.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event)
{
if(new String(masterPasswordField.getPassword()).equals(masterPassword))
{
masterPasswordFrame.setVisible(false);
passwordVault.setVisible(true);
}
else
{
JOptionPane.showMessageDialog(null, "Password Incorrect. Please Try Again.");
}
}
}
您可以通过以下示例实现此组件布局。在这里,我为主面板使用了布局管理器 GridBagLayout
。对于 buttonPanel
我还没有设置布局管理器。因此,默认布局管理器 FlowLayout
在那里使用。
请注意我如何使用 GridBagConstraints
个对象,每个组件具有不同的值。
import javax.swing.*;
import java.awt.*;
public class LayoutComponents
{
public static void main(String[] args)
{
JLabel nameOfApp = new JLabel("App Name: ");
JLabel passOfApp = new JLabel("App password: ");
JLabel descOfApp = new JLabel("Description: ");
JTextField appName = new JTextField();
JTextField appPass = new JTextField();
JTextField appDesc = new JTextField();
JButton add = new JButton("Add");
JButton delete = new JButton("Delete");
JButton update = new JButton("Update");
JPanel buttonPanel = new JPanel();
buttonPanel.add(add);
buttonPanel.add(delete);
buttonPanel.add(update);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(new GridBagLayout());
f.getContentPane().add(nameOfApp, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
f.getContentPane().add(appName, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
f.getContentPane().add(passOfApp, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
f.getContentPane().add(appPass, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
f.getContentPane().add(descOfApp, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
f.getContentPane().add(appDesc, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
f.getContentPane().add(buttonPanel, new GridBagConstraints(0, 3, 2, 1, 1.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
f.setBounds(300, 200, 400, 300);
f.setVisible(true);
}
}
This is what the passwordVault GUI currently looks like
我想要三行,每行包含一个 JLabel 和 JTextField,然后是底部的三个按钮。我当前的代码无法完成此操作,但有人知道我该怎么做吗?
我试过将每个元素添加到 JFrame 的顺序,但到目前为止,没有任何效果。请帮忙!这是我的 IB 计算机科学 IA。
import javax.swing.*;
import javax.swing.table.*;
import java.awt.FlowLayout;
import java.awt.event.*;
public class PasswordVault implements ActionListener
{
static String masterPassword, enteredPassword;
JFrame masterPasswordFrame, passwordVault;
JPasswordField masterPasswordField;
JTable passwordTable;
Object[] columnNames = {"Name of Application", "Application Password", "Description"};
Object[] rowData = new Object[3];
JTextField appName, appPass, appDesc;
JButton add, delete, update;
JLabel nameOfApp, passOfApp, descOfApp;
public static void main(String[] args)
{
String name = JOptionPane.showInputDialog("What is your name?");
masterPassword = JOptionPane.showInputDialog("Hello, " + name + ". " + "What would you like your master password to be?");
//The master password will be used to access the Password Vault
new PasswordVault();
}
public PasswordVault()
{
//This is the frame that will ask the user to input their password
masterPasswordFrame = new JFrame("Master Password");
masterPasswordFrame.setSize(400,100);
masterPasswordFrame.setLocationRelativeTo(null);
masterPasswordFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel masterPasswordPanel = new JPanel();
//Accepts the master password from the user; when the user types the password, rather than words it will show asterisks
masterPasswordField = new JPasswordField(10);
masterPasswordField.setEchoChar('*');
JLabel passwordLabel = new JLabel("Enter Password: ");
JButton okayButton = new JButton("Check");
okayButton.addActionListener(this);
masterPasswordPanel.add(passwordLabel);
masterPasswordPanel.add(masterPasswordField);
masterPasswordPanel.add(okayButton);
masterPasswordFrame.add(masterPasswordPanel);
masterPasswordFrame.setVisible(true);
masterPasswordFrame.pack();
enteredPassword = new String(masterPasswordField.getPassword());
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);
FlowLayout flowLayout = new FlowLayout();
JScrollPane scrollPane = new JScrollPane(passwordTable);
scrollPane.setBounds(0, 0, 1000, 200);
passwordVault.setLayout(flowLayout);
passwordVault.add(scrollPane);
//These labels don't appear in the vault
passwordPanel.add(nameOfApp);
passwordPanel.add(passOfApp);
passwordPanel.add(descOfApp);
passwordVault.add(passwordPanel);
passwordVault.add(appName);
passwordVault.add(appPass);
passwordVault.add(appDesc);
passwordVault.add(add);
passwordVault.add(update);
passwordVault.add(delete);
add.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
rowData[0] = appName.getText();
rowData[1] = appPass.getText();
rowData[2] = appDesc.getText();
tableModel.addRow(rowData);
appName.setText("");
appPass.setText("");
appDesc.setText("");
}
});
update.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int i = passwordTable.getSelectedRow();
if(i >= 0)
{
tableModel.setValueAt(appName.getText(), i, 0);
tableModel.setValueAt(appPass.getText(), i, 1);
tableModel.setValueAt(appDesc.getText(), i, 2);
appName.setText("");
appPass.setText("");
appDesc.setText("");
}
else
{
JOptionPane.showMessageDialog(null, "Update Error");
}
}
});
delete.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int i = passwordTable.getSelectedRow();
if(i >= 0)
{
tableModel.removeRow(i);
appName.setText("");
appPass.setText("");
appDesc.setText("");
}
else
{
JOptionPane.showMessageDialog(null, "Delete Error");
}
}
});
passwordTable.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
int i = passwordTable.getSelectedRow();
appName.setText(tableModel.getValueAt(i, 0).toString());
appPass.setText(tableModel.getValueAt(i, 1).toString());
appDesc.setText(tableModel.getValueAt(i, 2).toString());
}
});
passwordVault.setSize(1000,500);
passwordVault.setLocationRelativeTo(null);
passwordVault.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event)
{
if(new String(masterPasswordField.getPassword()).equals(masterPassword))
{
masterPasswordFrame.setVisible(false);
passwordVault.setVisible(true);
}
else
{
JOptionPane.showMessageDialog(null, "Password Incorrect. Please Try Again.");
}
}
}
您可以通过以下示例实现此组件布局。在这里,我为主面板使用了布局管理器 GridBagLayout
。对于 buttonPanel
我还没有设置布局管理器。因此,默认布局管理器 FlowLayout
在那里使用。
请注意我如何使用 GridBagConstraints
个对象,每个组件具有不同的值。
import javax.swing.*;
import java.awt.*;
public class LayoutComponents
{
public static void main(String[] args)
{
JLabel nameOfApp = new JLabel("App Name: ");
JLabel passOfApp = new JLabel("App password: ");
JLabel descOfApp = new JLabel("Description: ");
JTextField appName = new JTextField();
JTextField appPass = new JTextField();
JTextField appDesc = new JTextField();
JButton add = new JButton("Add");
JButton delete = new JButton("Delete");
JButton update = new JButton("Update");
JPanel buttonPanel = new JPanel();
buttonPanel.add(add);
buttonPanel.add(delete);
buttonPanel.add(update);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(new GridBagLayout());
f.getContentPane().add(nameOfApp, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
f.getContentPane().add(appName, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
f.getContentPane().add(passOfApp, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
f.getContentPane().add(appPass, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
f.getContentPane().add(descOfApp, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
f.getContentPane().add(appDesc, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
f.getContentPane().add(buttonPanel, new GridBagConstraints(0, 3, 2, 1, 1.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
f.setBounds(300, 200, 400, 300);
f.setVisible(true);
}
}