问题描述
我不确定为什么无法在下面的代码中调整按钮的位置.我有几行设定了边界,但是我一定缺少一些东西.
I'm not sure why I can't adjust the location of my buttons in the code below. I've got a couple of lines that set the boundries, but I must be missing something.
public DayGUI()
{
mainFrame = new JFrame("What Kind Of Day Are You Having?");
btnGood = new JButton("Good");
btnBad = new JButton("Bad");
btnGood.setBounds(10, 10, 80, 20);
btnBad.setBounds(10, 30, 80, 20);
Container c = mainFrame.getContentPane();
c.setLayout(new FlowLayout());
c.add(btnGood);
c.add(btnBad);
推荐答案
您可以使用GridLayout
,GridBagLayout
,SpringLayout
,GroupLayout
等来对齐按钮.
You can align your buttons using a GridLayout
, a GridBagLayout
, a SpringLayout
, a GroupLayout
, and others.
这是有关GridBagLayout的教程.在GridLayout上这里是一个. 这是GroupLayout上的一个.
This is a tutorial on GridBagLayout. Here is one on GridLayout. Here is one on GroupLayout.
这是布局管理器的概述.布局管理器允许您定义容器中与容器之间以及彼此之间的关系.他们每个人都有自己的优点和缺点.
This is the overview on layout managers. Layout managers allow you to define components in a container in relationship to the container and each other. They each have their own strengths and weaknesses.
出于您的需要,我会考虑GridBagLayout.
For your needs I would consider GridBagLayout.
这篇关于对齐JButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!