Results 1 to 5 of 5
  1. #1
    louboulos is offline Member
    Join Date
    Mar 2011
    Posts
    25
    Rep Power
    0

    Default Problem with JLabel!

    Hello! My problem is that i can't get the Jlabel to show no matter what i do!
    I have highlighted the JLabel code. Here is the whole code:

    Java Code:
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JDesktopPane;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.JTextPane;
    import javax.swing.border.BevelBorder;
    
    public class Frame extends JFrame {
    
    
       
    
        public  Frame() {
          
        
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Encrypter/Decrypter");
        setBounds(50,50,530,200);
        setVisible(true);
        setResizable(false);
    
        }
        
        public static void main(String[] args) {
            
          
            JFrame f = new Frame();
            JDesktopPane desktop = new JDesktopPane();
            f.setContentPane(desktop);
            JTextField t = new JTextField("Erase This And Enter Your Number Here: ");
            t.setBounds(10, 10, 500, 30);
            desktop.add(t);
            JButton e = new JButton("Encrypt");
            e.setBounds(20,50,200,50);
            desktop.add(e);
            JButton d = new JButton("Decrypt");
            d.setBounds(300, 50, 200, 50);
            desktop.add(d);
            [COLOR="Red"]JLabel l =new JLabel();
            l.setBounds(10, 50, 500, 30);
            l.setText("....");
            l.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
            desktop.add(l);
            l.setVisible(true);[/COLOR]
            
            
            
            }
    
    
       
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It's there, but it's partially covered by your buttons.

    But more importantly: Why are you using JDesktopPane for this application as it does not appear to be a good fit? Why are you using null layouts and absolute positioning when use of layout managers would make your code much easier to create, update, and maintain?

  3. #3
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    Your code is working. Just change the position of "Encrypt button."
    JButton e = new JButton("Encrypt");
    e.setBounds(20,50,200,50);
    desktop.add(e);
    JButton d = new JButton("Decrypt");
    d.setBounds(300, 50, 200, 50);
    desktop.add(d);
    JLabel l =new JLabel();
    l.setBounds(10, 50, 500, 30);
    l.setText("....");
    l.setBorder(BorderFactory.createBevelBorder(BevelB order.LOWERED));
    desktop.add(l);
    l.setVisible(true);
    JButton e = new JButton("Encrypt");
    e.setBounds(20,50,200,50);
    l.setBounds(10, 50, 500, 30);
    l is from 10,upto 10+500
    where e is from 20.
    thats why you can't see the label
    Don't Forget to try yourself before asking others help.....
    Press REP, if you find their advices/solutions effective.

  4. #4
    louboulos is offline Member
    Join Date
    Mar 2011
    Posts
    25
    Rep Power
    0

    Default

    thank you guys! I am not using the layout manager and the design mode cause i want to practice first and understand the positioning etc etc.... I am new to Java :P

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by louboulos View Post
    thank you guys! I am not using the layout manager and the design mode cause i want to practice first and understand the positioning etc etc.... I am new to Java :P
    I am not recommending that you use NetBeans "design mode" but rather that you learn to code these things correctly by hand. DesktopPane is used for showing JInternalFrames, and should not be used for what you are doing.

    For instance, I'd put everything in a JPanel that uses BorderLayout. I'd put the JButtons in a JPanel that uses GridLayout. For example:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class JavaForum41721b extends JPanel {
       private JTextField field = new JTextField("Erase This And Enter Your Number Here: ", 30);
       private JButton encryptBtn = new JButton("Encrypt");
       private JButton decryptBtn = new JButton("Decrypt");
       private JTextArea displayArea = new JTextArea(10, 30);
       
       public JavaForum41721b() {
          field.addFocusListener(new FocusAdapter() {
             @Override
             public void focusGained(FocusEvent e) {
                JTextField source = (JTextField) e.getSource();
                source.selectAll();
             }
          });
          
          JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 10, 0));
          buttonPanel.add(encryptBtn);
          buttonPanel.add(decryptBtn);
          displayArea.setText("... ");
          displayArea.setEditable(false);
          displayArea.setFocusable(false);
    
          int eb = 15;
          setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
          setLayout(new BorderLayout(10, 10));
          add(field, BorderLayout.NORTH);
          add(buttonPanel, BorderLayout.SOUTH);
          add(new JScrollPane(displayArea), BorderLayout.CENTER);
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("JavaForum41721b");
          frame.getContentPane().add(new JavaForum41721b());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    Last edited by Fubarable; 04-02-2011 at 03:50 PM.

Similar Threads

  1. A problem with JLabel!
    By niklas in forum New To Java
    Replies: 0
    Last Post: 12-14-2010, 08:47 PM
  2. problem with JLabel.setText();
    By nonabhai in forum AWT / Swing
    Replies: 5
    Last Post: 10-09-2010, 04:44 AM
  3. Jlabel update problem
    By fantasyme in forum AWT / Swing
    Replies: 3
    Last Post: 04-14-2010, 05:10 AM
  4. Swing JLabel Problem
    By g123456 in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 02:17 AM
  5. JLabel + GUI problem
    By tonyelaltaico in forum Java Applets
    Replies: 5
    Last Post: 02-03-2009, 01:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •