LabelFor.java Source code

Java tutorial

Introduction

Here is the source code for LabelFor.java

Source

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.KeyEvent;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class LabelFor {
    public static void main(String args[]) {
        JFrame f = new JFrame("LabelFor Sample");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = f.getContentPane();
        JLabel label = new JLabel("Username");
        JTextField textField = new JTextField();
        label.setDisplayedMnemonic(KeyEvent.VK_U);
        label.setLabelFor(textField);
        Container box = Box.createHorizontalBox();
        box.add(label);
        box.add(textField);
        content.add(box, BorderLayout.NORTH);
        content.add(new JButton("Submit"), BorderLayout.SOUTH);
        f.setSize(300, 200);
        f.setVisible(true);
    }
}