如何从GridBagLayout列出JComponents?

时间:2014-02-01 03:43:18

标签: java swing jcomponent

我试图弄清楚在按下按钮后如何打印组件名称和/或它们的值。我目前正在使用一个包含2列和6行的GridBagLayout,但我不知道如何遍历我的actionPerformed()方法中的布局或类似内容。我认为这可能与getContentPane()有关,但我不完全确定。

2 个答案:

答案 0 :(得分:4)

enter image description here

在下面的代码中,我打印:

            System.out.println(comp.getClass().getSimpleName() + 
                    " Bounds: " + comp.getBounds());

但是对于更全面的组件视图,请将其更改为:

            System.out.println(comp);

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

class ListComponents {

    public static void listComponents(Container c) {
        Component[] components = c.getComponents();
        for (Component comp : components) {
            System.out.println(comp.getClass().getSimpleName() + 
                    " Bounds: " + comp.getBounds());
            if (comp instanceof Container) {
                listComponents((Container)comp);
            }
        }
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                final JPanel gui = new JPanel(new BorderLayout(3,3));

                JTree tree = new JTree();
                tree.setVisibleRowCount(8);
                gui.add(new JScrollPane(tree), BorderLayout.LINE_START);

                JToolBar tb = new JToolBar();
                gui.add(tb, BorderLayout.PAGE_START);
                Action list = new AbstractAction("List") {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        listComponents(gui);
                    }
                };
                tb.add(list);
                tb.add(new JToggleButton("Toggle"));
                tb.add(new JCheckBox("Check"));

                gui.add(new JScrollPane(new JTextArea("Default Text",3,20)));

                JOptionPane.showMessageDialog(null, gui);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}

工具栏在GUI上时的输出

JScrollPane Bounds: java.awt.Rectangle[x=0,y=35,width=81,height=147]
JViewport Bounds: java.awt.Rectangle[x=1,y=1,width=78,height=144]
JTree Bounds: java.awt.Rectangle[x=0,y=0,width=78,height=144]
CellRendererPane Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
ScrollBar Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
ScrollBar Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
JToolBar Bounds: java.awt.Rectangle[x=0,y=0,width=307,height=32]
 Bounds: java.awt.Rectangle[x=16,y=1,width=33,height=28]
JToggleButton Bounds: java.awt.Rectangle[x=49,y=1,width=50,height=28]
JCheckBox Bounds: java.awt.Rectangle[x=99,y=1,width=65,height=28]
JScrollPane Bounds: java.awt.Rectangle[x=84,y=35,width=223,height=147]
JViewport Bounds: java.awt.Rectangle[x=1,y=1,width=220,height=144]
JTextArea Bounds: java.awt.Rectangle[x=0,y=0,width=220,height=144]
ScrollBar Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
ScrollBar Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]

从GUI

拖出工具栏时的输出
JScrollPane Bounds: java.awt.Rectangle[x=0,y=0,width=81,height=182]
JViewport Bounds: java.awt.Rectangle[x=1,y=1,width=78,height=179]
JTree Bounds: java.awt.Rectangle[x=0,y=0,width=78,height=179]
CellRendererPane Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
ScrollBar Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
ScrollBar Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
JScrollPane Bounds: java.awt.Rectangle[x=84,y=0,width=223,height=182]
JViewport Bounds: java.awt.Rectangle[x=1,y=1,width=220,height=179]
JTextArea Bounds: java.awt.Rectangle[x=0,y=0,width=220,height=179]
ScrollBar Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
ScrollBar Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]
MetalScrollButton Bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]

答案 1 :(得分:0)

假设所有内容都包含在java.awt.Component中,您可以在容器上调用getComponents()。这将为您提供一组Component个对象,您可以迭代这些对象以打印出您喜欢的任何对象。