Results 1 to 13 of 13
Thread: problem with GUI
- 07-22-2010, 11:01 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 6
- Rep Power
- 0
problem with GUI
Dear people,
I have a problem with a certain problem in Java.
A GUI is given and the assignment is as follows.
A user types an appointment, f.e. with the dentist and write:
""dentist 10.00" in a textfield. The program must save the information in an array. But how can I put save the appointmens in the array? Here are the pictures:
Now is my Java code:
Java Code:import java.awt.*; import javax.swing.*; import java.awt.event.*; public class AgendaFrame extends JFrame implements ActionListener{ private JButton addButton= new JButton("VoegToe: "); private JTextField textfield= new JTextField(); private JButton searchButton= new JButton("Afspraaknummer: "); private JPanel panel = new JPanel(new GridLayout(2,2)); private String[] appointments= new String[10]; private int amount= 0; public AgendaFrame(){ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100,100,350,100); Container c = getContentPane(); panel.add(addButton); panel.add(textfield); panel.add(searchButton); c.add(panel); setVisible(true); } public void actionPerformed(ActionEvent e) { if(e.getSource() == addButton) { String n = textfield.getText(); if(amount< appointments.length) { appointments[amount]= n; amount++; textfield.setText(""); } } } public static void main(String[] args){ new AgendaFrame(); } }Last edited by vermicelli; 07-25-2010 at 08:49 AM.
- 07-23-2010, 12:05 AM #2
Does it just not work?
What is aantal? You don't define it anywhere?:rolleyes: ~ Sno ~ :rolleyes:
'-~ B.S. Computer Science ~-'
- 07-23-2010, 12:13 AM #3
Your code does not compile. Please correct the compile errors and respost the code
- 07-24-2010, 04:06 PM #4
Member
- Join Date
- Jul 2010
- Posts
- 6
- Rep Power
- 0
First of all, thanks for your response.
My bad, I have changed aantal into amount.
Well, I have made an array called "appointments". And when someone types his appointment and the time in the textfield, and click on "VoegToe"(it means to add in Dutch), then the information should be saved into the array, and the textfield is supposed to be empty again. But as you see, when I run the program the appointment is not being saved into the array appointments, and the textfield wont go blank. So I wonder what have I done incorrect?Last edited by vermicelli; 07-24-2010 at 04:09 PM.
- 07-24-2010, 04:37 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
You haven't added an ActionListener to your addButton so you can click all you want but nothing is going to happen. Implementing an actionPerformed( ... ) method is not enough. Your frame should implement the ActionListener interface and it should be added as such to the button.
kind regards,
Jos
ps. there are more errors in the code you've shown us ...
- 07-24-2010, 05:54 PM #6
Member
- Join Date
- Jul 2010
- Posts
- 6
- Rep Power
- 0
ah! I've totally forgotten it! Thanks, that is a (big) step forwards :) But still have to figure out the other requirements
- 07-24-2010, 06:02 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
- 07-24-2010, 10:25 PM #8
Member
- Join Date
- Jul 2010
- Posts
- 6
- Rep Power
- 0
why not? Because what I mean is that the the information(text.getText) must be saved into position i of the array appointments, as far as the number of appointments are smaller then the arraylength. So how can I save the information (text.getText) into the array?
Last edited by vermicelli; 07-24-2010 at 10:45 PM.
-
There are many problems with it, perhaps too many to enumerate, but I can try. What is amount.length for one? And why would you want to take one unchanging String, text, and shove it into the elements of an array repeatedly with each button push, over-writing all array elements placed previously? Again, it just doesn't make any sense.
- 07-25-2010, 08:35 AM #10
Member
- Join Date
- Jul 2010
- Posts
- 6
- Rep Power
- 0
ok, I have changed the ActionPerformed code, now it still wont work. Is it possible to set the textfield on blank by the way? Because it just doesnt want to be empty after putting elements in the array
- 07-25-2010, 08:40 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
- 07-25-2010, 09:29 AM #12
Member
- Join Date
- Jul 2010
- Posts
- 6
- Rep Power
- 0
-


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks