Results 1 to 2 of 2
  1. #1
    tomovic is offline Member
    Join Date
    Jan 2014
    Posts
    1
    Rep Power
    0

    Default JLable provide a button

    My goal is: I click on a button and it comes a JLabel. It would be better with. Claer to delete everything and re-bellow to add would be. This Will not work anyway.
    I come so far that my "line" is displayed in the console.
    PHP Code:
                                                      package paketmann;
     import java.awt.BorderLayout;
     import java.awt.event.MouseAdapter;
     import java.awt.event.MouseEvent;
     import javax.swing.BorderFactory;
     import javax.swing.ImageIcon;
     import javax.swing.JButton;
     import javax.swing.JFrame;
     import javax.swing.JLabel;
     import javax.swing.JOptionPane;
     import javax.swing.JPanel;
     import javax.swing.Box;
     import javax.swing.BoxLayout;
     import javax.swing.JFrame;
     import javax.swing.JButton;
     import java.awt.Container;
     import javax.swing.GroupLayout;
     import javax.swing.*;
     import java.awt.*;
     import java.io.FileReader;
     import java.io.FileNotFoundException;
     import java.io.IOException;
     import java.io.BufferedReader;
     import java.awt.*;
     import javax.swing.*;
     
     
     
     public class Tomclass extends JPanel {
         private JButton firstButton;
         private JLabel lbl1;
         
         class LblListener extends MouseAdapter {
             @Override
             public void mouseClicked(MouseEvent e) {
                 Object source = e.getSource();
    
                 ///////////////////////// ??????????????????????
                 if (source == lbl1) {
                     JOptionPane.showMessageDialog(null,
                             "Es wurde auf Label1 geklickt");
                      
                 }
                 
                 if (source == firstButton) {
                     JOptionPane.showMessageDialog(null,
                             "Es wurde auf Button geklickt");
                     new Tomclass(1);
                 }
             }
         }
      
         public Tomclass(int fa) {
                  
             super(null);
             MouseAdapter listener = new LblListener(); 
            
             lbl1 = new JLabel("blalbalbla");             
             lbl1.setBounds(10, 10, 222, 22);
           //  add(lbl1);
             lbl1.addMouseListener(listener); 
            
              firstButton = new JButton(new ImageIcon("c:/1.jpg"));
          
             firstButton.setBounds(50, 40, 80, 20);
             add(firstButton);
             firstButton.addMouseListener(listener);
             
          
             if(fa == 1)
             {
                 
                 System.out.println("line");
                 add(lbl1);   //[B] <--- create a Lbl1.When I press a Button[/B]
             }
                    
         }
             @Override
             public Dimension getPreferredSize(){
                 //Hard coded preferred size - but you'd probably want 
                 //to calculate it based on the panel's content 
                 return new Dimension(500, 300);
                 
         }
         public static void main(String[] args) {
             JFrame frame = new JFrame("ButtonTest");
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
             frame.setContentPane(new Tomclass(0));     
             frame.pack();
             frame.setVisible(true);
         }
     }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default Re: JLable provide a button

    Why can't you just disable the JButton?

    Alternatively, place a JLabel right next to the JButton (perhaps in their own panel). Set the JLabel's visibility to false. When you click the JButton, set the JButton's visibility to false and the JLabel's visibility to true.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. how to provide language support?
    By Ashi in forum New To Java
    Replies: 7
    Last Post: 01-14-2012, 12:45 PM
  2. how provide the method ?
    By pruton in forum New To Java
    Replies: 4
    Last Post: 06-13-2010, 04:41 PM
  3. Please provide some help
    By saurabh_87 in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 08:38 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
  •