除了使用GridBagConstraints之外,还有更精确的方法来放置我的JButton吗?

时间:2016-03-27 00:34:12

标签: java swing jbutton layout-manager gridbaglayout

package swingtraining;

import static java.awt.Color.BLACK;
import java.awt.GridBagConstraints;
import static java.awt.GridBagConstraints.CENTER;
import static java.awt.GridBagConstraints.NORTH;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JFrameTest extends JFrame {

    public JFrameTest() {
        setSize(500, 500);
        setTitle("Hello :D");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
    }

    public static class JPanelTest extends JPanel {

        public JPanelTest() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.ipadx = 100;
            gbc.ipady = 0;
            gbc.anchor = GridBagConstraints.WEST;
            gbc.weightx = 1.0;
            gbc.weighty = 1.0;
            setBackground(BLACK);
            setOpaque(true);
            add(new JButton("Hello"), gbc);
        }
    }

    public static class JButtonTest extends JButton {

        JButtonTest() {
        }
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrameTest T = new JFrameTest();
                JPanelTest Jp1 = new JPanelTest();
                T.add(Jp1);
            }
        });
    }
}

此代码工作正常,但我不喜欢我的Button如何恰好放在左边,只能改变bButton的大小(宽度,高度)。所以我想知道是否有更精确的方式来放置东西,比如可能只是告诉Java将它放在Panel上使用X Y坐标或类似的东西,如;

JButton.setLocation(200,200);

有什么建议吗?

2 个答案:

答案 0 :(得分:3)

  

所以我想知道是否有更精确的方式来放置东西,比如可能只是告诉Java使用X Y坐标或类似的东西将它放在Panel上;

您可能不会欣赏它,但布局管理人员非常强大并且非常擅长工作,这大大减轻了您的工作量并使您的生活变得更加轻松

char *

所以我真正做的就是将GridBagConstraints#insets约束更改为anchor并添加GridBagConstraints.NORTHWEST

Example

红色来自自定义gbc.insets = new Insets(200, 200, 0, 0);正在绘制一个以200x200为中心的点,但因为glassPane没有填满整个区域,所以我添加了{{1}它发生JButton事件发生时ComponentListener打印出200x20

location

答案 1 :(得分:-2)

import javax.swing.*;
import java.awt.*;


public class JFrameTest extends JFrame{

   JPanel pnl = new JPanel();

 public static void main(String[] args){
      SwingUtilities.invokeLater(new Runnable(){
        public void run(){                  
             JFrameTest myFrame = new JFrameTest();
                   myFrame.setTitle("Hello :D");                 
                   myFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                   myFrame.pack();
                   myFrame.setLocationRelativeTo(null);
                   myFrame.setResizable(false);
                   myFrame.setVisible(true);
                }});
           }

public JFrameTest(){

MyCustomLayout customLayout = new MyCustomLayout();
Container c = getContentPane();
          c.setLayout(customLayout);
          pnl.setBackground(Color.BLACK);
          c.add(pnl);
          c.add(new JButton("Hello"));
}

}

class MyCustomLayout implements LayoutManager {

public MyCustomLayout() {}
public void addLayoutComponent(String name, Component comp) {}
public void removeLayoutComponent(Component comp) {}

public Dimension preferredLayoutSize(Container parent) {

    Dimension dim = new Dimension(0, 0);

    Insets insets = parent.getInsets();                
    dim.width = 500;
    dim.height = 500;
    return dim;

}

   public Dimension minimumLayoutSize(Container parent) {
    Dimension dim = new Dimension(0, 0);
    return dim;
}

   public void layoutContainer(Container parent) {

    Insets insets = parent.getInsets();
    Component c;

      c = parent.getComponent(0);
    if (c.isVisible())
       {c.setBounds(insets.left+1,insets.top+8,930,720);}
      c = parent.getComponent(1);
    if (c.isVisible())
       {c.setBounds(insets.left+250,insets.top+250,80,25);} 
//Here just set left value and top value for xy positions
         }
}