Results 1 to 7 of 7
Thread: Draw TextField
- 07-17-2010, 12:13 AM #1
Draw TextField
Is it possible to draw a JTextField on Graphics g? If yes could you please tell me how, and if not then could you give me suggestions what could I do?
Second question.. How can I add a keylistener to this:
and I use this class on :Java Code:package org.swc.loginreg; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.TextField; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import org.swc.App.Main; import org.swc.Misc.Misc; public class drawLoginPanel{ public static TextField usernameField; public static void drawTehPanel(Graphics g){ Main main = new Main(); g.setColor(Color.white); g.fillRoundRect(242, 110, 300, 250, 20, 20); g.setColor(Color.GRAY); g.setFont(new Font("Calibri", Font.BOLD, 24)); g.drawString("Login", 362, 140); g.setFont(new Font("Calibri", Font.PLAIN, 12)); g.drawString("Username: ", 262, 180); g.setColor(Color.BLACK); g.drawRect(328, 169, 200, 14); g.drawString(main.username, 330, 181); } }
and if you need the misc class:Java Code:package org.swc.App; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.UIManager; import org.swc.Misc.Misc; import org.swc.loginreg.*; public class Main implements ActionListener{ public String frameTitle = "Street Wars Client"; public JFrame frame; public boolean loggedIn = false; public String username = "N/A"; public JPanel gamePanel; public static void main(String args[]) { Main main = new Main(); try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { } main.initUI(); } public void initUI() { frame = new JFrame(frameTitle); frame.setLayout(new BorderLayout()); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gamePanel = new JPanel () { private static final long serialVersionUID = 1L; public void paintComponent(Graphics g) { g.setColor(Color.BLACK); g.fillRect(0, 0, 765, 503); if(loggedIn == false){ drawLoginPanel.drawTehPanel(g); } } }; JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); String[] fileButtons = new String[] { "Exit" }; for (String name : fileButtons) { JMenuItem menuItem = new JMenuItem(name); if (name.equalsIgnoreCase("-")) fileMenu.addSeparator(); else { menuItem.addActionListener(this); fileMenu.add(menuItem); } } menuBar.add(fileMenu); gamePanel.setLayout(new BorderLayout()); gamePanel.setPreferredSize(new Dimension(765, 503)); frame.getContentPane().add(gamePanel, BorderLayout.CENTER); frame.getContentPane().add(menuBar, BorderLayout.NORTH); frame.pack(); frame.setVisible(true); } @Override public void actionPerformed(ActionEvent evt) { String cmd = evt.getActionCommand(); //misc.debug(cmd); if (cmd != null){ if(cmd == "Exit"){ int i1 = 0; if((i1 = JOptionPane.showConfirmDialog(frame, "Are you sure that you want to exit Street Wars?", "Exit", i1)) == 0) System.exit(0); return; } }else{ Misc.debug("CMD is null !"); } } }
Java Code:package org.swc.Misc; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; public class Misc { private static boolean debug = true; public static void sM(String message){ System.out.println(message); } public static void debug(final String message){ if(debug == true) System.err.println("DEBUG :: " + message); } }
-
1) What do you mean draw a JTextField? You can draw an image of a JTextField using the Graphics object, but that's all it is, an image, not a functioning JTextField. What are you trying to do with it? What's its purpose?
2) Can you give more detail on your key listener problem? You have asked a broad question and posted a lot of code. It would help us out a lot by telling us more as well as what you've tried, how it doesn't work, etc...
Luck.
- 07-17-2010, 12:45 AM #3
I am going to try and make my first multiplayer game, and right now I am trying to add a login system, but I will make a normal panel instead, so that's ok now.
2) Can you give more detail on your key listener problem? You have asked a broad question and posted a lot of code. It would help us out a lot by telling us more as well as what you've tried, how it doesn't work, etc...
Luck.I did for my gamePanel.Java Code:gamePanel.addKeyListener(this);
And then for the KeyListener I did this:
But when I run my app, and when I click something nothing happens.Java Code:@Override public void keyPressed(KeyEvent e) { Misc.debug("Key: " + e.getKeyCode()); } @Override public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub }
EDIT: SOLVED, I've changed gamePanel.addKeyListener(this); to frame.addKeyListener(this);
Thanks for your help !Last edited by PhQ; 07-17-2010 at 12:49 AM.
-
I see what you are trying to do with drawing a text field and I just wanted to mention that you should know that your code commits a cardinal Swing sin in that you're trying to put program logic in the paint portion of the code. As you've already suggested you're much better to create a JPanel and display it in a JDialog or JOptionPane.
Luck.
- 07-17-2010, 01:13 AM #5
-
What do you mean by "draw"? You can add components to a JPanel, sure, and doing so is one of the foundation techniques of Swing, one of the reasons why gaining a thorough knowledge of the layout managers is key. And you can "draw" on a JPanel within its paintComponent override, but you're not going to want to draw components on a JPanel, you'll want to add them. Also, what do you mean by "little box"?
- 07-17-2010, 01:30 AM #7
Similar Threads
-
how to get integer value from textfield
By sandysm in forum Advanced JavaReplies: 4Last Post: 04-16-2010, 01:11 PM -
TextField Example
By Java Tip in forum SWTReplies: 0Last Post: 07-25-2008, 02:21 PM -
JSP - getting value from a textfield
By Java Tip in forum Java TipReplies: 0Last Post: 12-01-2007, 08:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks