I want to add JTextField. My JFrame contains a JButton and a JTextField . First, i have added the JButton, and it is working. But Jtextfield is not seen.
import java.awt.*;import java.awt.event.import java.awt.geom.;import javax.swing.;import java.sql.;import java.util.Random;import java.awt.geom.*;
public class DrawMap extends JFrame{//private JLabel LabelTitle,MapNo;Private JTextField MapField;private final JButton Load,Back,Logout;private final JPanel DrawPanel;private String UserName,Password,City;public boolean check;private int r,g,b;private int flag =0;private Shape shape;private final int w = 20;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.sql.*;
import java.util.Random;
import java.awt.geom.*;
public class DrawMap extends JFrame{
//private JLabel LabelTitle,MapNo;
private JTextField MapField;
private final JButton Load,Back,Logout;
private final JPanel DrawPanel;
private String UserName,Password,City;
public boolean check;
private int r,g,b;
private int flag =0;
private Shape shape;
private final int w = 20;
private final int h = 20;
private Object lastButtonPressed;
public DrawMap(String UserName,String Password,String City){
setTitle("Draw Map");
setSize(800,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Random p = new Random();
//r = p.nextInt(255);
//g = p.nextInt(255);
//b = p.nextInt(255);
this.UserName = UserName;
this.Password = Password;
this.City = City;
final Container contentPane = getContentPane();
JPanel buttonPanel = new JPanel();
JPanel buttonPanel1 = new JPanel();
JPanel TextField = new JPanel();
/*Draw = new JButton("Draw");
Shape = new JButton("Shape");*/
Load = new JButton("Load");
Back = new JButton("Back");
Logout = new JButton("Logout");
MapField = new JTextField();
//MapField.setBounds(130,130,100,30);
TextField.add(MapField);
//buttonPanel.add(Draw);
//buttonPanel.add(Shape);
buttonPanel1.add(Load);
buttonPanel1.add(Back);
buttonPanel1.add(Logout);
contentPane.add(buttonPanel, BorderLayout.NORTH);
contentPane.add(buttonPanel1, BorderLayout.SOUTH);
contentPane.add(TextField, BorderLayout.CENTER);
DrawPanel = new JPanel(){
};
contentPane.add(DrawPanel, BorderLayout.CENTER);
final ActionListener buttonPressed = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
lastButtonPressed = event.getSource();
}
};
//Draw.addActionListener(buttonPressed);
//Shape.addActionListener(buttonPressed);
Load.addActionListener(buttonPressed);
Back.addActionListener(buttonPressed);
Logout.addActionListener(buttonPressed);
contentPane.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if (lastButtonPressed == Load){
//shape = new Rectangle2D.Double(x, y, w, w);
//echo("Square",x,y);
//color = new Color(0,0,0);
}
else if (lastButtonPressed == Back){
UserHome ush = new UserHome(UserName,Password,City);
ush.setVisible(true);
DrawMap.this.setVisible(false);
}
else if (lastButtonPressed == Logout){
Login L = new Login();
L.setVisible(true);
DrawMap.this.setVisible(false);
}
//DrawPanel.repaint();
}
});
}
}
JPanel
TextField
). What is your error? Does this even compile? – achAmháinPrivate JTextField MapField ;
is that a typo? – MadProgrammerprivate
inPrivate JTextField MapField;?
starts with Upper case P. How is this even compiled? – Yohannes Gebremariam