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) 

Source Link

Document

Creates a JLabel instance with the specified image.

Usage

From source file:DropModeON_OR_INSERT.java

public static void main(String[] args) {
    JPanel north = new JPanel();
    north.add(new JLabel("Drag from here:"));
    JTextField field = new JTextField(10);
    field.setDragEnabled(true);//  w w w.ja  va2 s  . c  o  m
    north.add(field);

    final DefaultListModel listModel = new DefaultListModel();
    listModel.addElement("first");
    listModel.addElement("second");
    final JList list = new JList(listModel);
    list.setDragEnabled(true);

    list.setTransferHandler(new TransferHandler() {
        public boolean canImport(TransferHandler.TransferSupport support) {
            if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                return false;
            }
            JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
            if (dl.getIndex() == -1) {
                return false;
            } else {
                return true;
            }
        }

        public boolean importData(TransferHandler.TransferSupport support) {
            if (!canImport(support)) {
                return false;
            }

            Transferable transferable = support.getTransferable();
            String data;
            try {
                data = (String) transferable.getTransferData(DataFlavor.stringFlavor);
            } catch (Exception e) {
                return false;
            }

            JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
            int index = dl.getIndex();
            if (dl.isInsert()) {
                listModel.add(index, data);
            } else {
                listModel.set(index, data);
            }

            // Scroll to display the element that was dropped
            Rectangle r = list.getCellBounds(index, index);
            list.scrollRectToVisible(r);
            return true;
        }
    });
    JScrollPane center = new JScrollPane();
    center.setViewportView(list);

    list.setDropMode(DropMode.ON_OR_INSERT);
    JPanel cp = new JPanel();
    cp.setLayout(new BorderLayout());
    cp.add(north, BorderLayout.NORTH);
    cp.add(center, BorderLayout.CENTER);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(cp);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JButton btnA = new JButton("A");
    JButton btnB = new JButton("B");

    btnA.setPreferredSize(new Dimension(50, 25));
    btnB.setPreferredSize(new Dimension(100, 25));

    JPanel btnAPanel = new JPanel();
    JPanel btnBPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    btnAPanel.add(btnA);/*  w  w w  .ja va 2 s  .  c o m*/
    btnBPanel.add(btnB);

    JPanel topPanel = new JPanel(new GridLayout(1, 0));
    topPanel.add(new JLabel("hi"));
    topPanel.add(btnAPanel);
    topPanel.add(btnBPanel);

    JPanel mainPanel = new JPanel(new BorderLayout());
    mainPanel.add(topPanel, BorderLayout.NORTH);

    mainPanel.setPreferredSize(new Dimension(400, 300));

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(mainPanel);
    frame.pack();
    frame.setVisible(true);
}

From source file:JCheckBoxTest.java

public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("JCheckBox Test");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JCheckBox ac = new JCheckBox("A/C");
    ac.setSelected(true);/*from w ww.java 2 s.  c om*/
    JCheckBox cdPlayer = new JCheckBox("A");
    JCheckBox cruiseControl = new JCheckBox("B");
    JCheckBox keylessEntry = new JCheckBox("C");
    JCheckBox antiTheft = new JCheckBox("D");
    JCheckBox centralLock = new JCheckBox("E");

    frame.add(new JLabel("Car Features"));
    frame.add(ac);
    frame.add(cdPlayer);
    frame.add(cruiseControl);
    frame.add(keylessEntry);
    frame.add(antiTheft);
    frame.add(centralLock);
    frame.pack();
    frame.setVisible(true);
}

From source file:ButtonCornerSample.java

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

    Icon acrossLogo = new ImageIcon("logo-across.jpg");
    Icon downLogo = new ImageIcon("logo-down.jpg");
    Icon bookCover = new ImageIcon("puzzlebook.jpg");
    JLabel columnLabel = new JLabel(acrossLogo);
    JLabel rowLabel = new JLabel(downLogo);
    JLabel coverLabel = new JLabel(bookCover);
    JButton button = new JButton("+");
    button.setFont(new Font("Monospaced", Font.PLAIN, 8));
    JScrollPane scrollPane = new JScrollPane(coverLabel);
    scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, button);
    scrollPane.setColumnHeaderView(columnLabel);
    scrollPane.setRowHeaderView(rowLabel);

    ActionListener actionListener = new JScrollPaneToTopAction(scrollPane);
    button.addActionListener(actionListener);

    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 200);//from w w  w . ja v a 2s.  c o m
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {

    JRadioButton button1 = new JRadioButton("Red");
    JRadioButton button2 = new JRadioButton("Green");
    JRadioButton button3 = new JRadioButton("Blue");
    ButtonGroup colorButtonGroup = new ButtonGroup();
    colorButtonGroup.add(button1);//www .  j av a  2 s . c om
    colorButtonGroup.add(button2);
    colorButtonGroup.add(button3);
    button1.setSelected(true);

    JFrame frame = new JFrame();
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(new JLabel("Color:"));
    frame.add(button1);
    frame.add(button2);
    frame.add(button3);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.java2s.com/style/download.png");
    final BufferedImage image = ImageIO.read(url);
    int x = 50;/*from www  . ja va2 s  . c om*/
    final Image crop = image.getSubimage(x, 0, image.getWidth() - x, image.getHeight());
    Runnable r = new Runnable() {
        @Override
        public void run() {
            JPanel gui = new JPanel();
            gui.add(new JLabel(new ImageIcon(image)), BorderLayout.LINE_START);
            gui.add(new JLabel("java2s.com"));
            gui.add(new JLabel(new ImageIcon(crop)), BorderLayout.LINE_END);
            JOptionPane.showMessageDialog(null, gui);
        }
    };
    SwingUtilities.invokeLater(r);
}

From source file:JFormattedTextFieldDateInputSampleDateFormatSHORT.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Date/Time Input");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel label;// w w w. j av  a 2s .  c o m
    JFormattedTextField input;
    JPanel panel;

    BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
    frame.setLayout(layout);

    Format shortDate = DateFormat.getDateInstance(DateFormat.SHORT);
    label = new JLabel("Short date:");
    input = new JFormattedTextField(shortDate);
    input.setValue(new Date());
    input.setColumns(20);

    panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    panel.add(label);
    panel.add(input);

    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] arg) throws Exception {
    Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
    Robot robot = new Robot();

    BufferedImage image = robot//  w w  w . j  a  va2s.co  m
            .createScreenCapture(new Rectangle(0, 0, (int) screenDim.getWidth(), (int) screenDim.getHeight()));

    JWindow window = new JWindow(new JFrame());
    window.getContentPane().setLayout(new BorderLayout());
    window.getContentPane().add(BorderLayout.CENTER, new JLabel(new ImageIcon(image)));
    window.pack();
    window.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JFrame f = new JFrame(Main.class.getSimpleName());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    BufferedImage bi = ImageIO.read(new URL("http://www.java2s.com/style/download.png"));
    JPanel panel = new JPanel(new BorderLayout());
    JLabel label = new JLabel(new ImageIcon(bi));
    panel.add(label);/*  w  ww  . ja v a  2s . c o m*/
    MouseMotionListener doScrollRectToVisible = new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            Rectangle r = new Rectangle(e.getX(), e.getY(), 1, 1);
            ((JPanel) e.getSource()).scrollRectToVisible(r);
        }
    };
    panel.addMouseMotionListener(doScrollRectToVisible);

    panel.setAutoscrolls(true);
    f.add(new JScrollPane(panel));
    f.pack();
    f.setSize(f.getWidth() / 2, f.getHeight() / 2);
    f.setVisible(true);
}

From source file:CircleLayoutDemo.java

public static void main(String a[]) {
    JFrame frame = new JFrame();
    JPanel circle = new JPanel();
    circle.setLayout(new CircleLayout(true));

    for (int i = 0; i < 10; i++) {
        circle.add(new JLabel("circle_" + i * 7));
    }/*  w w  w.j a  v a 2 s  . c o  m*/
    frame.setContentPane(circle);
    frame.setSize(200, 400);

    frame.setVisible(true);
}