Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JLabel

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default JLabel

    Hello

    I have a problem... I can't get my JLabel to update.. I have tried Repaint(), revaldiate(), but I think it is something wrong with my connection between my buttons and the JLabel.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    class Vindu extends JFrame{
     
    	//private JTextField tekstFelt = new JTextField(20);
     
     
    	String teksten = "Dette er min tekst.";
    	public JLabel tekst = new JLabel(teksten);
    	int valg;
     
    	public Vindu(){
    	}
     
    	public Vindu(String tittel){
    		setTitle(tittel);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setLayout(new FlowLayout());
     
    		Font SS = new Font("SansSerif", Font.BOLD, 12);
    		Font S = new Font("Serif", Font.BOLD, 12);
    		Font M = new Font("Monospaced", Font.BOLD, 12);
    		Font D = new Font("Dialog", Font.BOLD, 12);
     
     
    		JButton SansSerif = new JButton("SansSerif");  // lager knappen
    		SansSerif.setFont(SS);
    		add(SansSerif);  // legger knappen i beholderen
     
        	JButton Serif = new JButton("Serif");
        	Serif.setFont(S);
        	add(Serif);
     
        	JButton Monospaced = new JButton("Monospaced");
        	Monospaced.setFont(M);
        	add(Monospaced);
     
        	JButton Dialog1 = new JButton("Dialog");
        	Dialog1.setFont(D);
        	add(Dialog1);
        	//pack();
     
        	Knappelytter knappelytteren = new Knappelytter(); // lager en lytter
    		SansSerif.addActionListener(knappelytteren); // knytter lytteren til knappen
    		Serif.addActionListener(knappelytteren);
    		Monospaced.addActionListener(knappelytteren);
    		Dialog1.addActionListener(knappelytteren);
     
    		add(tekst);
    		pack();
     
    	}
    }
     
     
    class Knappelytter extends Vindu implements ActionListener{
      	public void actionPerformed(ActionEvent hendelse){
     
    		JButton valgtKnapp = (JButton) hendelse.getSource();
    		String knappeValg = valgtKnapp.getActionCommand(); // getText();
     
    		String font;
    		Font skrift;
    		if(knappeValg.equals("SansSerif")){
    			skrift = new Font("SansSerif", Font.BOLD, 20);
    			tekst.setFont(skrift);
    			tekst.repaint();
    			tekst.revalidate();
    			add(tekst);
    			System.out.println(knappeValg);
    			//font SansSerif
    		}else if(knappeValg.equals("Serif")){
    			skrift = new Font("Serif", Font.BOLD, 20);
    			tekst.setFont(skrift);
    			System.out.println(knappeValg);
    			// font Serif
    		}else if(knappeValg.equals("Monospaced")){
    			skrift = new Font("Monospaced", Font.BOLD, 20);
    			tekst.setFont(skrift);
    			System.out.println(knappeValg);
    			// font Monospaced
    		}else if(knappeValg.equals("Dialog")){
    			skrift = new Font("Dialog", Font.BOLD, 20);
    			tekst.setFont(skrift);
    			System.out.println(knappeValg);
    			// font Dialog
    		}
      	}
    }
     
     
     
     
    class Oppgave1{
    	public static void main(String[]args){
     
    	Vindu etVindu = new Vindu("Et vindu med en knapp");
        	etVindu.setVisible(true);
     
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JLabel

    Can you explain what labels are the problem? How to you test the code to show the problem?

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JLabel

    I figured it out

Similar Threads

  1. JLabel image?
    By frozen java in forum AWT / Java Swing
    Replies: 2
    Last Post: September 28th, 2011, 07:44 AM
  2. Everything but JLabel is displaying
    By Jacksontbh in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 3rd, 2011, 10:30 PM
  3. A problem with JLabel!
    By niklas in forum AWT / Java Swing
    Replies: 5
    Last Post: December 19th, 2010, 05:51 AM
  4. A problem with JLabel!
    By niklas in forum Member Introductions
    Replies: 1
    Last Post: December 14th, 2010, 06:03 PM
  5. how to output using JLabel?
    By qaromi in forum AWT / Java Swing
    Replies: 1
    Last Post: August 30th, 2009, 02:09 PM