Example usage for javax.swing JFrame JFrame

List of usage examples for javax.swing JFrame JFrame

Introduction

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

Prototype

public JFrame() throws HeadlessException 

Source Link

Document

Constructs a new frame that is initially invisible.

Usage

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new BorderLayout());
    JLabel label = new JLabel("Name: ");
    label.setDisplayedMnemonic(KeyEvent.VK_N);
    JTextField textField = new JTextField();
    textField.setHorizontalAlignment(JTextField.CENTER);

    label.setLabelFor(textField);//w w w.  j ava 2s. c om
    panel.add(label, BorderLayout.WEST);
    panel.add(textField, BorderLayout.CENTER);
    frame.add(panel, BorderLayout.NORTH);
    frame.setSize(250, 150);
    frame.setVisible(true);

}

From source file:JSplitPaneVerticalSetTopBottom.java

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

    JComponent topButton = new JButton("Left");
    JComponent bottomButton = new JButton("Right");
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setTopComponent(topButton);
    splitPane.setBottomComponent(bottomButton);

    horizontalFrame.add(splitPane, BorderLayout.CENTER);
    horizontalFrame.setSize(150, 150);/*from   w w  w. j a  va 2 s.  co m*/
    horizontalFrame.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setUndecorated(true);/*from  w w  w.  ja  va2 s.c  o  m*/
    JPanel panel = new JPanel();
    panel.add(new JLabel("Stackoverflow!"));
    panel.add(new JButton(new AbstractAction("Close") {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }));
    f.add(panel);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {

    JFrame frame = new JFrame();

    JButton btn = new JButton("Test");

    JPanel panel = new JPanel();
    panel.add(btn);/*from  w w w  . j  av  a  2s  .co  m*/

    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.add("Tab1", panel);

    frame.add(tabbedPane, BorderLayout.CENTER);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setMinimumSize(new Dimension(300, 300));
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    Container container = frame.getContentPane();

    GridBagLayout gbl = new GridBagLayout();

    container.setLayout(gbl);//from w  ww .j a va 2 s .c  o m

    // Place a component at cell location (1,1)
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 1;

    JButton component = new JButton("a");

    // Associate the gridbag constraints with the component
    gbl.setConstraints(component, gbc);

    container.add(component);

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

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.add(new JPanel() {
        @Override/*from   w  w  w .  ja v a2  s.c om*/
        public Dimension getPreferredSize() {
            return new Dimension(320, 240);
        }

    });
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
    Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
    int x = (int) rect.getMaxX() - f.getWidth();
    int y = (int) rect.getMaxY() - f.getHeight();
    f.setLocation(x, y);
    f.setVisible(true);
}

From source file:Main.java

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

    JPanel buttonPanel = new JPanel();
    buttonPanel.addContainerListener(new ContainerListener() {
        public void componentAdded(ContainerEvent e) {
            displayMessage(" added to ", e);
        }//  www. ja  v a  2  s. co  m

        public void componentRemoved(ContainerEvent e) {
            displayMessage(" removed from ", e);
        }

        void displayMessage(String action, ContainerEvent e) {
            System.out.println(((JButton) e.getChild()).getText() + " was" + action
                    + e.getContainer().getClass().getName());
        }
    });
    buttonPanel.add(new JButton("A"));

    frame.add(buttonPanel);

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

From source file:Main.java

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

    JPanel buttonPanel = new JPanel(true);
    buttonPanel.addContainerListener(new ContainerListener() {
        public void componentAdded(ContainerEvent e) {
            displayMessage(" added to ", e);
        }// w w  w  .j a  v  a 2  s .  c o  m

        public void componentRemoved(ContainerEvent e) {
            displayMessage(" removed from ", e);
        }

        void displayMessage(String action, ContainerEvent e) {
            System.out.println(((JButton) e.getChild()).getText() + " was" + action
                    + e.getContainer().getClass().getName());
        }
    });
    buttonPanel.add(new JButton("A"));

    frame.add(buttonPanel);

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

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.add(new JPanel() {
        @Override// www  .  j a  v  a 2s .c  om
        public Dimension getPreferredSize() {
            return new Dimension(320, 240);
        }

    });
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
    Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
    int x = (int) rect.getMaxX() - f.getWidth();
    int y = 0;
    f.setLocation(x, y);
    f.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    AbstractButton jb = new JButton("Press Me");
    System.out.println(jb.getRolloverSelectedIcon());

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(jb);/*from  ww w.  j a  va  2  s . c o m*/
    f.pack();
    f.setVisible(true);
}