Java 为什么我的所有组件都聚集在JFrame的中心
我正在使用网格包布局创建一个如下所示的布局: 但我得到的是: 为什么会这样?我已经指定了左对齐和占据所有水平空间,但我仍然以这个结束。这是我的密码:Java 为什么我的所有组件都聚集在JFrame的中心,java,swing,awt,layout-manager,gridbaglayout,Java,Swing,Awt,Layout Manager,Gridbaglayout,我正在使用网格包布局创建一个如下所示的布局: 但我得到的是: 为什么会这样?我已经指定了左对齐和占据所有水平空间,但我仍然以这个结束。这是我的密码: public DepotView() { setSize(FRAME_WIDTH,FRAME_HEIGHT); setLocationRelativeTo ( null ); getContentPane ().setLayout ( new GridBagLayout()); JPanel workerPa
public DepotView()
{
setSize(FRAME_WIDTH,FRAME_HEIGHT);
setLocationRelativeTo ( null );
getContentPane ().setLayout ( new GridBagLayout());
JPanel workerPanel = createTextAreaPanel("Processing: ",workerArea);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
c.gridheight = 1;
c.weightx = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.FIRST_LINE_START;
getContentPane().add ( workerPanel );
JPanel customerPanel = createTextAreaPanel("Customers: ",textArea);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 0.5;
c.insets = new Insets(5,5,5,5);
getContentPane().add ( customerPanel );
JPanel depotPanel = createTextAreaPanel("Depot: ",depotArea);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 0.5;
c.insets = new Insets(5,5,5,5);
getContentPane().add ( depotPanel );
//pack();
setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
在将面板添加到框架时,您似乎忘记了包含
GridBadConstriants
。只需更改getContentPane().add(panel)代码>到getContentPane()。添加(面板,c)代码>应该可以使用。在将面板添加到框架时,您似乎忘记了包含GridBadConstriants
。只需更改getContentPane().add(panel)代码>到getContentPane()。添加(面板,c)代码>,它应该可以工作。没有正确定义JPanel
的布局。您当前使用的是JPanel
的默认布局FlowLayout
,这在您的情况下是一个糟糕的选择
- 它不会调整组件的大小以匹配容器的大小(JPanel
)
根据JPanel
的大小,它会将文本放在标签旁边
最好的选择可能是使用BorderLayout
还要为JPanel
设置最小大小:
JPanel customerQueuePanel = new JPanel(new BorderLayout());
customerQueuePanel.setMinimumSize(new Dimension(250, 150));
...
customerQueuePanel.add ( new JLabel(label), BorderLayout.NORTH);
customerQueuePanel.add( scrollPane, BorderLayout.CENTER);
编辑:上面的代码是对createTextArea方法的编辑,您从问题中删除了该方法
还可以像以前一样,将约束添加为参数
getContentPane().add(panel, c);
JPanel
的布局未正确定义。您当前使用的是JPanel
的默认布局FlowLayout
,这在您的情况下是一个糟糕的选择
- 它不会调整组件的大小以匹配容器的大小(JPanel
)
根据JPanel
的大小,它会将文本放在标签旁边
最好的选择可能是使用BorderLayout
还要为JPanel
设置最小大小:
JPanel customerQueuePanel = new JPanel(new BorderLayout());
customerQueuePanel.setMinimumSize(new Dimension(250, 150));
...
customerQueuePanel.add ( new JLabel(label), BorderLayout.NORTH);
customerQueuePanel.add( scrollPane, BorderLayout.CENTER);
编辑:上面的代码是对createTextArea方法的编辑,您从问题中删除了该方法
还可以像以前一样,将约束添加为参数
getContentPane().add(panel, c);
问题是框架的大小小于内容窗格的首选总大小。从那里,你得到了一个swcrewed布局
还有几件事:
- 在JFrame上使用
pack()
而不是setSize()
,以获得合适的帧大小
- 避免使用
gridx/gridy
,它们会使约束变得复杂且难以维护
anchor/fill
几乎应始终与大于0的weightx
和/或weighty
组合使用
- 不要使用
JPanel
的默认FlowLayout
,而是使用LayoutManager,它将利用额外的可用空间(例如BorderLayout
)
- 不要使用
static
变量,这是邪恶的
- textarea变量始终为空
下面是一段代码,它似乎工作得很好:
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.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class DepotView extends JFrame {
private JTextArea textArea;
private JTextArea depotArea;
private JTextArea workerArea;
public DepotView() {
getContentPane().setLayout(new GridBagLayout());
JPanel workerPanel = createTextAreaPanel("Processing: ", workerArea = new JTextArea());
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.weightx = 1.0;
c.weighty = 1.0;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.FIRST_LINE_START;
getContentPane().add(workerPanel, c);
JPanel customerPanel = createTextAreaPanel("Customers: ", textArea = new JTextArea());
c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.FIRST_LINE_START;
getContentPane().add(customerPanel, c);
JPanel depotPanel = createTextAreaPanel("Depot: ", depotArea = new JTextArea());
c = new GridBagConstraints();
c.weightx = 1.0;
c.weighty = 1.0;
c.gridwidth = GridBagConstraints.REMAINDER;
c.insets = new Insets(5, 5, 5, 5);
c.fill = GridBagConstraints.BOTH;
getContentPane().add(depotPanel, c);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
}
private JPanel createTextAreaPanel(String label, JTextArea textArea) {
JPanel customerQueuePanel = new JPanel(new BorderLayout());
customerQueuePanel.add(new JLabel(label), BorderLayout.NORTH);
textArea.setRows(15);
textArea.setColumns(20);
textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
customerQueuePanel.add(scrollPane);
return customerQueuePanel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new DepotView().setVisible(true);
}
});
}
}
问题是框架的大小小于内容窗格的首选总大小。从那里,你得到了一个swcrewed布局
还有几件事:
- 在JFrame上使用
pack()
而不是setSize()
,以获得合适的帧大小
- 避免使用
gridx/gridy
,它们会使约束变得复杂且难以维护
anchor/fill
几乎应始终与大于0的weightx
和/或weighty
组合使用
- 不要使用
JPanel
的默认FlowLayout
,而是使用LayoutManager,它将利用额外的可用空间(例如BorderLayout
)
- 不要使用
static
变量,这是邪恶的
- textarea变量始终为空
下面是一段代码,它似乎工作得很好:
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.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class DepotView extends JFrame {
private JTextArea textArea;
private JTextArea depotArea;
private JTextArea workerArea;
public DepotView() {
getContentPane().setLayout(new GridBagLayout());
JPanel workerPanel = createTextAreaPanel("Processing: ", workerArea = new JTextArea());
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.weightx = 1.0;
c.weighty = 1.0;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.FIRST_LINE_START;
getContentPane().add(workerPanel, c);
JPanel customerPanel = createTextAreaPanel("Customers: ", textArea = new JTextArea());
c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.FIRST_LINE_START;
getContentPane().add(customerPanel, c);
JPanel depotPanel = createTextAreaPanel("Depot: ", depotArea = new JTextArea());
c = new GridBagConstraints();
c.weightx = 1.0;
c.weighty = 1.0;
c.gridwidth = GridBagConstraints.REMAINDER;
c.insets = new Insets(5, 5, 5, 5);
c.fill = GridBagConstraints.BOTH;
getContentPane().add(depotPanel, c);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
}
private JPanel createTextAreaPanel(String label, JTextArea textArea) {
JPanel customerQueuePanel = new JPanel(new BorderLayout());
customerQueuePanel.add(new JLabel(label), BorderLayout.NORTH);
textArea.setRows(15);
textArea.setColumns(20);
textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
customerQueuePanel.add(scrollPane);
return customerQueuePanel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new DepotView().setVisible(true);
}
});
}
}
抱歉,我在发布此问题时对代码进行了更改-我将GridBagConstraints传递给add方法。抱歉,我在发布此问题时对代码进行了更改-我将GridBagConstraints传递给add方法。