Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.WindowEvent; 
import java.awt.event.WindowListener;

import javax.swing.JButton; 
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import net.miginfocom.swing.*;

public class Main{

private JFrame frame = new JFrame("Super Simple");
private JPanel panel1 = new JPanel(); //calendar of month

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try{
                Main m = new Main();
                m.monthScreen();
            } catch (Exception e){

            }
        }
    });
}

public Main(){
    panel1.setLayout(new MigLayout("", "[14.2%][14.2%][14.2%][14.2%][14.2%][14.2%][14.2%]", 
            "[12.5%][12.5%][12.5%][12.5%][12.5%][12.5%][12.5%][12.5%]"));
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension d = tk.getScreenSize();
    frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); //tells the window listener to handle closing
    frame.addWindowListener(new WindowListener(){

        @Override
        public void windowActivated(WindowEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void windowClosed(WindowEvent arg0) {
        }

        @Override
        public void windowClosing(WindowEvent arg0) {
            System.out.println("Closing!");
            System.exit(0);
        }

        @Override
        public void windowDeactivated(WindowEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void windowDeiconified(WindowEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void windowIconified(WindowEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void windowOpened(WindowEvent arg0) {
            // TODO Auto-generated method stub

        }

    });
    frame.getContentPane().add(panel1);
    frame.setBounds(100, 100, (int)(d.width/1.5), (int)(d.height/1.3)); // roughly 75% size of screen on opening
    d.setSize(600, 600); //minimum that the screen can resized to.
    frame.setMinimumSize(d);
    JMenuBar menuBar;
    JMenu menu;
    menuBar = new JMenuBar();
    menu = new JMenu("A Menu");
    menuBar.add(menu);
    frame.setJMenuBar(menuBar);
    frame.setVisible(true); 
}

public void monthScreen(){
    JButton buttonLeft = new JButton();
    JButton buttonRight = new JButton();
    Dimension d = new Dimension();
    d.setSize(20, 20);
    buttonLeft.setMinimumSize(d);
    buttonLeft.setMaximumSize(d);
    buttonRight.setMinimumSize(d);
    buttonRight.setMaximumSize(d);
    JLabel label = new JLabel("Example");
    try { //in case they don't have the font, use the systems default font
    Font tr = new Font("Impact", Font.PLAIN, 18);
    label.setFont(tr);
    } catch (Exception e){
        Font defaultFont = new JLabel().getFont();
        Font tr = new Font(defaultFont.toString(), Font.PLAIN, 18);
        label.setFont(tr);
    }
    panel1.add(label, "cell 0 1, span, align 50% 0%, id label");
    panel1.add(buttonLeft, "id bl, cell 0 1");
    panel1.add(buttonRight, "id br, cell 0 1");
    }
}

I'm trying to get buttonLeft and buttonRight to align with the center of the example text, and to stay aligned when the screen is resized, like this:

http://i.stack.imgur.com/LeBIG.png

Can anyone who knows how to use MigLayout help? Thanks

What I have tried:

I've tried alot of different parameters for panel1.add(buttonLeft, "id bl, cell 0 1");
panel1.add(buttonRight, "id br, cell 0 1"); and changed the entire panel's layout once
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900