Java GUI:如何正确设置布局
我在设置布局方面遇到了一些问题。(不要介意按钮的大小,我只想正确理解布局) 这是我想制作的窗口: 这是我试过的窗口: 这是我的密码:Java GUI:如何正确设置布局,java,swing,user-interface,layout,layout-manager,Java,Swing,User Interface,Layout,Layout Manager,我在设置布局方面遇到了一些问题。(不要介意按钮的大小,我只想正确理解布局) 这是我想制作的窗口: 这是我试过的窗口: 这是我的密码: import javax.swing.*; import java.awt.event.*; import java.awt.*; class Grid extends JFrame { private JTextField t1; private JTextField t2; private JCheckBox c1; pr
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class Grid extends JFrame
{
private JTextField t1;
private JTextField t2;
private JCheckBox c1;
private JCheckBox c2;
private JButton b1;
private JButton b2;
private JButton b3;
private JPanel ButtonPanel1;
private JPanel ButtonPanel2;
private JPanel ButtonPanel3;
private JPanel CheckPanel1;
private JPanel CheckPanel2;
private JPanel TextPanel1;
private JPanel TextPanel2;
private JPanel EastPanel;
private JPanel CenterPanel;
private JPanel WestPanel;
private JLabel l1;
private JLabel l2;
public Grid()
{
//CheckBoxes
c1 = new JCheckBox("Snap to Grid");
c2 = new JCheckBox("Show Grid");
CheckPanel1 = new JPanel();
CheckPanel1.add(c1);
CheckPanel2 = new JPanel();
CheckPanel2.add(c2);
WestPanel = new JPanel(new GridLayout(2,1));
WestPanel.add(c1);
WestPanel.add(c2);
add(WestPanel,BorderLayout.WEST);
//I don't get how to arrange the labels aligned with the textfields.
//TextFields
l1 = new JLabel("X:");
t1 = new JTextField("8",3);
l2 = new JLabel("Y:");
t2 = new JTextField("8",3);
TextPanel1 = new JPanel();
TextPanel1.add(l1);
TextPanel1.add(t1);
TextPanel2 = new JPanel();
TextPanel2.add(l2);
TextPanel2.add(t2);
CenterPanel = new JPanel(new GridLayout(2,1));
CenterPanel.add(l1);
CenterPanel.add(l2);
CenterPanel.add(t1);
CenterPanel.add(t2);
add(CenterPanel,BorderLayout.CENTER);
// Buttons
b1 = new JButton("Ok");
b2 = new JButton("Cancel");
b3 = new JButton("Help");
ButtonPanel1 = new JPanel();
ButtonPanel1.add(b1);
ButtonPanel2 = new JPanel();
ButtonPanel2.add(b2);
ButtonPanel3 = new JPanel();
ButtonPanel3.add(b3);
EastPanel = new JPanel(new GridLayout(3,1));
EastPanel.add(ButtonPanel1);
EastPanel.add(ButtonPanel2);
EastPanel.add(ButtonPanel3);
add(EastPanel, BorderLayout.EAST);
}
}
public class oottest {
public static void main(String[] args) {
Grid app = new Grid();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(300,150);
app.setVisible(true);
}
}
有人能帮我理解布局是如何工作的吗
我已经看了很多视频,尝试了不同的布局,但仍然不能得到它的权利。告诉我是否使用了正确的布局或是否应该更改布局。我会对右侧的按钮使用单列
GridLayout
,对左侧的所有按钮使用gridbagloayout
。将带有按钮的面板放在边框布局的行的末端
,其余的放在中间
下面是它的外观:
调整所需空白的边距、布局填充、插入和空边框。(不包括电池。)我会对右边的按钮使用单列GridBagLayout
,对左边的所有按钮使用GridBagLayout
。将带有按钮的面板放在边框布局的行的末端
,其余的放在中间
下面是它的外观:
调整所需空白的边距、布局填充、插入和空边框。(不包括电池。)答案中建议的GridBagLayout
可以为左侧部分提供所需的所有控制
如果您愿意为了简单而牺牲一些控制,您可以在网格布局中使用两个FlowLayout
面板:
更多应用此策略的示例:和GridBagLayout
,如答案中所建议,可以提供您对左侧部分所需的所有控制
如果您愿意为了简单而牺牲一些控制,您可以在网格布局中使用两个FlowLayout
面板:
更多应用此策略的示例:您是否尝试过简单的谷歌搜索或阅读过Java教程?为了让你的代码更容易阅读,请遵循。嘿,很抱歉回复太晚了。是的,我尝试了许多不同的网站,如果我不能很好地理解布局,我很抱歉。谢谢@c0der提供的提示和帮助。如果我的代码很混乱,我很抱歉,我会尽我最大的努力改变我。你是否尝试过简单的谷歌搜索或阅读过任何Java教程?为了让你的代码更容易阅读,请遵循。嘿,很抱歉回复太晚了。是的,我尝试了许多不同的网站,如果我不能正确理解布局,我很抱歉。谢谢@c0der提供的提示和帮助。如果我的代码混乱,我很抱歉,我会尽力更改它。非常感谢您的帮助非常感谢您的帮助谢谢您帮助我,并花时间让我理解,我真的很感激。哇,你真的让我很清楚地了解了那个男人!我真是太感谢你了!!我很高兴这有帮助。谢谢你的反馈。我喜欢一个好的截图,这是一个非常有教育意义的截图(不仅很好,而且非常好)。谢谢你@AndrewThompson,我正在尝试跟随你的设置(0:谢谢你帮助我,并花时间让我理解,我真的很感激。哇,你真的让我明白了那个非常清楚的人!我真的非常感谢你!!我很高兴它有帮助。谢谢你的反馈。我喜欢一个好的截图,这是一个非常有教育意义的(不仅很好,而且非常棒)屏幕截图。谢谢@AndrewThompson我正在尝试跟随you设置(0:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
class Grid extends JFrame
{
private JTextField t1, t2;
private JCheckBox c1, c2;
private JButton b1, b2, b3;
private JPanel topPanel, bottomPanel;
private JPanel eastPanel, centerPanel;
private JLabel l1, l2;
public Grid()
{
c1 = new JCheckBox("Snap to Grid");
l1 = new JLabel("X:");
t1 = new JTextField("8",3);
topPanel = new JPanel();//uses flow layout by default
topPanel.add(c1); topPanel.add(l1); topPanel.add(t1);
c2 = new JCheckBox("Show Grid");
l2 = new JLabel("Y:");
t2 = new JTextField("8",3);
bottomPanel = new JPanel();
bottomPanel.add(c2); bottomPanel.add(l2); bottomPanel.add(t2);
centerPanel = new JPanel(new GridLayout(2,1));
centerPanel.add(topPanel);
centerPanel.add(bottomPanel);
add(centerPanel,BorderLayout.CENTER);
b1 = new JButton("Ok");
b2 = new JButton("Cancel");
b3 = new JButton("Help");
eastPanel = new JPanel(new GridLayout(3,1));
eastPanel.add(b1);
eastPanel.add(b2);
eastPanel.add(b3);
add(eastPanel, BorderLayout.EAST);
}
public static void main(String[] args) {
Grid app = new Grid();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(300,150);
app.setVisible(true);
}
}