-
simple GUI implementation problem
I have to research GUI components to provide an interface for a university OOP project. I am halfway through chapter 11 of the Deitel "Java how to Program" 6th edition and have tried to compile and run the code shown below but get 4 errors which appear to relate to the format method of String:
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
public class TextFieldFrame extends JFrame
{
private JTextField textField1;
private JTextField textField2;
private JTextField textField3;
private JPasswordField passwordField;
public TextFieldFrame(){
super("Testing JTextField and JPasswordField");
setLayout(new FlowLayout());
textField1 = new JTextField(10);
add(textField1);
textField2 = new JTextField("Enter text here");
add(textField2);
textField3 = new JTextField("Uneditable text field", 21);
textField3.setEditable(false);
add(textField3);
passwordField = new JPasswordField("Hidden text");
add(passwordField);
TextFieldHandler handler = new TextFieldHandler();
textField1.addActionListener(handler);
textField2.addActionListener(handler);
textField3.addActionListener(handler);
passwordField.addActionListener(handler);
}
private class TextFieldHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
String string = "";
if (event.getSource()==textField1)
string = String.format("textField1: %s", event.getActionCommand());
else if (event.getSource()==textField2)
string = String.format("textField2: %s", event.getActionCommand());
else if (event.getSource()==textField3)
string = String.format("textField3: %s", event.getActionCommand());
else if (event.getSource()==passwordField)
string = String.format("passwordField: %s", new String(passwordField.getPassword()));
JOptionPane.showMessageDialog(null, string);
}
}
}
import javax.swing.JFrame;
public class TextFieldTest
{
public static void main(String[] args)
{
TextFieldFrame textFieldFrame = new TextFieldFrame();
textFieldFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textFieldFrame.setSize(325,100);
textFieldFrame.setVisible(true);
}
}
The errors are as follows:
---------- Compile ----------
TextFieldFrame.java:45: cannot resolve symbol
symbol : method format (java.lang.String,java.lang.String)
location: class java.lang.String
string = String.format("textField1: %s", event.getActionCommand());
^
TextFieldFrame.java:47: cannot resolve symbol
symbol : method format (java.lang.String,java.lang.String)
location: class java.lang.String
string = String.format("textField2: %s", event.getActionCommand());
^
TextFieldFrame.java:49: cannot resolve symbol
symbol : method format (java.lang.String,java.lang.String)
location: class java.lang.String
string = String.format("textField3: %s", event.getActionCommand());
^
TextFieldFrame.java:51: cannot resolve symbol
symbol : method format (java.lang.String,java.lang.String)
location: class java.lang.String
string = String.format("passwordField: %s", new String(passwordField.getPassword()));
^
4 errors
Output completed (0 sec consumed) - Normal Termination
Could anyone please tell me how to correct the code to get the application running properly?
Thanks.
Last edited by Stevo27; 11-26-2005 at 07:09 PM.
-
Problem solved
Figured out the solution - I had the compile tool set to Java 1.4 rather than 1.5. Changed it and it works fine.
Thanks anyway.
Similar Threads
-
By Jim Pragit in forum .NET
Replies: 5
Last Post: 08-10-2005, 07:16 AM
-
Replies: 1
Last Post: 06-20-2002, 11:04 AM
-
Replies: 4
Last Post: 04-13-2001, 03:31 AM
-
By Eric in forum Database
Replies: 1
Last Post: 11-10-2000, 02:05 AM
-
By Gary Thompson in forum authorevents.kurata
Replies: 1
Last Post: 04-20-2000, 08:13 PM
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
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks