|
-
November 5th, 2011, 12:26 PM
#1
Beginner Programming Problem
Hey so I've been working on a lab for my intro to software developing class and have been trying to get my program to work for several days now. It's a real basic program, im sure someone will be able to figure out the issue im having. Hopefully once I learn what I'm doing I'll be able to return the favor, but until then thanks, heres the code.
There are no errors indicated by eclipse but i want to use the jtextfield to input the number of the month AND then when you press enter i want the same textfield to display the month...I am supposed to do this using data.setText() as i did, but the problem happens after i enter the month number....the program freezes heres my code
package lab8;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class GUIMonthNames extends JFrame implements ActionListener {
private JLabel instructions;
private JTextField data;
public GUIMonthNames() {
super("Month Names: GUI version");
setSize(400,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container myPane = getContentPane();
myPane.setLayout(new FlowLayout());
instructions = new JLabel("Enter a number (1-12) below");
data = new JTextField(20);
add(data);
data.addActionListener(this);
add(instructions);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
int monthNumber = new Integer(data.getText());
Scanner keyboard = new Scanner(System.in);
monthNumber = keyboard.nextInt();
final String MONTH_TABLE = "January February March April May June " +
"July August SeptemberOctober November December ";
int start = (monthNumber - 1) * 9;
int stop = start + 9;
String monthName = MONTH_TABLE.substring(start, stop);
data.setText(monthNumber + " is '" + monthName.trim() + "'.");
}
public static void main(String[] args) {
new GUIMonthNames();
}
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|