Example usage for javax.swing JFrame setLayout

List of usage examples for javax.swing JFrame setLayout

Introduction

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

Prototype

public void setLayout(LayoutManager manager) 

Source Link

Document

Sets the LayoutManager.

Usage

From source file:MainClass.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("Alignment Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(0, 1));
    JTextField textField = new JTextField("Left");
    textField.setHorizontalAlignment(JTextField.LEFT);
    frame.add(textField);//from  w ww.  j  a  va 2 s.  c o  m
    textField = new JTextField("Center");
    textField.setHorizontalAlignment(JTextField.CENTER);
    frame.add(textField);
    textField = new JTextField("Right");
    textField.setHorizontalAlignment(JTextField.RIGHT);
    frame.add(textField);
    textField = new JTextField("Leading");
    textField.setHorizontalAlignment(JTextField.LEADING);
    frame.add(textField);
    textField = new JTextField("Trailing");
    textField.setHorizontalAlignment(JTextField.TRAILING);
    frame.add(textField);
    frame.pack();
    frame.setSize(250, (int) frame.getSize().getHeight());
    frame.setVisible(true);

}

From source file:FocusCycleSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Focus Cycle Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(3, 3));
    for (int i = 0; i < 3; i++) {
        JButton button = new JButton("" + i);
        frame.add(button);// w w w.  ja va 2s.  c  o m
    }
    JPanel panel = new JPanel();
    panel.setFocusCycleRoot(true);
    panel.setFocusTraversalPolicyProvider(true);
    panel.setLayout(new GridLayout(1, 3));
    for (int i = 0; i < 3; i++) {
        JButton button = new JButton("" + (i + 3));
        panel.add(button);
    }
    frame.add(panel);
    for (int i = 0; i < 3; i++) {
        JButton button = new JButton("" + (i + 6));
        frame.add(button);
    }
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Reverse Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(3, 3));
    for (int i = 0; i < 9; i++) {
        JButton button = new JButton(Integer.toString(i));
        frame.add(button);/*from   ww  w .ja  va  2s  .c  o m*/
    }
    final Container contentPane = frame.getContentPane();
    Comparator<Component> comp = new Comparator<Component>() {
        public int compare(Component c1, Component c2) {
            Component comps[] = contentPane.getComponents();
            List list = Arrays.asList(comps);
            int first = list.indexOf(c1);
            int second = list.indexOf(c2);
            return second - first;
        }
    };
    FocusTraversalPolicy policy = new SortingFocusTraversalPolicy(comp);
    frame.setFocusTraversalPolicy(policy);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    int cp = 0;//  ww  w.j av  a 2  s  . c om
    StyledDocument doc;
    JTextPane jta = new JTextPane();
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    doc = jta.getStyledDocument();
    JScrollPane jsp = new JScrollPane(jta);
    jsp.setPreferredSize(new Dimension(400, 400));
    String[] fnt = ge.getAvailableFontFamilyNames();
    MutableAttributeSet mas = jta.getInputAttributes();
    for (int i = 0; i < fnt.length; i++) {
        StyleConstants.setBold(mas, false);
        StyleConstants.setItalic(mas, false);
        StyleConstants.setFontFamily(mas, fnt[i]);
        StyleConstants.setFontSize(mas, 16);

        doc.insertString(cp, fnt[i] + "\n", mas);

        StyleConstants.setBold(mas, true);
        doc.insertString(cp, fnt[i] + "bold \n", mas);
        StyleConstants.setItalic(mas, true);

        doc.insertString(cp, fnt[i] + "bold and italic\n", mas);
        StyleConstants.setBold(mas, false);
        doc.insertString(cp, fnt[i] + "italic\n", mas);
    }
    JFrame frm = new JFrame();
    frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frm.setLayout(new BorderLayout());
    frm.add(jsp, BorderLayout.CENTER);
    frm.setLocation(100, 100);
    frm.pack();
    frm.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame("Testing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(new LoginPanel());
    frame.pack();//from w  w w  .java 2s  . c  o  m
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel statusBar = new JPanel(new FlowLayout(FlowLayout.LEFT));
    statusBar.setBorder(new CompoundBorder(new LineBorder(Color.DARK_GRAY), new EmptyBorder(4, 4, 4, 4)));
    final JLabel status = new JLabel();
    statusBar.add(status);//  w w w .  j  av  a2s. co  m

    JLabel content = new JLabel("Content in the middle");
    content.setHorizontalAlignment(JLabel.CENTER);

    final JFrame frame = new JFrame("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(content);
    frame.add(statusBar, BorderLayout.SOUTH);

    frame.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            status.setText(frame.getWidth() + "x" + frame.getHeight());
        }
    });

    frame.setBounds(20, 20, 200, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] a) {
    final JFrame frame = new JFrame("GridBagLayout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridBagLayout());
    JButton button;/*  ww  w  .j  a v  a2 s . co  m*/

    button = new JButton("One");
    addComponent(frame, button, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE);
    button = new JButton("Two");
    addComponent(frame, button, 1, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE);

    button = new JButton("Three");
    addComponent(frame, button, 0, 1, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE);

    button = new JButton("Four");
    addComponent(frame, button, 0, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE);
    frame.setSize(500, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame("Testing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(new JLabel("Auto Hide"));
    frame.pack();/*from w  w  w  .ja v a2  s.  co  m*/
    frame.setVisible(true);

    Timer autoHideTimer = new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            frame.dispose();
        }
    });
    autoHideTimer.setRepeats(false);

    frame.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseExited(MouseEvent e) {
            System.out.println("Restart...");
            autoHideTimer.restart();
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            System.out.println("Stop");
            autoHideTimer.stop();
        }
    });
}

From source file:MainClass.java

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

    frame.setLayout(new GridLayout(2, 2, 10, 10));

    JButton button1 = new JButton("Text Button");
    button1.setMnemonic(KeyEvent.VK_B);
    frame.add(button1);//from  w  ww .  j  a  v a  2 s .  c  o m

    JButton button2 = new JButton("warn");
    frame.add(button2);

    JButton button3 = new JButton("Warning");
    frame.add(button3);

    String htmlButton = "<html><sup>HTML</sup> <sub><em>Button</em></sub><br>"
            + "<font color=\"#FF0080\"><u>Multi-line</u></font>";
    JButton button4 = new JButton(htmlButton);
    frame.add(button4);

    JRootPane rootPane = frame.getRootPane();
    rootPane.setDefaultButton(button2);

    frame.setSize(300, 200);
    frame.setVisible(true);
}

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);/*from  w ww .j  a v a 2  s  .c o  m*/
    frame.add(userNameField);
    frame.add(label2);
    frame.add(passwordField);
    frame.setSize(200, 70);
    frame.setVisible(true);
}