Example usage for java.awt Dimension Dimension

List of usage examples for java.awt Dimension Dimension

Introduction

In this page you can find the example usage for java.awt Dimension Dimension.

Prototype

public Dimension(int width, int height) 

Source Link

Document

Constructs a Dimension and initializes it to the specified width and specified height.

Usage

From source file:AboutDialog.java

public AboutDialog() {
    setTitle("About");
    setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

    add(Box.createRigidArea(new Dimension(0, 10)));

    JLabel name = new JLabel("Notes");
    name.setAlignmentX(0.5f);//from  w ww .ja v  a  2 s . c om
    add(name);

    add(Box.createRigidArea(new Dimension(0, 100)));

    JButton close = new JButton("Close");
    close.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            dispose();
        }
    });

    close.setAlignmentX(0.5f);
    add(close);
    setModalityType(ModalityType.APPLICATION_MODAL);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setSize(300, 200);
}

From source file:Main.java

/** Gets the dimension of this window were it to be repacked. */
public static Dimension getRepackSize(final Window w) {
    final Dimension size = w.getSize();
    final Dimension pref = w.getPreferredSize();
    if (size.width >= pref.width && size.height >= pref.height)
        return size;
    return new Dimension(size.width < pref.width ? pref.width : size.width,
            size.height < pref.height ? pref.height : size.height);
}

From source file:Main.java

public Main() throws HeadlessException {
    setSize(200, 200);/*from w  ww .j  a v a2s .c o  m*/
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout(FlowLayout.LEFT));

    JLabel usernameLabel = new JLabel("Username: ");
    JTextField usernameTextField = new JTextField();
    usernameTextField.setPreferredSize(new Dimension(100, 20));
    add(usernameLabel);
    add(usernameTextField);

    usernameTextField.addKeyListener(new KeyAdapter() {
        public void keyReleased(KeyEvent e) {
            JTextField textField = (JTextField) e.getSource();
            String text = textField.getText();
            textField.setText(text.toUpperCase());
        }

        public void keyTyped(KeyEvent e) {
        }

        public void keyPressed(KeyEvent e) {
        }
    });
}

From source file:Main.java

/** Constructs a JButton with an icon from the given file id. */
public static JButton makeButton(final Object owner, final String id, final String altText, final int wpad,
        final int hpad) {
    final URL url = owner.getClass().getResource(id);
    ImageIcon icon = null;// ww w .  j  a va  2s. c  om
    if (url != null)
        icon = new ImageIcon(url);
    JButton button;
    if (icon == null)
        button = new JButton(altText);
    else {
        button = new JButton(icon);
        button.setPreferredSize(new Dimension(icon.getIconWidth() + wpad, icon.getIconHeight() + hpad));
    }
    return button;
}

From source file:Main.java

public Main() {
    JEditorPane editorPane = new JEditorPane();
    JScrollPane scrollPane = new JScrollPane();

    this.setPreferredSize(new Dimension(300, 300));
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    getContentPane().setLayout(new BorderLayout());
    editorPane.setEditorKit(new PreWrapHTMLEditorKit());
    editorPane.setText("" + "<html>" + "<head></head>" + "<body>"
            + "<pre>long text line long text line long text line long text line (two new lines here!)\n\n"
            + "long text line long text line long text line long text line long text line long text line</pre>"
            + "</body>" + "</html>");
    scrollPane.setViewportView(editorPane);
    getContentPane().add(scrollPane);/*w w w .java 2 s  .co m*/
    pack();
}

From source file:MainClass.java

MainClass() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Vector v = new Vector();
    v.add("First item");
    v.add("Second item");
    v.add("Third item");
    v.add("Fourth item");

    JPanel p = new JPanel();
    p.setPreferredSize(new Dimension(200, 100));
    JList jl = new JList(v);
    jl.setPreferredSize(new Dimension(100, 75));
    p.add(new JScrollPane(jl));

    getContentPane().add(p);//from www .jav a 2 s. c o m

    pack();
    setVisible(true);
}

From source file:Main.java

public Main() throws HeadlessException {
    this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    JButton button = new JButton("Close");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);/*from w  w  w .ja  v a2 s  .c o  m*/
        }
    });

    this.setLayout(new FlowLayout(FlowLayout.CENTER));
    this.setSize(new Dimension(100, 100));
    this.getContentPane().add(button);
}

From source file:org.jfree.chart.demo.DefaultXYDatasetDemo1.java

public DefaultXYDatasetDemo1(String s) {
    super(s);//from w  w w.  ja v a2s .c o  m
    JPanel jpanel = createDemoPanel();
    jpanel.setPreferredSize(new Dimension(500, 270));
    setContentPane(jpanel);
}

From source file:Main.java

/**
 * Determine the maximum size for a 2-state button with the specified text and icons.
 * This can be used to make sure that a button doesn't resize during state change.
 *
 * @param button  the UI of this JButton is used for size determination
 * @param string1 text for 1st mode//from   w w  w  .  j av  a  2  s  .  c o m
 * @param icon1   icon for 1st mode
 * @param string2 text for 2nd mode
 * @param icon2   icon for 2nd mode
 * @return the Dimension that contains both modes for the button.
 */
public static Dimension getMaxDimension(JButton button, String string1, ImageIcon icon1, String string2,
        ImageIcon icon2) {

    String originalText = button.getText();
    Icon originalIcon = button.getIcon();

    // Get dimensions for "Play" state
    button.setText(string1);
    button.setIcon(icon1);
    Dimension playSize = button.getUI().getPreferredSize(button);

    // Get dimensions for "Pause" state
    button.setText(string2);
    button.setIcon(icon2);
    Dimension pauseSize = button.getUI().getPreferredSize(button);

    // Restore original text and icon
    button.setText(originalText);
    button.setIcon(originalIcon);

    // Return max dimensions
    int maxWidth = (int) Math.max(playSize.getWidth(), pauseSize.getWidth());
    int maxHeight = (int) Math.max(playSize.getHeight(), pauseSize.getHeight());
    return new Dimension(maxWidth, maxHeight);
}

From source file:org.jfree.chart.demo.DefaultXYDatasetDemo2.java

public DefaultXYDatasetDemo2(String s) {
    super(s);/*from   w  w  w .j ava 2s .  c o  m*/
    JPanel jpanel = createDemoPanel();
    jpanel.setPreferredSize(new Dimension(500, 270));
    setContentPane(jpanel);
}