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.");
}
}
}
答案 0 :(得分:0)
您可以通过以下示例实现此组件布局。在这里,我将布局管理器 function markdownToDocs() {
const body = DocumentApp.getActiveDocument().getBody();
// Use editAsText to obtain a single text element containing
// all the characters in the document.
const text = body.editAsText();
// e.g. replace "**string**" with "string" (bolded)
var boldStyle = {};
boldStyle[DocumentApp.Attribute.BOLD] = true;
replaceDeliminaters(body, "\\*\\*", boldStyle, false);
// e.g. replace multiline "```line 1\nline 2\nline 3```" with "line 1\nline 2\nline 3" (with gray background highlight)
var blockHighlightStyle = {};
blockHighlightStyle[DocumentApp.Attribute.BACKGROUND_COLOR] = "#EEEEEE";
replaceDeliminaters(body, "```", blockHighlightStyle, true);
// e.g. replace inline "`console.log("hello world")`" with "console.log("hello world")" (in "Times New Roman" font and italic)
var inlineStyle = {};
inlineStyle[DocumentApp.Attribute.FONT_FAMILY] = "Times New Roman";
inlineStyle[DocumentApp.Attribute.ITALIC] = true;
replaceDeliminaters(body, "`", inlineStyle, false);
// feel free to change all the styling and markdown deliminaters as you wish.
}
// replace markdown deliminaters like "**", "`", and "```"
function replaceDeliminaters(body, deliminator, attributes, multiline){
var capture;
if (multiline){
capture = "([\\s\\S]+?)"; // capture newline characters as well
} else{
capture = "(.+?)"; // do not capture newline characters
}
const regex = new RegExp(deliminator + capture + deliminator, "g");
const replacer = function(match, regex){
return match[1]; // return the first capture group
}
replaceText(body, regex, replacer, attributes);
}
用于主面板。对于GridBagLayout
,我尚未设置布局管理器。因此,此处使用了默认的布局管理器buttonPanel
。
请注意如何为每个组件使用具有不同值的FlowLayout
对象。
GridBagConstraints