Example usage for javax.swing JLabel JLabel

List of usage examples for javax.swing JLabel JLabel

Introduction

In this page you can find the example usage for javax.swing JLabel JLabel.

Prototype

public JLabel(Icon image, int horizontalAlignment) 

Source Link

Document

Creates a JLabel instance with the specified image and horizontal alignment.

Usage

From source file:JColorChooserSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("JColorChooser Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JLabel label = new JLabel("www.java2s.com", JLabel.CENTER);
    label.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48));
    frame.add(label, BorderLayout.SOUTH);

    final JColorChooser colorChooser = new JColorChooser(label.getBackground());
    colorChooser.setBorder(BorderFactory.createTitledBorder("Pick Foreground Color"));

    frame.add(colorChooser, BorderLayout.CENTER);

    frame.pack();/*from  w  ww .  jav  a2  s  . co  m*/
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String[] a) {

    final JColorChooser colorChooser = new JColorChooser();
    final JLabel previewLabel = new JLabel("I Love Swing", JLabel.CENTER);
    previewLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48));
    previewLabel.setSize(previewLabel.getPreferredSize());
    previewLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 1, 0));
    colorChooser.setPreviewPanel(previewLabel);

    ActionListener okActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("OK Button");
            System.out.println(colorChooser.getColor());
        }//from  w ww  . j a v a  2 s  .  c  o m
    };

    ActionListener cancelActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Cancel Button");
        }
    };

    final JDialog dialog = JColorChooser.createDialog(null, "Change Button Background", true, colorChooser,
            okActionListener, cancelActionListener);

    dialog.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setLayout(new GridLayout(2, 2));
    JLabel label = new JLabel("User Name:", SwingConstants.RIGHT);
    JLabel label2 = new JLabel("Password:", SwingConstants.RIGHT);
    JTextField userNameField = new JTextField(20);
    JPasswordField passwordField = new JPasswordField();
    frame.add(label);/* w  ww. j a  va  2 s . c o  m*/
    frame.add(userNameField);
    frame.add(label2);
    frame.add(passwordField);
    frame.setSize(200, 70);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JTextField firstField = new JTextField(10);
    JTextField middleField = new JTextField(10);
    JTextField lastField = new JTextField(10);

    JLabel firstLabel = new JLabel("First Name", JLabel.RIGHT);
    firstLabel.setDisplayedMnemonic('F');
    firstLabel.setLabelFor(firstField);/*w  w w . jav  a 2  s  . co  m*/

    JLabel middleLabel = new JLabel("Middle Initial", JLabel.RIGHT);
    middleLabel.setDisplayedMnemonic('I');
    middleLabel.setDisplayedMnemonicIndex(7);
    middleLabel.setLabelFor(middleField);

    JLabel lastLabel = new JLabel("Last Name", JLabel.RIGHT);
    lastLabel.setDisplayedMnemonic('L');
    lastLabel.setLabelFor(lastField);

    JPanel p = new JPanel();
    p.setLayout(new GridLayout(3, 2, 5, 5));
    p.add(firstLabel);
    p.add(firstField);
    p.add(middleLabel);
    p.add(middleField);
    p.add(lastLabel);
    p.add(lastField);

    JFrame f = new JFrame("MnemonicLabels");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(p);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setLayout(new GridLayout(2, 2));
    JLabel label = new JLabel("User Name:", SwingConstants.RIGHT);
    JLabel label2 = new JLabel("Password:", SwingConstants.RIGHT);
    JTextField userNameField = new JTextField(20);

    JPasswordField passwordField = new JPasswordField();
    passwordField.setEchoChar('#');

    frame.add(label);//from  ww  w.  j av a2 s  .c om
    frame.add(userNameField);
    frame.add(label2);
    frame.add(passwordField);
    frame.setSize(200, 70);
    frame.setVisible(true);
}

From source file:WindowEventHandler.java

public static void main(String[] args) {
    JFrame frame = new JFrame("Swing Frame");

    JLabel label = new JLabel("This is a Swing frame", JLabel.CENTER);

    frame.add(label);/*w w w .  j  av a  2 s.co m*/

    frame.addWindowListener(new WindowEventHandler());
    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    frame.setSize(350, 200); // width=350, height=200
    frame.setVisible(true); // Display the frame
}

From source file:Main.java

public static void main(String... args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel contentPane = new JPanel();
    contentPane.setOpaque(true);// w w w  .  j  a  va 2  s.c  o m
    contentPane.setBackground(Color.WHITE);
    contentPane.setLayout(null);

    JLabel label = new JLabel("This JPanel uses Absolute Positioning", JLabel.CENTER);
    label.setSize(300, 30);
    label.setLocation(5, 5);

    JButton button = new JButton("USELESS");
    button.setSize(100, 30);
    button.setLocation(95, 45);

    contentPane.add(label);
    contentPane.add(button);

    frame.setContentPane(contentPane);
    frame.setSize(310, 125);
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
}

From source file:Borders.java

public static void main(String[] args) {
    JFrame frame = new JFrame("Borders");

    int center = SwingConstants.CENTER;
    JLabel labelOne = new JLabel("raised BevelBorder", center);
    labelOne.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
    JLabel labelTwo = new JLabel("EtchedBorder", center);
    labelTwo.setBorder(BorderFactory.createEtchedBorder());
    JLabel labelThree = new JLabel("MatteBorder", center);
    labelThree.setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, Color.pink));
    JLabel labelFour = new JLabel("TitledBorder", center);
    Border etch = BorderFactory.createEtchedBorder();
    labelFour.setBorder(BorderFactory.createTitledBorder(etch, "Title"));
    JLabel labelFive = new JLabel("TitledBorder", center);
    Border low = BorderFactory.createLoweredBevelBorder();
    labelFive//from   ww w. j  a v  a  2  s  . com
            .setBorder(BorderFactory.createTitledBorder(low, "Title", TitledBorder.RIGHT, TitledBorder.BOTTOM));
    JLabel labelSix = new JLabel("CompoundBorder", center);
    Border one = BorderFactory.createEtchedBorder();
    Border two = BorderFactory.createMatteBorder(4, 4, 4, 4, Color.blue);
    labelSix.setBorder(BorderFactory.createCompoundBorder(one, two));

    frame.setLayout(new GridLayout(3, 2));
    frame.add(labelOne);
    frame.add(labelTwo);
    frame.add(labelThree);
    frame.add(labelFour);
    frame.add(labelFive);
    frame.add(labelSix);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setLayout(new BorderLayout());
    frame.add(new JLabel("Hit Escape to exit full screen", JLabel.CENTER), BorderLayout.CENTER);

    frame.setSize(300, 300);// w w w  .ja va2s . c  om

    KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke("ESCAPE");
    Action escapeAction = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                    .setFullScreenWindow(null);
        }
    };

    frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE");
    frame.getRootPane().getActionMap().put("ESCAPE", escapeAction);

    GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(frame);

}

From source file:JPasswordFieldTest.java

public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("JTextField Test");
    frame.setLayout(new GridLayout(2, 2));
    JLabel label = new JLabel("User Name:", SwingConstants.RIGHT);
    JLabel label2 = new JLabel("Password:", SwingConstants.RIGHT);
    JTextField userNameField = new JTextField(20);
    JPasswordField passwordField = new JPasswordField();
    frame.add(label);/*w w  w .  j ava  2  s  .c o  m*/
    frame.add(userNameField);
    frame.add(label2);
    frame.add(passwordField);
    frame.setSize(200, 70);
    frame.setVisible(true);
}