Example usage for javax.swing JFrame pack

List of usage examples for javax.swing JFrame pack

Introduction

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

Prototype

@SuppressWarnings("deprecation")
public void pack() 

Source Link

Document

Causes this Window to be sized to fit the preferred size and layouts of its subcomponents.

Usage

From source file:Main.java

public static void main(String[] args) {
    JLabel userLabel = new JLabel("User Name");
    JLabel passLabel = new JLabel("Password");
    JTextField userText = new JTextField(20);
    JPasswordField passText = new JPasswordField(20);
    JButton loginButton = new JButton("Login");

    JPanel panel = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();

    gbc.insets = new Insets(4, 4, 4, 4);
    gbc.gridx = 0;/*from  www.  j  a v  a2s  .c  o  m*/
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.NONE;
    panel.add(userLabel, gbc);

    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    panel.add(userText, gbc);

    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.fill = GridBagConstraints.NONE;
    panel.add(passLabel, gbc);

    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    panel.add(passText, gbc);

    gbc.gridx = 0;
    gbc.gridy = 2;
    gbc.fill = GridBagConstraints.NONE;
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.gridwidth = 2;
    panel.add(loginButton, gbc);

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(panel));
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override//  ww  w .jav  a 2  s . c o m
        public void run() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new BackgroundPane());
            frame.pack();
            frame.setVisible(true);
        }
    });
}

From source file:MainClass.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JRootPane root = f.getRootPane();
    Container content = root.getContentPane();
    content.add(new JButton("Hello"));
    f.pack();
    f.setVisible(true);//from w  ww  . ja  va 2s  .  c  o  m
}

From source file:Main.java

public static void main(String[] args) {
    Node root = new Node("Root");
    fillTree(root, 5, "tree label");
    DefaultTreeModel model = new DefaultTreeModel(root);

    JTree tree = new JTree(model);
    ToolTipManager.sharedInstance().registerComponent(tree);
    tree.setCellRenderer(new MyTreeCellRenderer());
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new JScrollPane(tree));
    f.pack();
    f.setVisible(true);/*from   w  w  w. ja  v a 2 s. co  m*/
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new Main().getMainPanel());
    frame.pack();
    frame.setVisible(true);//from  ww  w .  ja va  2s. co m
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    final JFrame frame = new JFrame();
    frame.setUndecorated(true);/*  ww w .  j  a v a  2 s  .  c om*/
    JButton button = new JButton("Minimize");
    button.addActionListener(e -> frame.setState(Frame.ICONIFIED));
    frame.add(button);
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

From source file:Main.java

public static void main(String[] args) {
    JTabbedPane tab = new JTabbedPane();
    tab.addTab("A", new JPanel());
    tab.addTab("+", new JPanel());
    tab.getModel().addChangeListener(new ChangeListener() {
        private int lastSelected;
        private boolean ignore = false;

        @Override//from   w w w.  j a v  a2 s  .c  om
        public void stateChanged(ChangeEvent e) {
            if (!ignore) {
                ignore = true;
                try {
                    int selected = tab.getSelectedIndex();
                    String title = tab.getTitleAt(selected);
                    if ("+".equals(title)) {
                        JPanel pane = new JPanel();
                        tab.insertTab("Tab" + (tab.getTabCount() - 1), null, pane, null, lastSelected + 1);
                        tab.setSelectedComponent(pane);
                    } else {
                        lastSelected = selected;
                    }
                } finally {
                    ignore = false;
                }
            }
        }
    });
    final JButton btn = new JButton("Add");
    btn.addActionListener(e -> System.out.println(tab.getTabCount()));

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(tab);
    frame.add(btn, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new TabView());
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setMinimumSize(new Dimension(800, 450));
    frame.pack();
    frame.setVisible(true);/*from ww  w .  j  ava 2  s  . c  o  m*/
}

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);/*from   ww w . ja va 2s.com*/

    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:MyActionListener.java

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

    JButton button = new JButton("Click and holding alt, control, and shift key");
    button.addActionListener(new MyActionListener());
    frame.add(button);/*from  w w  w .  j ava  2  s.  c  o  m*/
    frame.pack();
    frame.setVisible(true);
}