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(String title) throws HeadlessException 

Source Link

Document

Creates a new, initially invisible Frame with the specified title.

Usage

From source file:BevelBorderWithDiffBorderColor.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Sample Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Border bevelBorder = new BevelBorder(BevelBorder.RAISED, Color.RED, Color.RED.darker(), Color.PINK,
            Color.PINK.brighter());

    JLabel aLabel = new JLabel("Bevel");
    aLabel.setBorder(bevelBorder);/*from  w ww.j av  a 2 s.c o  m*/
    aLabel.setHorizontalAlignment(JLabel.CENTER);

    frame.add(aLabel);
    frame.setSize(400, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame("BoxLayout  Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    JPanel hPanel = new JPanel();
    BoxLayout boxLayout = new BoxLayout(hPanel, BoxLayout.X_AXIS);
    hPanel.setLayout(boxLayout);/*w  ww  . j  a va2 s.  c o  m*/

    for (int i = 1; i <= 5; i++) {
        hPanel.add(new JButton("Button  " + i));
    }

    contentPane.add(hPanel, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) {
    JFrame f = new JFrame("JPasswordField Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Box rowOne = Box.createHorizontalBox();
    rowOne.add(new JLabel("Username"));
    rowOne.add(new JTextField());
    Box rowTwo = Box.createHorizontalBox();
    rowTwo.add(new JLabel("Password"));
    rowTwo.add(new JPasswordField());
    f.add(rowOne, BorderLayout.NORTH);
    f.add(rowTwo, BorderLayout.SOUTH);
    f.setSize(300, 200);/*ww  w . j  a v  a  2s  . c om*/
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame(Main.class.getSimpleName());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JButton button = new JButton("Click me to open dialog");
    button.addActionListener(e -> {/*from   w  ww . j a  v  a 2 s. c om*/
        Window parentWindow = SwingUtilities.windowForComponent(button);
        JDialog dialog = new JDialog(parentWindow);
        dialog.setLocationRelativeTo(button);
        dialog.setModal(true);
        dialog.add(new JLabel("A dialog"));
        dialog.pack();
        dialog.setVisible(true);
    });
    frame.add(button);
    frame.pack();
    frame.setVisible(true);
}

From source file:BoxSample.java

public static void main(String args[]) {
    JFrame verticalFrame = new JFrame("Vertical");
    verticalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Box verticalBox = Box.createVerticalBox();
    verticalBox.add(new JLabel("Top"));
    verticalBox.add(new JTextField("Middle"));
    verticalBox.add(new JButton("Bottom"));
    verticalFrame.add(verticalBox, BorderLayout.CENTER);
    verticalFrame.setSize(150, 150);/*from w  ww . j  a v  a 2s.  co  m*/
    verticalFrame.setVisible(true);

}

From source file:TrySpringLayout.java

public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Spring Layout");
    aWindow.setBounds(30, 30, 300, 300);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    SpringLayout layout = new SpringLayout();
    Container content = aWindow.getContentPane();
    content.setLayout(layout);// w  ww  . ja v a  2s  . c om
    JButton[] buttons = new JButton[6];
    SpringLayout.Constraints constr = null;
    for (int i = 0; i < buttons.length; i++) {
        buttons[i] = new JButton("Press " + (i + 1));
        content.add(buttons[i]);
    }
    Spring xSpring = Spring.constant(5, 15, 25);
    Spring ySpring = Spring.constant(10, 30, 50);
    constr = layout.getConstraints(buttons[0]);
    constr.setX(xSpring);
    constr.setY(ySpring);
    // Hook buttons together with springs
    for (int i = 1; i < buttons.length; i++) {
        constr = layout.getConstraints(buttons[i]);
        layout.putConstraint(SpringLayout.WEST, buttons[i], xSpring, SpringLayout.EAST, buttons[i - 1]);
        layout.putConstraint(SpringLayout.NORTH, buttons[i], ySpring, SpringLayout.SOUTH, buttons[i - 1]);
    }
    aWindow.setVisible(true); // Display the window
}

From source file:BoxSample.java

public static void main(String args[]) {
    JFrame horizontalFrame = new JFrame("Horizontal");
    horizontalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Box horizontalBox = Box.createHorizontalBox();
    horizontalBox.add(new JLabel("Left"));
    horizontalBox.add(new JTextField("Middle"));
    horizontalBox.add(new JButton("Right"));
    horizontalFrame.add(horizontalBox, BorderLayout.CENTER);
    horizontalFrame.setSize(150, 150);//from w  w  w. java  2 s.  co m
    horizontalFrame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    String text = "one\ntwo\nthree\nfour\nfive";
    JFrame frame = new JFrame("title");
    JTextArea textArea = new JTextArea(text, 1, 30); // shows only one line
    JScrollPane scrollPane = new JScrollPane(textArea);
    frame.add(scrollPane);//from   w ww .j a va2 s. c  o m
    frame.pack();
    frame.setVisible(true);

    final JViewport viewport = scrollPane.getViewport();

    textArea.addCaretListener(e -> {
        System.out.println("First : " + viewport.getViewPosition());
        System.out.println("Second: " + viewport.getViewPosition());
    });
    textArea.setCaretPosition(text.length());
}

From source file:TreeArraySample.java

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

    Vector oneVector = new NamedVector("One", args);
    Vector twoVector = new NamedVector("Two", new String[] { "Mercury", "Venus", "Mars" });
    Vector threeVector = new NamedVector("Three");
    threeVector.add(System.getProperties());
    threeVector.add(twoVector);//from w w  w .j  av a  2 s.c  om
    Object rootNodes[] = { oneVector, twoVector, threeVector };
    Vector rootVector = new NamedVector("Root", rootNodes);
    JTree tree = new JTree(rootVector);

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    //    frame.getContentPane().add(tree, BorderLayout.CENTER);
    frame.setSize(300, 300);
    frame.setVisible(true);
}

From source file:Main.java

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

    JLabel leftImage = new JLabel(new ImageIcon("a.gif"));
    Component left = new JScrollPane(leftImage);
    JLabel rightImage = new JLabel(new ImageIcon("b.gif"));
    Component right = new JScrollPane(rightImage);

    JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, right);
    split.setDividerLocation(100);/* ww  w  .java 2s  .c  o m*/
    frame.getContentPane().add(split);

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