【发布时间】:2015-11-02 21:07:31
【问题描述】:
从我的代码中,我希望我的 JTextArea 填充如下所示的左上边框:
但是你可以看到它在中间占据了一小部分。 我在包含组件的面板上使用 GridBagConstraints。
有一个主类调用了一个叫做框架的类。此类创建 JFrame 并设置大小以及其他内容。然后从 MainFrame.java 调用它,它扩展了创建 3 个面板并设置它们的布局的 jframe。如下所示
import javax.swing.*;
import java.awt.*;
public class MainFrame extends JFrame
{
private Panel1 storyPanel;
private Panel2 statsPanel;
private Panel3 commandsPanel;
public MainFrame(String title)
{
super(title);
// Setting Layout
GridBagConstraints gbc = new GridBagConstraints();
storyPanel = new Panel1();
storyPanel.setLayout(new GridBagLayout());
statsPanel = new Panel2();
commandsPanel = new Panel3();
Container p = getContentPane();
p.add(storyPanel, BorderLayout.WEST);
p.add(statsPanel, BorderLayout.EAST);
p.add(commandsPanel, BorderLayout.SOUTH);
}
}
有问题的面板是 Panel1 或 storyPanel。我已经设置了布局,代码调用了 Panel1.java,如下所示:
import javax.swing.*;
import java.awt.*;
public class Panel1 extends JPanel
{
public Panel1()
{
GridBagConstraints gbc = new GridBagConstraints();
//Set size of Panel1
int xsizeP1 = (Frame.xsize() / 2);
int ysizeP1 = (Frame.ysize() / 3 * 2);
setPreferredSize(new Dimension(xsizeP1, ysizeP1));
setBorder(BorderFactory.createLineBorder(Color.black));
//Adding JTextArea and adding settings to it
JTextArea storyLine = new JTextArea(" test ");
storyLine.setLineWrap(true);
storyLine.setWrapStyleWord(true);
storyLine.setEditable(false);
//Adding JScrollPane to the JTextArea and making it have a vertical scrollbar
JScrollPane scroll = new JScrollPane(storyLine);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//GridBagConstraints setup for components
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.fill = GridBagConstraints.VERTICAL;
add(scroll, gbc);
}
}
我不明白为什么我的 gbc.fill 没有让 JTextArea 填充屏幕截图的左上边框。
提前感谢您的任何回复
-汤姆T
将布局更改为边框布局
import javax.swing.*;
import java.awt.*;
public class Panel1 extends JPanel
{
public Panel1()
{
//GridBagConstraints gbc = new GridBagConstraints();
//gbc.weightx = 0.1;
//gbc.weighty = 0.1;
BorderLayout b = new BorderLayout();
//Set size of Panel1
int xsizeP1 = (Frame.xsize() / 2);
int ysizeP1 = (Frame.ysize() / 3 * 2);
setPreferredSize(new Dimension(xsizeP1, ysizeP1));
setBorder(BorderFactory.createLineBorder(Color.black));
//Adding JTextArea and adding settings to it
JTextArea storyLine = new JTextArea(" test ");
storyLine.setLineWrap(true);
storyLine.setWrapStyleWord(true);
storyLine.setEditable(false);
//Adding JScrollPane to the JTextArea and making it have a vertical scrollbar
JScrollPane scroll = new JScrollPane(storyLine);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//gbc.gridx = 0;
//gbc.gridy = 0;
//gbc.weightx = 1;
//gbc.weighty = 1;
//gbc.fill = GridBagConstraints.BOTH;
add(scroll, b.CENTER);
}
}
【问题讨论】:
-
不要担心设置 xsize 和 ysize,thoes 的代码在 Frame.java 中,问题不需要。如果需要任何其他信息,请询问。
-
不要再设置storyPanel的布局。使用 GridBagConstaints.BOTH 作为填充约束,并使用 weightx 和 weighty 指定组件应占用的剩余空间量。根据您的代码,改用 BorderLayout 会更简单
-
我尝试了边框布局并将其设置为中心,希望它可以调整大小,但也失败了,不过我会尝试您的建议,谢谢
-
我删除了 Mainframe 面板上的 Gridbagconstraints 并将填充设置为 both 和 weighty,但除了将组件移动到左侧面板的顶部之外,它没有改变任何东西。
标签: java swing layout-manager jtextarea gridbaglayout