为什么我不能在我的JPanel上调整JButton的大小或重新定位?

时间:2016-03-26 06:45:52

标签: java swing jbutton

package swingtraining;

import static java.awt.Color.BLACK;
import static java.awt.Color.RED;
import java.awt.EventQueue;
import static java.awt.Font.BOLD;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;



public class JFrameWithAButton extends JFrame {

public JFrameWithAButton(){

    setSize(400,400);
    setTitle("Swing is hard");
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);

    }

    public static void main(String args[]){

    JPanel Jp1 = new JPanel();

    Jp1.setOpaque(true);

    Jp1.setBackground(RED);

    JButton Jbt = new JButton();

    Jbt.setLayout(null);  
    Jbt.setSize(200,200);

    Jbt.setBounds(new Rectangle(new Point(200, 200)));
    Jbt.setText("Hello!");

    EventQueue.invokeLater(new Runnable(){

        public void run(){

            JFrameWithAButton ex = new JFrameWithAButton();
            ex.setVisible(true); 
            ex.add(Jp1);
            Jp1.add(Jbt);
            }
      });
 }

}

很抱歉,如果代码有点妈妈的意大利面条,但我只是不能破解这个cookie>。>即使布局设置为null,它也不会移动。有关如何让这个JButton不仅移动到窗口中间而且还增加200 x 200像素的任何建议?

2 个答案:

答案 0 :(得分:2)

  

有关如何让这个JButton不仅移动到窗口中间而且还增加200 x 200像素的任何建议?

我可以想到一些,其中没有一个使用null布局

GridBagConstraints

GridBagConstraints

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.ipadx = 200;
            gbc.ipady = 200;

            add(new JButton("Hello"), gbc);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

    }

}

JButton#setMargin

JButton margins

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();

            JButton btn = new JButton("Hello");
            btn.setMargin(new Insets(100, 100, 100, 100));
            add(btn);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

    }

}

EmptyBorder

EmptyBorder

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            setBorder(new EmptyBorder(50, 50, 50, 50));
            JButton btn = new JButton("Hello");
            add(btn);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

    }

}

您可以使用它们的组合,可能使用EmptyBorderGridBagConstraints来进一步约束布局。

这些示例的好处是,在大多数情况下,如果字体大小发生变化或字体的渲染要求发生变化,布局就能够补偿

避免使用null布局,像素完美布局是现代ui设计中的一种幻觉。影响组件个体大小的因素太多,您无法控制。 Swing旨在与布局管理器一起工作,放弃这些将导致问题和问题的终结,您将花费越来越多的时间来纠正

因为它总是很有趣,Why is it frowned upon to use a null layout in SWING?

答案 1 :(得分:-1)

如果您想手动定义任何组件大小,则必须设置母组件的布局:null 所以你必须设置Jframe layout null来定义Jpanel的大小和位置 那么你必须设置JPanel布局null来定义其中的Jbutton大小和位置

final JPanel Jp1 = new JPanel();
    Jp1.setOpaque(true);
    Jp1.setBackground(RED);
    Jp1.setLayout(null);
    final JButton Jbt = new JButton();
    // Jbt.setLayout(null); not needed!
    Jbt.setBounds(10, 10, 100, 40);
    // Jbt.setBounds(new Rectangle(new Point(200, 200))); not in this style
    Jbt.setText("Hello!");
    Jp1.add(Jbt);
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            JFrameWithAButton ex = new JFrameWithAButton();
            ex.setVisible(true);
            ex.add(Jp1);
        }
    });
  • 当您在空布局Jpanel或Jframe中添加组件时,不要忘记定义大小和位置......