GridBagLayout,消失了JList

时间:2012-12-27 12:25:01

标签: java layout gridbaglayout

我写了简单的java应用程序。一个窗口(JFrame ramka)包含3个元素:JButton,JComboBox和容器(包含JList)。 我想重新安排这些元素的位置,这会导致问题。我想使用GridBagLayout,但是当我这样做时,我的容器消失了(或者它是1px large -.-),setSize也适用于JComboBox。当我不使用任何布局时,会显示带有JList的容器,但是调整JComboBox的大小不起作用,并且所有内容都重叠...通常它看起来像......它看起来并不是全部。

这里有我的问题:如何以及使用什么布局来重新组织我的组件和JFrame的位置? 我使用NetBeans 7.2

package layouttest;

import java.awt.*;
import javax.swing.*;
import javax.swing.JList;
import java.util.ArrayList;
import java.awt.*;
import java.awt.GridBagLayout;
import java.awt.event.*;
import javax.swing.*;

public class Layouttest {
    public static void main(String[] args) {
        JFrame ramka = new JFrame();
        //ramka.setLayout(new GridLayout(4, 3));
        //ramka.setLayout(new GridBagLayout());
        String [] test = {"cos", "cos2", "cos3", "cos4", "cos5", "cos6", "cos7", "cos8", "cos9","cos10", "cos11", "cos12"};
        ArrayList<String> myList = new ArrayList<String>();
        myList.add("cos");
        myList.add("cos2");
        myList.add("cos3");
        myList.add("cos4");
        myList.add("cos5");
        myList.add("cos6");
        myList.add("cos7");
        myList.add("cos8");
        myList.add("cos9");
        myList.add("cos10");

        GridBagConstraints ogranicznik = new GridBagConstraints();
        ogranicznik.insets = new Insets(5, 10, 5, 5);
        ogranicznik.gridx = 0;
        ogranicznik.gridy = 0; 

        Container c = new Container();
        ramka.setSize(500, 500);
        c.setSize(100,100);

        JList lista = new JList(myList.toArray());
        JScrollPane sp = new JScrollPane(lista);
        JTextField txt = new JTextField("cos");
        c.add(sp);

        sp.setSize(100,100);
        lista.setSize(100, 100);
        ramka.add(c);
        ramka.add(txt);

        String[] dropBoxList = {"uno", "due", "tre"};
        final JComboBox listaRozwijalna = new JComboBox(dropBoxList);

        listaRozwijalna.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXX"); 
        listaRozwijalna.revalidate();
        listaRozwijalna.repaint();
        ramka.add(listaRozwijalna);

        c.revalidate();
        c.repaint();
        ramka.revalidate();
        ramka.setVisible(true);
        ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // TODO code application logic here
    }
}

@EDIT 我发现java在使用布局模型时显示容器有问题......

2 个答案:

答案 0 :(得分:1)

我已重新安排您的代码并添加到GridBagLayout中。我不得不猜测你想如何安排你的组件。

  1. 始终确保您的Swing组件位于Event Dispatch线程上。这就是主要方法的作用。

  2. 将GUI组件与数据定义分开。这样可以更轻松地验证GUI组件。

  3. 按照放置在主JPanel上的顺序定义GUI组件。

  4. 始终定义主要JPanel。这使您可以更灵活地使用布局管理器来定义GUI。

  5. 始终使用一个或多个布局管理器。

  6. 这是我的代码版本。

    import java.awt.Component;
    import java.awt.Container;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class Layouttest implements Runnable {
    
        protected Insets    ogranicznik = new Insets(5, 10, 5, 5);
    
        @Override
        public void run() {
            JFrame ramka = new JFrame();
    
            JPanel panel = new JPanel();
            panel.setLayout(new GridBagLayout());
    
            JTextField txt = new JTextField("cos");
    
            addComponent(panel, txt, 0, 0, 1, 1, ogranicznik,
                    GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
    
            JComboBox listaRozwijalna = new JComboBox(setDropBoxValues());
    
            addComponent(panel, listaRozwijalna, 1, 0, 1, 1, ogranicznik,
                    GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
    
            JList lista = new JList(setListValues());
            JScrollPane sp = new JScrollPane(lista);
    
            addComponent(panel, sp, 0, 1, 1, 1, ogranicznik,
                    GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
    
            ramka.add(panel);
            ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            // ramka.setSize(500, 500);
            ramka.pack();
            ramka.setVisible(true);
        }
    
        protected void addComponent(Container container, Component component,
                int gridx, int gridy, int gridwidth, int gridheight, Insets insets,
                int anchor, int fill) {
            GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
                    gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0);
            container.add(component, gbc);
        }
    
        protected String[] setListValues() {
            String[] test = { "cos", "cos2", "cos3", "cos4", "cos5", "cos6",
                    "cos7", "cos8", "cos9", "cos10", "cos11", "cos12" };
            return test;
        }
    
        protected String[] setDropBoxValues() {
            String[] dropBoxList = { "uno", "due", "tre" };
            return dropBoxList;
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Layouttest());
        }
    }
    

答案 1 :(得分:0)

我以其他方式做到了......我只是省略了容器......

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package layouttest;

import java.awt.*;
import javax.swing.*;
import javax.swing.JList;
import java.util.ArrayList;
import java.awt.*;
import java.awt.GridBagLayout;
import java.awt.event.*;
import javax.swing.*;


/**
 *
 * @author kula
 */
public class Layouttest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        JFrame ramka = new JFrame();
       //ramka.setLayout(new GridLayout(4, 3));
        ramka.setLayout(new GridBagLayout());
        String [] test = {"cos", "cos2", "cos3", "cos4", "cos5", "cos6", "cos7", "cos8", "cos9","cos10", "cos11", "cos12"};
        ArrayList<String> myList = new ArrayList<String>();
        myList.add("cos");
        myList.add("cos2");
        myList.add("cos3");
        myList.add("cos4");
        myList.add("cos5");
        myList.add("cos6");
        myList.add("cos7");
        myList.add("cos8");
        myList.add("cos9");
        myList.add("cos10");

        GridBagConstraints ogranicznik = new GridBagConstraints();
//ogranicznik.insets = new Insets(5, 10, 5, 5);
ogranicznik.gridx = 0;
ogranicznik.gridy = 2; 

        Container c = new Container();
        ramka.setSize(500, 500);
   // c.setSize(100,100);

    final JScrollPane sp = new JScrollPane();
    JList lista = new JList(myList.toArray());

   sp.getViewport().add(lista);//!!!!!!!!!!!!!!!!!!

    //sp.removeAll();

    //c.add(sp);
   //sp.setViewportView(lista);
  // sp.setSize(100,100);
   //lista.setSize(100, 100);
    ramka.add(sp);




    String[] dropBoxList = {"uno", "due", "tre"};
    final JComboBox listaRozwijalna = new JComboBox(dropBoxList);


   /* listaRozwijalna.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXX"); 
    listaRozwijalna.revalidate();
    listaRozwijalna.repaint();*/
   // ramka.add(listaRozwijalna);


    c.revalidate();
    c.repaint();
    ramka.revalidate();


    ramka.setVisible(true);
    ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // TODO code application logic here
    }
}