3

我写了简单的java应用程序。一个窗口(JFrame ramka)包含 3 个元素:JButton、JComboBox 和容器(其中包含 JList)。我想重新排列这些元素的位置,这会导致问题。我想使用 GridBagLayout,但是当我这样做时,我的容器消失了(或者它是 1px 大 -.-),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在使用布局模型时显示容器有问题......

4

2 回答 2

1

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

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

  2. 将您的 GUI 组件与数据定义分开。这使得验证您的 GUI 组件变得更加容易。

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

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

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

这是我的代码版本。

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());
    }
}
于 2012-12-27T16:37:40.657 回答
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
    }
}
于 2012-12-27T19:53:22.870 回答