标签: gridbaglayout

Java GridBagLayout自动构建

我使用GridBagLayout和GridBagConstraints设计了一个GUI.它包含可变数量的行,每行包含一些可能的列布局.为了测试代码,我构建了具有不同颜色的面板GUI,用于说明每行和每列中所有单元格的位置和大小调整行为.这个测试GUI工作正常,但现在我需要自动化它的构造.具体来说,我需要将行作为三种不同类型之一.如果您运行下面的代码并查看生成的GUI,您可以看到row1是一种类型,行2,6和7是另一种类型,而行3,4和5是第三种类型.我需要将这三种类型的行中的每一行封装在自己的类中.更重要的是,我需要我的代码能够创建第三种类型的可变数量的行(在示例中由行3,4和5说明).(这是用于数据分析软件.面板将加载数据视图和工具来操纵数据视图.)

当我尝试将行封装到自己的类中时,GUI看起来应该看起来不应该看起来.下面的测试代码以它应该看起来的方式生成GUI布局.任何人都可以告诉我如何更改此代码,以便它具有上面第一段中描述的功能?

您可以将我的测试代码粘贴到您的IDE中,以使其立即运行.测试代码分为两个单独的文件,如下所示:

GridBagLayoutDemo.java的代码是:

import java.awt.*;
import javax.swing.JFrame;

public class GridBagLayoutDemo {
    final static boolean RIGHT_TO_LEFT = false;

    public static void addComponentsToPane(Container pane) {
        if (RIGHT_TO_LEFT) {pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);}
        pane.setLayout(new GridBagLayout());

// top row (fill, gridx, gridy, gridwidth 1, weightx 0, weighty 0, ipady 0)
        TestPanel panelr1c1 = new TestPanel(Color.black);
        GridBagConstraints constraint_r1c1 = getGridBagConstraints(GridBagConstraints.NONE,0,0,1,0,0,0);
        pane.add(panelr1c1,constraint_r1c1);

        TestPanel panelr1c2 = new TestPanel(Color.blue);
        GridBagConstraints constraint_r1c2 = getGridBagConstraints(GridBagConstraints.HORIZONTAL,1,0,1,0.8,0,0);
        pane.add(panelr1c2,constraint_r1c2);

        TestPanel panelr1c2a = new TestPanel(Color.green);
        GridBagConstraints constraint_r1c2a = getGridBagConstraints(GridBagConstraints.HORIZONTAL,2,0,1,0.8,0,0);
        pane.add(panelr1c2a,constraint_r1c2a);

        TestPanel panelr1c3 = new TestPanel(Color.red);
        GridBagConstraints constraint_r1c3 …
Run Code Online (Sandbox Code Playgroud)

java layout user-interface swing gridbaglayout

4
推荐指数
1
解决办法
1606
查看次数

jpanel没有很好地显示jframe设置为gridbaglayout

下面的程序是使用gridbaglayout将jpanel定位在jframe的左上角,而是在jframe的中心显示一个非常小的框.当我将jframe的布局设置为null时,jpanel显示正常.有人可以告诉我为什么jpanel被压缩到framebaglayout框架的中心?我真的需要使用gridbag.请帮忙

import java.awt.*;
import javax.swing.*; //swing package

public class Major {

    //defining the constructor
    public Major() {
        JFrame maFrame = new JFrame("The main screen"); //creating main Jframe
        JPanel headPanel = new JPanel(); //creating the header panel
        maFrame.setSize(900, 700); //setting size
        maFrame.setBackground(Color.LIGHT_GRAY); //setting color of frame
        Container container = maFrame.getContentPane();
        container.setLayout(new GridBagLayout()); //setting layout of main frame
        GridBagConstraints cns = new GridBagConstraints(); //creating constraint
        cns.gridx = 0;
        cns.gridy = 0;
        maFrame.setLocationRelativeTo(null); //centering frame
        headPanel.setBackground(Color.WHITE);
        headPanel.setSize(200, 150);
        container.add(headPanel, cns);
        maFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //setting the …
Run Code Online (Sandbox Code Playgroud)

java swing jpanel layout-manager gridbaglayout

4
推荐指数
1
解决办法
6143
查看次数

当文本发生变化时,如何阻止JLabel更改其大小?

我在代码中生成一些JComponents并使用GridBag布局来排列它们.我的布局由12行和3列组成,每行包含一个JSlider,一个JCheckBox和一个JLabel.这是我用来生成UI的代码:

final int NUM_MOTORS = 12;

// This is the panel I'm adding the components to.
pnlMotorSliders.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

for (int i = 0; i < NUM_MOTORS; ++i) {
    c.gridy = i;

    // Create the slider
    JSlider slider = new JSlider(SwingConstants.HORIZONTAL, 10, 4085, 10);
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.weightx = 0.9;
    pnlMotorSliders.add(slider, c);

    // Create the checkbox
    JCheckBox checkBox = new JCheckBox();
    checkBox.setOpaque(true);
    checkBox.setBackground(Color.blue);
    c.fill = GridBagConstraints.NONE;
    c.gridx = 1;
    c.weightx = 0.1;
    pnlMotorSliders.add(checkBox, …
Run Code Online (Sandbox Code Playgroud)

java swing resize jlabel gridbaglayout

4
推荐指数
1
解决办法
2万
查看次数

使用GridBagLayout右对齐

我正在使用GridBagLayout创建一个简单的GUI,几乎一切顺利,但有这个细节:有两列,左边一个用于标签,另一个用于其他小部件,如JTextArea,我希望第一列对齐到右边.

所以我尝试将锚定属性设置为GridBagConstraints.NORTHEAST或.EAST,并且它们都不起作用.

我的代码如下:

private void createDataPanel() {
        data = new JPanel();
        data.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        data.setBorder(new TitledBorder("NOTAM proposal parameters"));

        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(5, 10, 5, 10);
        c.fill = GridBagConstraints.HORIZONTAL;
        data.add(new JLabel("Start time (DD-MM-YYYY HH:MM:SS):"), c);

        c.gridx++;
        long today = System.currentTimeMillis();
        startTimePicker = new JXDatePicker(new Date(today));
        startTimePicker.setFormats(dateFormatter);
        data.add(startTimePicker);

        c.gridx = 0;
        c.gridy++;
        c.anchor = GridBagConstraints.EAST;
        data.add(new JLabel("End time (DD-MM-YYYY HH:MM:SS):"), c);

        c.gridx++;
        c.anchor = GridBagConstraints.CENTER;
        long tomorrow = today + 86400000;
        endTimePicker …
Run Code Online (Sandbox Code Playgroud)

java swing alignment gridbaglayout

4
推荐指数
2
解决办法
7499
查看次数

Java GridBagLayout不会水平填充框架

我正在为聊天程序编写GUI.我似乎无法scroller水平和垂直messageInput地填充框架并且水平地填充框架.这是它的样子:

在此输入图像描述

import javax.swing.*;
import java.awt.*;

public class GUI extends JFrame{

private JPanel panel;
private JEditorPane content;
private JTextField messageInput;
private JScrollPane scroller;
private JMenu options;
private JMenuBar mb;
private JMenuItem item;

public GUI(){
    /** This is the frame**/
    this.setPreferredSize(new Dimension(380,600));
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;

    /** This is where the context shows up **/
    content = new JEditorPane();
    content.setEditable(false);

    /** Scroller that shows up in the …
Run Code Online (Sandbox Code Playgroud)

java swing layout-manager gridbaglayout

4
推荐指数
1
解决办法
3161
查看次数

如何在JFrame中围绕特定JPanel设置边框?

我解释不好,但我会尽我所能.

我基本已经尝试添加在我的边框JLabel,JTextFieldJButton我的内的组件JPanel,但边境根据规模不断扩大.

这是我的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LoginPanel
{
    private JPanel loginPanel = new JPanel();
    private JFrame loginFrame = new JFrame();

    public LoginPanel()
    {
        loginPanel.setLayout(new GridBagLayout());

        GridBagConstraints gridBagConstraints = new GridBagConstraints();

        JTextField textLogin = new JTextField(10);
        JPasswordField password = new JPasswordField(10);

        JButton login = new JButton("Login");
        JButton register = new JButton("Register");

        gridBagConstraints.insets = new Insets(0,0,0,0);

        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridx = 0;

        loginPanel.setBorder(BorderFactory.createTitledBorder("Login"));

        loginPanel.add(new JLabel("E-Mail"), gridBagConstraints);
        gridBagConstraints.gridy++;
        loginPanel.add(textLogin, gridBagConstraints); …
Run Code Online (Sandbox Code Playgroud)

java swing layout-manager gridbaglayout border-layout

4
推荐指数
1
解决办法
1663
查看次数

如何在不改变其他元素位置的情况下使组件不可见

有两个按钮:"下一步"和"后退"以传递列表.当到达结束时,按钮"下一步"消失,但按钮"后退"不应该跳到她的位置.我使用了setVisible (false),但按钮'Back'跳转到"Next"的位置.位置管理器是GridBagLayout.

java swing jpanel jbutton gridbaglayout

3
推荐指数
1
解决办法
4132
查看次数

如何在 GridBagLayout 中左对齐组件?

在此处输入图片说明

上图中的每一行,都是使用 gridbaglayout 放置组件的自定义组件。我试图避免按钮“Abc”之前的空间,我希望它完全推到左侧。如何使用 gridbaglayout 约束实现这一点?

编辑添加源代码。

        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = GridBagConstraints.NONE;
        gridBagConstraints.insets =  new Insets(11,0,0,0);
        add(abcButton,gridBagConstraints);
            gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = GridBagConstraints.NONE;
        gridBagConstraints.insets = new Insets(20, 10, 0, 0);
        add(comp1,gridBagConstraints);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = GridBagConstraints.NONE;
        gridBagConstraints.insets = new Insets(20, 35, 0, 0);
        add(comp2, gridBagConstraints);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipadx = …
Run Code Online (Sandbox Code Playgroud)

java swing gridbaglayout

3
推荐指数
1
解决办法
4720
查看次数

多个面板彼此之下

是否有可能我有一个登录的顶部面板.然后在下面的中间和底部面板.我正在使用gridbaglayout,我已经分配了2个面板.一个面板具有与其链接的组件以用于登录详细信息,另一个面板具有链接到其的其他组件.但是,计划不会发生.香港专业教育学院尝试使用Box/Borderlayout来做到这一点,但后来我无法创建我想要的间距.

这是我想要的草图.

http://s16.postimage.org/dtefn48qd/16_11_2012_11_24_40.png

public class GUI extends JFrame{

    private JPanel myTopPL, myTopPL2;
    private JTextField myUsernameTF;
    private JTextField myPasswordTF;
    private JTextField myMessageTF;
    private JLabel myUsernameLBL;
    private JLabel myPasswordLBL;
    private JLabel myItemsLBL;
    private JTextArea myMainTA;
    private JButton myLoginBN;
    private JButton myConnectBN;
    private JButton mySendBN;
    private JComboBox myItemsCB;

    public GUI () {

        super("GUI ");

        setLayout(new GridBagLayout());
        myTopPL = new JPanel();
        myTopPL2 = new JPanel();
        myTopPL.setLayout(new GridBagLayout());
        myTopPL2.setLayout(new GridBagLayout());
        GridBagConstraints myTopPLGBC = new GridBagConstraints();
        myTopPLGBC.weightx = 1;
        myTopPLGBC.weighty = 1;
        GridBagConstraints myTopPLGBC2 = new GridBagConstraints(); …
Run Code Online (Sandbox Code Playgroud)

java user-interface swing layout-manager gridbaglayout

3
推荐指数
1
解决办法
3725
查看次数

使用GridBagLayout在JTextArea中添加JScrollPane

我在使用GridBagLayout在JTextArea中添加JScrollPane时遇到问题.基本上,当不需要滚动条时程序运行正常,但布局搞砸了,内容被切断了.相关代码如下

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class testGUI extends JFrame
{
    public static String name;
    static JTextField textfield = new JTextField(30);
    static JTextArea  textarea = new JTextArea(30,30);

    public static void main( String[] args)
    {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Checkem");
        frame.setLocation(500,400);
        frame.setSize(800,800);

        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        JScrollPane scrolltxt = new JScrollPane(textarea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrolltxt.setWheelScrollingEnabled(true); 
        scrolltxt.getVerticalScrollBar().isVisible();
        panel.add(scrolltxt, c); 

        JLabel label = new JLabel("Enter the Name of the file:");
        c.gridx …
Run Code Online (Sandbox Code Playgroud)

java swing jscrollpane jtextarea gridbaglayout

3
推荐指数
1
解决办法
3922
查看次数