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) {
    JLabel label = new JLabel("First Name");
    label.setForeground(Color.magenta);

    JFrame frame = new JFrame();
    frame.add(label);//  w w w  .  j a  va 2 s . co  m
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(20, 20, 500, 500);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GroupLayout layout = new GroupLayout(panel);
    panel.setLayout(layout);// ww  w.  j a  v a 2 s. c o m

    JButton buttonD = new JButton("D");
    JButton buttonR = new JButton("R");
    JButton buttonY = new JButton("Y");
    JButton buttonO = new JButton("O");
    JButton buttonT = new JButton("T");

    GroupLayout.SequentialGroup leftToRight = layout.createSequentialGroup();

    leftToRight.addComponent(buttonD);
    GroupLayout.ParallelGroup columnMiddle = layout.createParallelGroup();
    columnMiddle.addComponent(buttonR);
    columnMiddle.addComponent(buttonO);
    columnMiddle.addComponent(buttonT);
    leftToRight.addGroup(columnMiddle);
    leftToRight.addComponent(buttonY);

    GroupLayout.SequentialGroup topToBottom = layout.createSequentialGroup();
    GroupLayout.ParallelGroup rowTop = layout.createParallelGroup();
    rowTop.addComponent(buttonD);
    rowTop.addComponent(buttonR);
    rowTop.addComponent(buttonY);
    topToBottom.addGroup(rowTop);
    topToBottom.addComponent(buttonO);
    topToBottom.addComponent(buttonT);

    layout.setHorizontalGroup(leftToRight);
    layout.setVerticalGroup(topToBottom);

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

From source file:RelativeY.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = f.getContentPane();
    pane.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;/*from  w w  w.j av  a  2s.c o  m*/
    pane.add(new JButton("First column"), gbc);
    gbc.gridx = 1;
    gbc.gridy = GridBagConstraints.RELATIVE;
    pane.add(new JButton("Second column, first row"), gbc);
    pane.add(new JButton("Second column, second row"), gbc);
    pane.add(new JButton("Second column, third row"), gbc);
    gbc.gridx = 2;
    pane.add(new JButton("Third column"), gbc);
    f.setSize(500, 300);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JLabel label = new JLabel("First Name");
    label.setForeground(Color.MAGENTA);

    JFrame frame = new JFrame();
    frame.add(label);// w ww .  j a  va 2 s .c o m
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(20, 20, 500, 500);
    frame.setVisible(true);
}

From source file:MainFrameBorderLayout.java

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

    JPanel outerPanel = new JPanel(new BorderLayout());
    JPanel topPanel = new JPanel(new BorderLayout());
    JLabel label = new JLabel("Name:");
    JTextField text = new JTextField();
    topPanel.add(label, BorderLayout.BEFORE_LINE_BEGINS);
    topPanel.add(text, BorderLayout.CENTER);
    outerPanel.add(topPanel, BorderLayout.BEFORE_FIRST_LINE);

    frame.add(outerPanel);//from   ww  w . j  a v a  2  s. c om
    frame.setSize(300, 200);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    JLabel label = new JLabel("First Name");
    label.setForeground(Color.darkGray);

    JFrame frame = new JFrame();
    frame.add(label);/*w w w .j a v  a  2  s  .c  o m*/
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(20, 20, 500, 500);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String text = "A JTextArea object represents a multiline area for displaying text. "
            + "You can change the number of lines that can be displayed at a time, "
            + "as well as the number of columns. You can wrap lines and words too. "
            + "You can also put your JTextArea in a JScrollPane to make it scrollable.";

    JTextArea textAreal = new JTextArea(text, 5, 10);
    textAreal.setPreferredSize(new Dimension(100, 100));
    JTextArea textArea2 = new JTextArea(text, 5, 10);
    textArea2.setPreferredSize(new Dimension(100, 100));
    JScrollPane scrollPane = new JScrollPane(textArea2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    textAreal.setLineWrap(true);/*from   ww w  .  j a v  a 2s . c  o m*/
    textArea2.setLineWrap(true);
    frame.add(textAreal);
    frame.add(scrollPane);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new MyContentPane());
    frame.pack();// ww  w.  ja  v  a  2s. c o m
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JLabel label = new JLabel("First Name");
    label.setForeground(Color.DARK_GRAY);

    JFrame frame = new JFrame();
    frame.add(label);//from  w  w w . jav  a 2s.com
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(20, 20, 500, 500);
    frame.setVisible(true);
}

From source file:GridBagLayoutFill.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = f.getContentPane();
    pane.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;//from  w  w  w  .  j a va 2  s . c  o  m
    gbc.gridy = GridBagConstraints.RELATIVE;
    pane.add(new JButton("This button's preferred width " + "is large because its text is long"), gbc);
    pane.add(new JButton("Small centered button"), gbc);
    gbc.fill = GridBagConstraints.HORIZONTAL;
    pane.add(new JButton("Expands to fill column width"), gbc);
    f.setSize(400, 300);
    f.setVisible(true);
}