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");
}
}
我正在尝试将buttonLeft和buttonRight与示例文本的中心对齐,并在调整屏幕大小时保持对齐,如下所示:
example where buttons should be, note they aren't actually there in the program
任何知道如何使用MigLayout帮助的人都可以吗?感谢
答案 0 :(得分:0)
您可以嵌套具有不同布局的组件。这里有一个使用FlowLayout和MigLayout的可能解决方案:
public void monthScreen() {
JButton buttonLeft = new JButton();
buttonLeft.setPreferredSize(new Dimension(20, 20)); // for correct size
JButton buttonRight = new JButton();
buttonRight.setPreferredSize(new Dimension(20, 20)); // for correct size
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");
JPanel panel = new JPanel(); // add components here
FlowLayout fl = new FlowLayout();
fl.setHgap(0); // for some space/insets if u want
fl.setVgap(0); // for some space/insets if u want
panel.setLayout(fl);
panel.add(buttonLeft);
panel.add(label);
panel.add(buttonRight);
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(panel, "cell 0 1, span, align 50% 0%, id label"); // Dont forget to add the new panel
// panel1.add(buttonLeft, "id bl, cell 0 1");
// panel1.add(buttonRight, "id br, cell 0 1");
}