Basically, I have to do the same thing that is on this website:tigerRam | COSC 181 Lab 7.1 | Password Tester
I dont need to check my GUI layout and my layout should not be in he constructor. I need to use other classes that extend JPanel.
Printable View
Basically, I have to do the same thing that is on this website:tigerRam | COSC 181 Lab 7.1 | Password Tester
I dont need to check my GUI layout and my layout should not be in he constructor. I need to use other classes that extend JPanel.
Okay, so here is the code I have.
Code :import java.awt.*; // for layout managers import java.awt.event.*; // for event handling import javax.swing.*; // for GUI components public class PasswordCheck extends JFrame { private LaxPanel laxPanel; private StrictPanel strictPanel; public PasswordCheck() { super("Password Checker"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout()); JPanel northPanel = new JPanel(); northPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); contentPane.add(northPanel, BorderLayout.NORTH); String instructions = "<html>Welcome to Password Tester.<br>" + "Choose how you want to evaluate your password,<br>" + "enter it below, and click evaluate</html>"; northPanel.add(new JLabel(instructions, JLabel.CENTER), BorderLayout.NORTH); JPanel centerPanel = new JPanel(new GridLayout(0, 3, 10, 10)); centerPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10)); contentPane.add(centerPanel, BorderLayout.CENTER); laxPanel = new LaxPanel(); centerPanel.add(laxPanel); strictPanel = new StrictPanel(); centerPanel.add(strictPanel); JPanel southPanel = new JPanel(); southPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); contentPane.add(southPanel, BorderLayout.SOUTH); JButton evaluate = new JButton("Evaluate"); southPanel.add(evaluate); evaluate.addActionListener(new Listener()); pack(); setVisible(true); } private class Listener implements ActionListener { public void actionPerformed(ActionEvent event) { evaluation(); } } private void evaluation() { String lax = laxPanel.getLax(); String strict = strictPanel.getStrict(); if (lax == null && strict == null) { JOptionPane.showMessageDialog(this, "You need to select either lax or strict evaluation.", "PasswordCheck", JOptionPane.ERROR_MESSAGE); } else { JOptionPane.showMessageDialog(this, ("Your password is secure"), "PasswordCheck", JOptionPane.INFORMATION_MESSAGE); } } public static void main(String[] args) { new PasswordCheck(); } }
Code :import javax.swing.*; public class LaxPanel extends JPanel { private JRadioButton laxButton; public LaxPanel() { setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); laxButton = new JRadioButton("Lax evaluation"); add(laxButton); } public String getLax() { if (laxButton.isSelected()) return "lax button"; else return null; } }
Code :import javax.swing.*; public class StrictPanel extends JPanel { private JRadioButton strictButton; public StrictPanel() { setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); strictButton = new JRadioButton("Strict evaluation"); add(strictButton); } public String getStrict() { if (strictButton.isSelected()) return "Strict button"; else return null; } }
from here I need to make it so only one radio button can be chosen, but since they are on different panels I don't think I can add a button group. I also need to put in a user input box above the evaluate button so the user can input their password. Then I need to evaluate if their password is strong or not based on the criteria above.
Are you asking how to make sure that they don't have lax and strict on at same time?
If so, use ButtonGroup.
Code java:ButtonGroup group = new RadioButtonGroup(); JRadioButton lax = new JRadioButton("Lax"); JRadioButton strict = new JRadioButton("Strict"); group.add(lax); group.add(strict);
Now you can select "Lax" or "Strict", but not both.
And selecting "Lax" automatically deselects "Strict" and selecting "Strict" automatically deselects "Lax".
yeah, but they are in two different panels. Is there a way to keep the panels separate like i have them and make it so they are in a buttongroup?