Example usage for javax.swing SwingConstants CENTER

List of usage examples for javax.swing SwingConstants CENTER

Introduction

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

Prototype

int CENTER

To view the source code for javax.swing SwingConstants CENTER.

Click Source Link

Document

The central position in an area.

Usage

From source file:SimpleSplashScreen.java

public static void main(String[] arg) {
    JWindow jwin = new JWindow();
    jwin.getContentPane().add(new JLabel("Loading ZIP/JAR Manager...", SwingConstants.CENTER));
    jwin.setBounds(200, 200, 200, 100);/* www .  j  a  v  a2s  .co m*/
    jwin.setVisible(true);

    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    jwin.setVisible(false);
    jwin.dispose();

}

From source file:Main.java

public static void main(String args[]) {
    JWindow window = new JWindow();
    window.getContentPane().add(new JLabel("Loading", SwingConstants.CENTER));
    window.setBounds(500, 150, 300, 200);
    window.setVisible(true);//from w w w  .j  av  a 2  s.  com
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
    }
    window.setVisible(false);
    JFrame frame = new JFrame();
    frame.add(new JLabel("Welcome Swing application..."));
    frame.setVisible(true);
    frame.setSize(300, 200);
    window.dispose();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    JButton button = new JButton();
    // Place text over center of icon; they both occupy the same space
    button.setVerticalTextPosition(SwingConstants.CENTER);
    button.setHorizontalTextPosition(SwingConstants.CENTER);

    // Place text above icon
    button.setVerticalTextPosition(SwingConstants.TOP);
    button.setHorizontalTextPosition(SwingConstants.CENTER);

    // Place text below icon
    button.setVerticalTextPosition(SwingConstants.BOTTOM);
    button.setHorizontalTextPosition(SwingConstants.CENTER);

    // Place text to the left of icon, vertically centered
    button.setVerticalTextPosition(SwingConstants.CENTER);
    button.setHorizontalTextPosition(SwingConstants.LEFT);

    // Place text to the left of icon and align their tops
    button.setVerticalTextPosition(SwingConstants.TOP);
    button.setHorizontalTextPosition(SwingConstants.LEFT);

    // Place text to the left of icon and align their bottoms
    button.setVerticalTextPosition(SwingConstants.BOTTOM);
    button.setHorizontalTextPosition(SwingConstants.LEFT);

    // Place text to the right of icon, vertically centered
    button.setVerticalTextPosition(SwingConstants.CENTER);
    button.setHorizontalTextPosition(SwingConstants.RIGHT);

    // Place text to the right of icon and align their tops
    button.setVerticalTextPosition(SwingConstants.TOP);
    button.setHorizontalTextPosition(SwingConstants.RIGHT);

    // Place text to the right of icon and align their bottoms
    button.setVerticalTextPosition(SwingConstants.BOTTOM);
    button.setHorizontalTextPosition(SwingConstants.RIGHT);

}

From source file:Main.java

public static void main(String[] args) {
    JTabbedPane tabbedPane = new JTabbedPane();
    for (int i = 0; i < 5; i++) {
        tabbedPane.add("Tab " + i, new JLabel("Label " + i, SwingConstants.CENTER));
    }/*from  ww  w .  j a v  a 2 s  .c om*/

    tabbedPane.getModel().addChangeListener(e -> {
        JLabel label = (JLabel) tabbedPane.getSelectedComponent();
        System.out.println(label.getText());
    });

    tabbedPane.setPreferredSize(new Dimension(500, 300));

    JFrame frame = new JFrame();
    frame.getContentPane().add(tabbedPane);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    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// ww  w. ja v a  2  s . c o m
            .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) {
    JLabel label1 = new JLabel("BottomRight", SwingConstants.RIGHT);
    JLabel label2 = new JLabel("CenterLeft", SwingConstants.LEFT);
    JLabel label3 = new JLabel("TopCenter", SwingConstants.CENTER);
    label1.setVerticalAlignment(SwingConstants.BOTTOM);
    label2.setVerticalAlignment(SwingConstants.CENTER);
    label3.setVerticalAlignment(SwingConstants.TOP);

    label1.setBorder(BorderFactory.createLineBorder(Color.black));
    label2.setBorder(BorderFactory.createLineBorder(Color.black));
    label3.setBorder(BorderFactory.createLineBorder(Color.black));

    JFrame frame = new JFrame("AlignmentExample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel(new GridLayout(3, 1, 8, 8));
    p.add(label1);//from  w  w w  .  ja va2  s  .  c o m
    p.add(label2);
    p.add(label3);
    p.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
    frame.setContentPane(p);
    frame.setSize(200, 200);
    frame.setVisible(true);
}

From source file:TestOval.java

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

    JLabel label1 = new JLabel(new OvalIcon(20, 50));
    JLabel label2 = new JLabel(new OvalIcon(50, 20));
    JLabel label3 = new JLabel("Round!", new OvalIcon(60, 60), SwingConstants.CENTER);
    label3.setHorizontalTextPosition(SwingConstants.CENTER);

    Container c = f.getContentPane();
    c.setLayout(new FlowLayout());
    c.add(label1);/*from  ww w.j  a  v  a  2s  . co  m*/
    c.add(label2);
    c.add(label3);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    int BTN_COUNT = 3;
    int VERT_GAP = 10;
    int EB_GAP = 5;
    float TITLE_SIZE = 36f;
    String TITLE_TEXT = "This is my Title";

    JLabel titleLabel = new JLabel(TITLE_TEXT, SwingConstants.CENTER);
    titleLabel.setFont(titleLabel.getFont().deriveFont(TITLE_SIZE));
    JPanel titlePanel = new JPanel();
    titlePanel.add(titleLabel);// w  w w.  jav  a  2 s  . c om

    JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0));
    for (int i = 0; i < BTN_COUNT; i++) {
        JButton btn = new JButton("Button " + (i + 1));
        buttonPanel.add(btn);
    }

    JTextArea textArea = new JTextArea(20, 30);

    JPanel mainPanel = new JPanel();
    mainPanel.setBorder(BorderFactory.createEmptyBorder(EB_GAP, EB_GAP, EB_GAP, EB_GAP));
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
    mainPanel.add(titlePanel);
    mainPanel.add(Box.createVerticalStrut(VERT_GAP));
    mainPanel.add(buttonPanel);
    mainPanel.add(Box.createVerticalStrut(VERT_GAP));
    mainPanel.add(new JScrollPane(textArea));

    JFrame frame = new JFrame();
    frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    SpringLayout layout = new SpringLayout();
    JPanel p = new JPanel(layout);
    p.setBorder(BorderFactory.createLineBorder(Color.GREEN, 10));

    JLabel l1 = new JLabel("label: width=90%", SwingConstants.CENTER);
    l1.setBorder(BorderFactory.createLineBorder(Color.RED, 1));
    JButton l2 = new JButton("button: width=50%");

    Spring panelw = layout.getConstraint(WIDTH, p);

    SpringLayout.Constraints c1 = layout.getConstraints(l1);
    c1.setX(Spring.constant(0));/*from www  .j a  v a2s.  co  m*/
    c1.setY(Spring.constant(20));
    c1.setWidth(Spring.scale(panelw, 0.9f));
    p.add(l1);

    SpringLayout.Constraints c2 = layout.getConstraints(l2);
    c2.setWidth(Spring.scale(panelw, 0.5f));
    layout.putConstraint(SOUTH, l2, -20, SOUTH, p);
    layout.putConstraint(EAST, l2, -20, EAST, p);
    p.add(l2);

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(p);
    f.setSize(320, 240);
    f.setVisible(true);
}

From source file:net.redstonelamp.gui.RedstoneLampGUI.java

public static void main(String[] args) {
    JFrame frame = new JFrame("RedstoneLamp");
    frame.setLayout(new GridLayout(2, 1));
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    JLabel label = new JLabel("RedstoneLamp");
    label.setHorizontalAlignment(SwingConstants.CENTER);
    frame.add(label);/*  ww w .  j a  va 2s .  co  m*/
    JPanel lowPanel = new JPanel();
    JPanel left = new JPanel();
    left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
    lowPanel.add(left);
    JPanel right = new JPanel();
    right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
    lowPanel.add(right);
    JButton openButton = new JButton("Open server at...");
    openButton.addActionListener(e -> {
        JFileChooser chooser = new JFileChooser(new File("."));
        chooser.setDialogTitle("Select RedstoneLamp server home");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
        int action = chooser.showOpenDialog(frame);
        if (action == JFileChooser.APPROVE_OPTION) {
            File selected = chooser.getSelectedFile();
            File jar = new File("RedstoneLamp.jar");
            if (!jar.isFile()) {
                int result = JOptionPane.showConfirmDialog(frame, "Could not find RedstoneLamp installation. "
                        + "Would you like to install RedstoneLamp there?");
                if (result == JOptionPane.YES_OPTION) {
                    installCallback(frame, selected);
                }
                return;
            }
            frame.dispose();
            addHistory(selected);
            currentRoot = new ServerActivity(selected);
        }
    });
    right.add(openButton);
    JButton installButton = new JButton("Install server at...");
    installButton.addActionListener(e -> {
        JFileChooser chooser = new JFileChooser(".");
        chooser.setDialogTitle("Select directory to install server in");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
        int action = chooser.showSaveDialog(frame);
        if (action == JFileChooser.APPROVE_OPTION) {
            File selected = chooser.getSelectedFile();
            File jar = new File("RedstoneLamp.jar");
            if (jar.isFile()) {
                int result = JOptionPane.showConfirmDialog(frame, "A RedstoneLamp jar installation is present. "
                        + "Are you sure you want to reinstall RedstoneLamp there?");
                if (result == JOptionPane.NO_OPTION) {
                    frame.dispose();
                    addHistory(selected);
                    currentRoot = new ServerActivity(selected);
                    return;
                }
            }
            installCallback(frame, selected);
        }
    });
    frame.add(lowPanel);
    frame.pack();
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation(dimension.width / 2 - frame.getSize().width / 2,
            dimension.height / 2 - frame.getSize().height / 2);
    frame.setVisible(true);
}