Java by API/javax.swing/JFrame — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 14:20, 31 мая 2010

Содержание

JFrame: addComponentListener(ComponentListener l)

  

import java.awt.ruponent;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ruponentAdapter;
import java.awt.event.ruponentEvent;
import java.awt.event.ruponentListener;
import javax.swing.JFrame;
public class Main {
  public static void main() {
    ComponentListener listener = new ComponentAdapter() {
      public void componentShown(ComponentEvent evt) {
        Component c = (Component) evt.getSource();
        System.out.println("Component is now visible");
      }
      public void componentHidden(ComponentEvent evt) {
        Component c = (Component) evt.getSource();
        System.out.println("Component is now hidden");
      }
      public void componentMoved(ComponentEvent evt) {
        Component c = (Component) evt.getSource();
        Point newLoc = c.getLocation();
        System.out.println("Get new location");
      }
      public void componentResized(ComponentEvent evt) {
        Component c = (Component) evt.getSource();
        Dimension newSize = c.getSize();
        System.out.println("Get new size");
      }
    };
    JFrame frame = new JFrame();
    frame.setSize(300, 300);
    frame.addComponentListener(listener);
    frame.setVisible(true);
  }
}





JFrame: addContainerListener(ContainerListener l)1

  
import java.awt.ruponent;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    ContainerListener cont = new ContainerListener() {
      ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          System.out.println("Selected: " + e.getActionCommand());
        }
      };
      public void componentAdded(ContainerEvent e) {
        Component c = e.getChild();
        if (c instanceof JButton) {
          JButton b = (JButton) c;
          b.addActionListener(listener);
        }
      }
      public void componentRemoved(ContainerEvent e) {
        Component c = e.getChild();
        if (c instanceof JButton) {
          JButton b = (JButton) c;
          b.removeActionListener(listener);
        }
      }
    };
    contentPane.addContainerListener(cont);
    contentPane.setLayout(new GridLayout(3, 2));
    contentPane.add(new JButton("First"));
    contentPane.add(new JButton("Second"));
    contentPane.add(new JButton("Third"));
    contentPane.add(new JButton("Fourth"));
    contentPane.add(new JButton("Fifth"));
    frame.setSize(300, 200);
    frame.show();
  }
}





JFrame: addMouseWheelListener(MouseWheelListener l)

  
import java.awt.Graphics;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class Main extends JFrame {
  private BufferedImage bi;
  private int zoom = 0;
  private static final double ZOOM_AMOUNT = 1.1;
  public static void main(String[] args) {
      new Main("your.jpg").setVisible(true);
  }
  private void sizeToZoom() {
    double factor = Math.pow(ZOOM_AMOUNT, zoom);
    setSize((int) (bi.getWidth() * factor), (int) (bi.getHeight() * factor));
  }
  public Main(String filename) {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    try {
      bi = ImageIO.read(new File(filename));
    } catch (Exception e) {
      e.printStackTrace();
    }
    sizeToZoom();
    addMouseWheelListener(new MouseWheelListener() {
      public void mouseWheelMoved(MouseWheelEvent e) {
        int steps = e.getWheelRotation();
        zoom += steps;
        sizeToZoom();
      }
    });
  }
  public void paint(Graphics g) {
    g.drawImage(bi, 0, 0, getWidth(), getHeight(), this);
  }
}





JFrame: addWindowListener(WindowListener lis)

  
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
  public MainClass() {
    JButton btn1 = new JButton("Button1");
    add(btn1);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MainClass());
    //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.addWindowListener(new FrameListener());
    
    frame.setSize(200, 200);
    frame.setVisible(true);
  }
}
class FrameListener extends WindowAdapter
{
   public void windowClosing(WindowEvent e)
  {
    System.exit(0);
  }
}





JFrame.DISPOSE_ON_CLOSE

  

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main extends JFrame {
  private String textMessage = "Java Internationalization";
  public Main() {
    super("TrueType Font Demonstration");
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    ge.getAllFonts();
    Font font = new Font("Jokerman", Font.PLAIN, 35);
    JLabel textLabel = new JLabel(textMessage);
    textLabel.setFont(font);
    getContentPane().add(textLabel);
    setVisible(true);
  }
  public static void main(String[] args) {
    JFrame frame = new Main();
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }
}





JFrame.DO_NOTHING_ON_CLOSE

  

import javax.swing.JFrame;
public class Main {
  public static void main(String[] argv) throws Exception {
    // Create a frame
    JFrame frame = new JFrame();
    // Get default close operation
    int op = frame.getDefaultCloseOperation(); // HIDE_ON_CLOSE
    // Set to ignore the button
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  }
}





JFrame.EXIT_ON_CLOSE

  
/*
 * Output:
 *  
 */
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
  public void paint(Graphics g) {
    Dimension d = this.getPreferredSize();
    int fontSize = 20;
    g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));
     
    g.setColor(Color.red);
    
    g.drawString("www.jexp.ru", 10, 20);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MainClass());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200,200);
    frame.setVisible(true);
  }
}





JFrame: getAccessibleContext()

  
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.accessibility.AccessibleContext;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JFrame implements ActionListener {
  JButton jb;
  MainClass(String title) {
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    AccessibleContext ac = getAccessibleContext();
    ac.setAccessibleDescription("Accessibility Demo1 description.");
    ac = getRootPane().getAccessibleContext();
    ac.setAccessibleName("Root pane");
    ac.setAccessibleDescription("Root pane description");
    ac = getGlassPane().getAccessibleContext();
    ac.setAccessibleName("Glass pane");
    ac.setAccessibleDescription("Glass pane description");
    ac = getLayeredPane().getAccessibleContext();
    ac.setAccessibleName("Layered pane");
    ac.setAccessibleDescription("Layered pane description");
    ac = getContentPane().getAccessibleContext();
    ac.setAccessibleName("Content pane");
    ac.setAccessibleDescription("Content pane description");
    JPanel p = new JPanel();
    ac = p.getAccessibleContext();
    ac.setAccessibleName("Panel");
    ac.setAccessibleDescription("Panel description");
    jb = new JButton("Press Me");
    jb.addActionListener(this);
    jb.setToolTipText("Press me for accessibility information.");
    p.add(jb);
    getContentPane().add(p);
    setSize(200, 75);
    setVisible(true);
  }
  public void actionPerformed(ActionEvent e) {
    dumpInfo(getAccessibleContext());
  }
  void dumpInfo(AccessibleContext ac) {
    System.out.println("Name = " + ac.getAccessibleName());
    System.out.println("Description = " + ac.getAccessibleDescription());
    int nChildren = ac.getAccessibleChildrenCount();
    for (int i = 0; i < nChildren; i++)
      dumpInfo(ac.getAccessibleChild(i).getAccessibleContext());
  }
  public static void main(String[] args) {
    new MainClass("Accessibility Demo1");
  }
}





JFrame: getContentPane().getActionMap() [Bind key action to JFrame]

  
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
public class MainClass {
  public static void main(final String args[]) {
    final JFrame frame = new JFrame("Frame Key");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Action actionListener = new AbstractAction() {
      public void actionPerformed(ActionEvent actionEvent) {
        System.out.println("Got an M");
      }
    };
    JPanel content = (JPanel) frame.getContentPane();
    KeyStroke stroke = KeyStroke.getKeyStroke("M");
    InputMap inputMap = content.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, "OPEN");
    content.getActionMap().put("OPEN", actionListener);
    frame.setSize(300, 300);
    frame.setVisible(true);
  }
}





JFrame: getDefaultCloseOperation()

  

import javax.swing.JFrame;
public class Main {
  public static void main(String[] argv) throws Exception {
    // Create a frame
    JFrame frame = new JFrame();
    // Get default close operation
    int op = frame.getDefaultCloseOperation(); // HIDE_ON_CLOSE
    // Set to ignore the button
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  }
}





JFrame: getFontMetrics(Font font)

  
import java.awt.FontMetrics;
import javax.swing.JFrame;
public class Main {
  public static void main(String args[]) {
    JFrame f = new JFrame("JColorChooser Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    f.setSize(300, 200);
    f.setVisible(true);
    
    FontMetrics metrics = f.getFontMetrics(f.getFont());
    int widthX = metrics.charWidth("X");
    System.out.println(widthX);
    
  }
}





JFrame: getGlassPane()

  
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainClass {
  public static void main(String[] args) {
    JFrame f = new JFrame();
    
    final JPanel p1 = new JPanel();
    p1.add(new JLabel("GlassPane Example"));
    JButton show = new JButton("Show");
    p1.add(show);
    p1.add(new JButton("No-op"));
    f.getContentPane().add(p1);
    final JPanel glass = (JPanel)f.getGlassPane();
    glass.setVisible(true);
    glass.setLayout(new GridBagLayout());
    JButton glassButton = new JButton("Hide");
    glass.add(glassButton);
    f.setSize(150, 80);
    f.setVisible(true);
    show.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        glass.setVisible(true);
        p1.repaint();
      }
    });
    glassButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        glass.setVisible(false);
        p1.repaint();
      }
    });
  }
}





JFrame: getLayeredPane()

  

import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
public class Main extends JFrame {
  public Main() {
    super("LayeredPane");
    setSize(200, 150);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JLayeredPane lp = getLayeredPane();
    // Create 3 buttons
    JButton top = new JButton();
    top.setBackground(Color.white);
    top.setBounds(20, 20, 50, 50);
    JButton middle = new JButton();
    middle.setBackground(Color.gray);
    middle.setBounds(40, 40, 50, 50);
    JButton bottom = new JButton();
    bottom.setBackground(Color.black);
    bottom.setBounds(60, 60, 50, 50);
    // Place the buttons in different layers
    lp.add(middle, new Integer(2));
    lp.add(top, new Integer(3));
    lp.add(bottom, new Integer(1));
  }
  public static void main(String[] args) {
    Main sl = new Main();
    sl.setVisible(true);
  }
}





JFrame: isFocusOwner()

  
import javax.swing.JFrame;
public class Main {
  public static void main() {
    JFrame f = new JFrame();
    boolean b = f.isFocusOwner();
  }
}





JFrame: isUndecorated()

  

import java.awt.Frame;
public class Main {
  public static void main() {
    Frame frame = new Frame();
    frame.setUndecorated(true);
    // Get the current decorated state
    boolean undecorated = frame.isUndecorated();
  }
}





JFrame: processWindowEvent(WindowEvent e)

  
 
import java.awt.event.WindowEvent;
import javax.swing.JFrame;

public class Main extends JFrame {
  public Main(String title) {
    setTitle(title); 
    // setDefaultCloseOperation(EXIT_ON_CLOSE);
    enableEvents(java.awt.AWTEvent.WINDOW_EVENT_MASK);
  }
  protected void processWindowEvent(WindowEvent e) {
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.out.println(WindowEvent.WINDOW_CLOSING);
      dispose(); 
      System.exit(0);
    }
    super.processWindowEvent(e); // Pass on the event
  }
  public static void main(String[] a) {
    Main window = new Main("Sketcher");
    window.setBounds(30, 30, 300, 300);
    window.setVisible(true);
  }
}





JFrame: setAlwaysOnTop(boolean alwaysOnTop)

  
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main{
  public static void main(String[] args) {
    JFrame frame = new JFrame("Hello!!");
    frame.setAlwaysOnTop(true);
    frame.setLocationByPlatform(true);
    frame.add(new JLabel("  Always visible"));
    frame.pack();
    frame.setVisible(true);
  }
}





JFrame: setBackground(Color c)

  
 
import java.awt.Color;
import java.awt.ruponent;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Main extends JFrame {
  public Main() {
    this.setSize(400, 100);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout(FlowLayout.CENTER));
    JButton button = new JButton("Change Frame Color");
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        Component component = (Component) e.getSource();
        JFrame frame = (JFrame) SwingUtilities.getRoot(component);
        frame.setBackground(Color.RED);
      }
    });
    this.getContentPane().add(button);
  }
  public static void main(String[] args) {
    new Main().setVisible(true);
  }
}





JFrame: setCursor(Cursor c)

  
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class MainClass {
  public static void main(String[] args) {
    JFrame aWindow = new JFrame();
    aWindow.setBounds(200, 200, 200, 200);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    aWindow.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
    aWindow.getContentPane().setBackground(Color.PINK);
    aWindow.setVisible(true);
  }
}





JFrame: setDefaultButton(JButton bn) (Remove "Enter" key stroke from JTextField)

  
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.text.Keymap;
public class MainClass {
  public static void main(final String args[]) {
    JFrame frame = new JFrame("Default Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextField textField = new JTextField();
    frame.add(textField, BorderLayout.NORTH);
    JPanel panel = new JPanel();
    JButton defaultButton = new JButton("Default Button");
    defaultButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        System.out.println(actionEvent.getActionCommand() + " selected");
      }
    });
    panel.add(defaultButton);
    JButton otherButton = new JButton("Other Button");
    otherButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        System.out.println(actionEvent.getActionCommand() + " selected");
      }
    });
    panel.add(otherButton);
    frame.add(panel, BorderLayout.SOUTH);
    Keymap keymap = textField.getKeymap();
    KeyStroke keystroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);
    keymap.removeKeyStrokeBinding(keystroke);
    frame.getRootPane().setDefaultButton(defaultButton);
    frame.setSize(250, 150);
    frame.setVisible(true);
  }
}





JFrame: setDefaultButton(JButton defaultButton)

  
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRootPane;
public class MainClass {
  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);
    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);
  }
}





JFrame: setDefaultCloseOperation(int option)

  
/*
 * Output:
 *  
 */
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
  public void paint(Graphics g) {
    Dimension d = this.getPreferredSize();
    int fontSize = 20;
    g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));
     
    g.setColor(Color.red);
    
    g.drawString("www.jexp.ru", 10, 20);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MainClass());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200,200);
    frame.setVisible(true);
  }
}





JFrame: setDefaultLookAndFeelDecorated(boolean b)

  
import javax.swing.JFrame;
public class MainClass {
  public static void main(final String args[]) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("Adornment Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 100);
    frame.setVisible(true);
  }
}





JFrame: setExtendedState(int state)

  

import java.awt.Frame;
public class Main {
  public static void main() {
    Frame frame = new Frame();
    frame.setSize(300, 300);
    frame.setVisible(true);
    iconify(frame);
  }
  public static void iconify(Frame frame) {
    int state = frame.getExtendedState();
    // Set the iconified bit
    state |= Frame.ICONIFIED;
    // Iconify the frame
    frame.setExtendedState(state);
  }
}





JFrame: setFocusable(boolean focusable)

  
import javax.swing.JFrame;
public class Main {
  public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    frame.setFocusable(false);
  }
}





JFrame: setFocusableWindowState(boolean focusableWindowState)

  

import javax.swing.JFrame;
public class Main {
  public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    frame.setFocusableWindowState(false);
  }
}





JFrame: setFocusTraversalPolicy(FocusTraversalPolicy lo)

  
import java.awt.ruponent;
import java.awt.Container;
import java.awt.FocusTraversalPolicy;
import java.awt.GridLayout;
import java.util.Arrays;
import java.util.ruparator;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SortingFocusTraversalPolicy;
public class MainClass {
  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 = 9; i > 0; i--) {
      JButton button = new JButton(Integer.toString(i));
      frame.add(button, 0);
    }
    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);
  }
}





JFrame: setIconImage(Image image)

  
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JFrame;
/*
 */
public class MainClass {
  public static void main(String[] unused) {
    JFrame f = new JFrame("FrameIcon");
    Image im = Toolkit.getDefaultToolkit().getImage("FrameIcon.gif");
    f.setIconImage(im);
    f.setSize(100, 100);
    f.setLocation(200, 200);
    f.setVisible(true);
  }
}





JFrame: setJMenuBar(JMenuBar bar)

  
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JRadioButtonMenuItem;
public class MainClass {
  public static void main(String args[]) {
    JFrame f = new JFrame("JRadioButtonMenuItem Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar bar = new JMenuBar();
    JMenu menu = new JMenu("Options");
    menu.setMnemonic(KeyEvent.VK_O);
    ButtonGroup group = new ButtonGroup();
    JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem("A");
    group.add(menuItem);
    menu.add(menuItem);
    menuItem = new JRadioButtonMenuItem("B");
    group.add(menuItem);
    menu.add(menuItem);
    menuItem = new JRadioButtonMenuItem("C");
    group.add(menuItem);
    menu.add(menuItem);
    bar.add(menu);
    f.setJMenuBar(bar);
    f.setSize(300, 200);
    f.setVisible(true);
  }
}





JFrame: setLocationByPlatform(boolean locationByPlatform)

  
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main{
  public static void main(String[] args) {
    JFrame frame = new JFrame("Hello!!");
    frame.setAlwaysOnTop(true);
    frame.setLocationByPlatform(true);
    frame.add(new JLabel("  Always visible"));
    frame.pack();
    frame.setVisible(true);
  }
}





JFrame: setLocation(int x, int y)

  
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class Main extends JFrame {
  public Main() {
    setTitle("CenteredFrame");
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension screenSize = tk.getScreenSize();
    int screenHeight = screenSize.height;
    int screenWidth = screenSize.width;
    setSize(screenWidth / 2, screenHeight / 2);
    setLocation(screenWidth / 4, screenHeight / 4);
  }
  public static void main(String[] args) {
    JFrame frame = new Main();
    frame.setVisible(true);
  }
}





JFrame: setLocationRelativeTo(Component c)

  
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Main {
  public static void main(String[] args) {
    JFrame f = new JFrame();
    final JSlider slider = new JSlider(0, 150, 0);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    slider.setPreferredSize(new Dimension(150, 30));
    slider.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent event) {
        int value = slider.getValue();
        if (value == 0) {
          System.out.println("0");
        } else if (value > 0 && value <= 30) {
          System.out.println("value > 0 && value <= 30");
        } else if (value > 30 && value < 80) {
          System.out.println("value > 30 && value < 80");
        } else {
          System.out.println("max");
        }
      }
    });
    f.add(slider);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}





JFrame: setMaximizedBounds(Rectangle bounds)

  

import java.awt.Frame;
import java.awt.Rectangle;
public class Main {
  public static void main() {
    Frame frame = new Frame();
    Rectangle bounds = new Rectangle(20, 20, 200, 200);
    frame.setMaximizedBounds(bounds);
    frame.setVisible(true);
  }
}





JFrame: setSize(int width, int height)

  
/*
 * Output:
 *  
 */
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
  public void paint(Graphics g) {
    Dimension d = this.getPreferredSize();
    int fontSize = 20;
    g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));
     
    g.setColor(Color.red);
    
    g.drawString("www.jexp.ru", 10, 20);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MainClass());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200,200);
    frame.setVisible(true);
  }
}





JFrame: setTitle(String title)

  
import javax.swing.JFrame;
public class Main extends JFrame {
  public static void main(String[] args) {
    new Main();
  }
  public Main() {
    this.setSize(200, 100);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Hello World!");
    this.setVisible(true);
  }
}





JFrame: setUndecorated(boolean b)

  
import javax.swing.JFrame;
import javax.swing.JRootPane;
public class MainClass {
  public static void main(final String args[]) {
    JFrame frame = new JFrame("Adornment Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setUndecorated(true);
    frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
    frame.setSize(300, 100);
    frame.setVisible(true);
  }
}





JFrame: setVisible(boolean visible)

  
/*
 * Output:
 *  
 */
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
  public void paint(Graphics g) {
    Dimension d = this.getPreferredSize();
    int fontSize = 20;
    g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));
     
    g.setColor(Color.red);
    
    g.drawString("www.jexp.ru", 10, 20);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MainClass());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200,200);
    frame.setVisible(true);
  }
}