我想将我的两个组件与窗口的左上角对齐。
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
public class MainFrame extends JFrame {
public MainFrame() {
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel gridbagPanel = new JPanel();
this.setLayout(new BorderLayout());
gridbagPanel.setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
JLabel nameLabel = new JLabel(player.getName());
nameLabel.setHorizontalAlignment(SwingConstants.CENTER);
nameLabel.setFont(new Font("Serif",Font.PLAIN,24));
mainPanel.add(nameLabel, BorderLayout.NORTH);
JLabel money = new JLabel("Pinigai: "+new Integer(player.getMoney()).toString());
gc.gridx = 0;
gc.gridy = 0;
gc.anchor = GridBagConstraints.PAGE_START;
gc.insets = new Insets(2,0,0,2);
gridbagPanel.add(money,gc);
JLabel job = new JLabel("Darbas: "+new Integer(player.getSkin()).toString());
gc.gridx = 0;
gc.gridy = 1;
gc.insets = new Insets(2,0,0,2);
gc.anchor = GridBagConstraints.LINE_START;
gridbagPanel.add(job, gc);
mainPanel.setBorder(new EmptyBorder(10,10,10,10));
mainPanel.add(gridbagPanel,BorderLayout.WEST);
add(mainPanel);
getContentPane().revalidate();
}
我想要左上角有数字的线条。
请注意,我知道JFrame(“this”类)和主面板都使用了边框布局。
另一个不相关的问题:我应该什么时候创建一个新的GridBagConstraint
对象?为什么我不能在私有实例变量中存储一个用于所有需要的使用?
我更喜欢盒子布局来实现这种静态定位。请看下面的例子:
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class main extends JFrame {
public static void main(String[] args) throws InterruptedException {
main m = new main();
m.setVisible(true);
}
public main() {
// setup stuff
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setBounds(100, 100, 500, 500);
// this is the panel I will add to the frame
JPanel innerPanel = new JPanel();
// give it a Y axis to stuff is added top to bottom
innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.Y_AXIS));
// this is a temp panel ill used to add the labels
JPanel tPanel = new JPanel();
// its an x axis to add stuff left to right
tPanel.setLayout(new BoxLayout(tPanel, BoxLayout.X_AXIS));
// create and add a label to the temp panel
JLabel label = new JLabel("Some text");
tPanel.add(label);
// use our stretchy glue to fill the space to the right of the label
tPanel.add(Box.createHorizontalGlue());
// add the temp panel to the inner panel
innerPanel.add(tPanel);
// create a spacer with 0 width and 10 height
innerPanel.add(Box.createRigidArea(new Dimension(0, 10)));
// reinitialize the temp panel for another label
tPanel = new JPanel();
tPanel.setLayout(new BoxLayout(tPanel, BoxLayout.X_AXIS));
label = new JLabel("Some other text");
// add the other label to the temp panel
tPanel.add(label);
// more stretchy glue
tPanel.add(Box.createHorizontalGlue());
// add the temp panel
innerPanel.add(tPanel);
// add verticle stretchy glue to fill the rest of the space below the
// labels
innerPanel.add(Box.createVerticalGlue());
// add to the frame
this.add(innerPanel);
}
}
GridBagLayout
的美妙之处在于,列/行的大小不是固定的(也就是说,并非每一行/列都必须具有相同的大小,就像在GridBagLayout
中一样)
您可以“鼓励”某些组件在其给定区域内比其他组件占据更多空间。
像weightx
和weighty
这样的东西描述了行/列应该占用多少可用空间。添加NORTHWEST
锚定
约束,您应该开始看到所需的效果。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
public class MainFrame extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new MainFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public MainFrame() {
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel gridbagPanel = new JPanel();
this.setLayout(new BorderLayout());
gridbagPanel.setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
JLabel nameLabel = new JLabel("Bebras");
nameLabel.setHorizontalAlignment(SwingConstants.CENTER);
nameLabel.setFont(new Font("Serif", Font.PLAIN, 24));
mainPanel.add(nameLabel, BorderLayout.NORTH);
JLabel money = new JLabel("Pinigai: " + new Integer(66484).toString());
gc.gridx = 0;
gc.gridy = 0;
gc.anchor = GridBagConstraints.NORTHWEST;
gc.insets = new Insets(2, 0, 0, 2);
gridbagPanel.add(money, gc);
JLabel job = new JLabel("Darbas: " + new Integer(126).toString());
gc.gridx = 0;
gc.gridy = 1;
gc.insets = new Insets(2, 0, 0, 2);
gc.anchor = GridBagConstraints.NORTHWEST;
gc.weightx = 1;
gc.weighty = 1;
gridbagPanel.add(job, gc);
mainPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
mainPanel.add(gridbagPanel, BorderLayout.WEST);
add(mainPanel);
getContentPane().revalidate();
}
}
通常,我会在组件中添加一个“填充”组件,用它将所有其他组件推到我想要的位置,但这将归结为您想要实现的目标。
看看如何使用GridBagLayout
了解更多详情
看看我的代码,有谁能告诉我,我如何在滚动窗格的右侧换行文本区域,以及如何将它们左对齐?
我已经创建了一个简单的程序,它有1个JLabel说你想继续和2个JButtons,一个说是,一个说不。
在Java中,如何使用printf()对同一行的输出进行一致的左对齐和右对齐?这是所有left aligned的代码: 这是所有东西都保持对齐的输出: 相反,如果第三个元素(0.20,1.00,9.00)是正确的,那么我想要的是什么。我在上面的输出中指出“l”是左对齐的,但我希望变量在“r”所在的位置是右对齐的。如何使右边的输出(0.20,1.00,9.00)右对齐,同时使左边的输出(苹果,派,奶
我有一个关于JavaFX中的HBox的问题。如果我向HBox添加了一个新组件,它就会自动添加到最后一个组件中。有没有可能得到这样的东西:
我有2个带有GridBagLayouts的JPanel来设置布局。唯一的问题是,所有部件都位于面板的中心。 我需要这些组件集群与它们添加到的JPanel的左上角对齐。 我知道您可以使用锚点变量对齐布局中的组件,但我希望整个网格锚定在面板的左上角。 示例工具对象: 主要方法: