Results 1 to 5 of 5
- 04-04-2012, 11:03 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 16
- Rep Power
- 0
JFrame freezes when button is clicked
Hi!
I've ran into a pretty anoying problem that I just can't seem to solve myself, so I'm asking you guys for advice instead.
Ok, so I've got a JFrame with a TextField, a Button and a ActionListener for the button. I get no errors or anything, but when i try to click the button the program freezes. For those of you wondering I've got javafx_sdk-2_0_1-windows-x64, jdk-6u29-windows-x64 and jdk-7u1-windows-x64
Code:
Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.lang.*; class SimonStopper extends JFrame{ static boolean yes = new Boolean(true); static int maxX; static int maxY; int k = 0; int l = 0; //char[] letters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', //'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'z'}; static String information_text = new String("Skriv in riktig passord for å låse opp pcen"); static String input_text = new String("Skriv in passord her"); static String confirm_text = new String("Confirm"); static JPanel panel = new JPanel(); static JLabel information = new JLabel(information_text); public static JPasswordField input = new JPasswordField(100); static JButton confirm = new JButton(confirm_text); static String password = new String("ipconfig"); ActionHandler handle = new ActionHandler(); public static void main(String[] args){ Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); maxX = screenSize.width; maxY = screenSize.height - 50; //Makes window SimonStopper vindu = new SimonStopper(); vindu.setLocation(0, 0); vindu.setSize(maxX, maxY); vindu.setAlwaysOnTop(true); vindu.setUndecorated(true); vindu.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); //vindu.setExtendedState(vindu.getExtendedState()|JFrame.MAXIMIZED_BOTH); vindu.setResizable(false); //setIconImage(). //FOR THE FUTURE vindu.setVisible(true); } public SimonStopper(){ //Sets the layouts setLayout(new BorderLayout()); panel.setLayout(new BorderLayout()); //Adds stuff to the screen panel.add(information, BorderLayout.PAGE_START); panel.add(input, BorderLayout.LINE_START); panel.add(confirm, BorderLayout.CENTER); add(panel, BorderLayout.PAGE_START); input.requestFocusInWindow(); //ActionListener //input.addActionListener(this); confirm.addActionListener(handle); addComponentListener(new ComponentAdapter() { public void componentMoved(ComponentEvent e) { setLocation(0,0); } }); } private static boolean isPasswordCorrect(char[] input){ char[] passW = {'i', 'p', 'c', 'o', 'n', 'f', 'i', 'g'}; boolean isCorrect = false; if (input.length != passW.length) { isCorrect = false; } else { isCorrect = Arrays.equals(input, passW); } return isCorrect; } class ActionHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ SimonStopper stopper = new SimonStopper(); char[] inputPassword = input.getPassword(); System.out.println(inputPassword); //stopper.windowActivated(); /*Runtime run = Runtime.getRuntime(); try{ Process p = run.exec("C:/Jacob/AntiTyveri/CompileAndRun.bat"); }catch(Exception error){ error.printStackTrace(); }*/ if(stopper.isPasswordCorrect(inputPassword)){ System.exit(0); System.out.println("correct"); }else{ System.out.println("incorrect"); } } } }Last edited by JG4m3r; 04-04-2012 at 11:06 PM.
- 04-04-2012, 11:36 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: JFrame freezes when button is clicked
As you have the code at the moment it is very hard to debug. Consider commenting out the line which maximizes the window so you can see output from the program (or your IDE if you're using one.)
Next I would suggest removing the static things. main() is required to be static, but for the rest I would suggest declaring things as "private" unless you have a reason not to. (keeping the compiler quiet is not a reason! Compiler messages are generally very useful indicators of a problem.)
Related to this is the way you create a second frame when the button is clicked. You then (in the constructor) take components that used to be in the first frame and move them to the second. The gui is left in a rather odd state which could well be why it appears "frozen" to the user. Consider what you want the action listener to actually do and find a way to do that without creating a second frame.Last edited by pbrockway2; 04-04-2012 at 11:45 PM.
- 04-04-2012, 11:43 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: JFrame freezes when button is clicked
Thinking about this...
No-one likes to be told to throw their code away and start again, so I won't suggest that. But if you said what you wanted this SimonStopper frame to actually do, someone might be able to suggest an approach that was simpler. It's strikes me that what you have is overly complex. (not that I know exactly what it's supposed to do ;)
-----
I'll move this thread to the Swing forum - because that's where all the Swing expertise is.
- 04-05-2012, 10:43 AM #4
Member
- Join Date
- Mar 2012
- Posts
- 16
- Rep Power
- 0
Re: JFrame freezes when button is clicked
The point of this program is to make a frame/window that displays in front of everything, that you can not close, move or minimize. The only way that you are supposed to possibly close down the frame is by entering the correct password.
Yes, i know that you can get around this by pressing: ctrl+alt+delete, alt+tab or the windows button, but these are problems that I will solve later.
-
Re: JFrame freezes when button is clicked
- If this is not for some sort of kiosk application, you will have some very angry users if they try to use your program and then find themselves stuck.
- And regardless, Java isn't the best language for implementing this sort of behavior as it requires that you alter the basic behavior of the operating system, something that Java doesn't do well at all.
Similar Threads
-
How to Change a JLabel in a Frame when a Button is clicked?
By fatabass in forum AWT / SwingReplies: 1Last Post: 02-11-2012, 03:07 AM -
returning a string when button clicked
By droidus in forum AWT / SwingReplies: 13Last Post: 02-02-2012, 03:50 AM -
form appear after ok button clicked
By sks in forum NetBeansReplies: 1Last Post: 06-01-2011, 08:50 AM -
show panel when button clicked
By Aggror in forum New To JavaReplies: 6Last Post: 10-14-2010, 04:05 PM -
[SOLVED] How to destroy a process when a button is clicked?
By pranav13 in forum AWT / SwingReplies: 11Last Post: 02-13-2009, 12:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks