本文介绍了如何从 GridBagLayout 列出 JComponents?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想弄清楚如何在按下按钮后打印组件名称和/或它们的值.我目前正在使用具有 2 列和 6 行的 GridBagLayout
,但我不知道如何遍历布局或我的 actionPerformed()
方法内部的任何类似内容.我认为这可能与 getContentPane()
有关,但我不完全确定.
I am trying to figure out how to print the component names and/or their values after a button is pressed. I currently am using a GridBagLayout
with 2 columns and 6 rows but I don't know how to traverse the layout or anything like that inside of my actionPerformed()
method. I think it may have to do with getContentPane()
but I'm not entirely sure.
推荐答案
在下面的代码中,我打印:
In the following code, I print:
System.out.println(comp.getClass().getSimpleName() +
" Bounds: " + comp.getBounds());
但要更全面地了解组件,请将其更改为:
But for a much more comprehensive view of the component, change that to:
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]
这篇关于如何从 GridBagLayout 列出 JComponents?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!