Results 1 to 5 of 5
Thread: Problem with JLabel!
- 04-02-2011, 03:16 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 25
- Rep Power
- 0
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] } }
-
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?
- 04-02-2011, 03:28 PM #3
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
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 is from 10,upto 10+500l.setBounds(10, 50, 500, 30);
where e is from 20.
thats why you can't see the labelDon't Forget to try yourself before asking others help.....
Press REP, if you find their advices/solutions effective.
- 04-02-2011, 03:34 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 25
- Rep Power
- 0
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
-
A problem with JLabel!
By niklas in forum New To JavaReplies: 0Last Post: 12-14-2010, 08:47 PM -
problem with JLabel.setText();
By nonabhai in forum AWT / SwingReplies: 5Last Post: 10-09-2010, 04:44 AM -
Jlabel update problem
By fantasyme in forum AWT / SwingReplies: 3Last Post: 04-14-2010, 05:10 AM -
Swing JLabel Problem
By g123456 in forum New To JavaReplies: 2Last Post: 02-15-2010, 02:17 AM -
JLabel + GUI problem
By tonyelaltaico in forum Java AppletsReplies: 5Last Post: 02-03-2009, 01:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks