【发布时间】:2020-11-08 17:56:05
【问题描述】:
为了创建我的 GUI,我创建了许多 JPanel 的子类,每个子类都解决了特定的布局问题。
第一个是AltBoxLayout_JPanel,灵感来自我之前提出的一个问题的答案(链接:Setting the height of a JTextFrame):它只是一对JPanels 依偎在一起,内部的一个与BoxLayout 以首选方式分配组件,外部的 BorderLayout 将组件压缩到最小尺寸。
第二个是Fieldset,灵感来自同名的HTML标签:同样它是由两个嵌套的JPanels组成的,外层是TitleBorder,内层是在构造函数,其目的是在Fieldset 区域内设置布局。
我分别测试了这两个专门的组件,结果很好,对它们很满意。最近我需要同时使用它们:我想要一个 JButton 和一个 JTextArea 在不被人为扩展的情况下相互显示(即在 AltBoxLayout_JPanel 内),并被边框包围(即 AltBoxLayout_JPanel在Fieldset) 内。由于我的所有组件都已经过测试,我确信它会顺利进行。
没有。
不知何故,我的JButton 不再可见。我知道它在那里(我检查过)并且我假设它被 JTextArea 覆盖,但我没有更多信息。下面是我的代码。
任何拯救我的JButton的帮助将不胜感激!
public class StackOverflowQuestion extends JFrame {
private static final long serialVersionUID = -5644282466098799568L;
private JPanel contentPanel;
// Display area
private Fieldset fieldset;
private JButton button;
private JTextArea jTextArea;
/**
* Create the frame.
*/
public StackOverflowQuestion() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 300, 140);
contentPanel = new JPanel();
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPanel.setLayout(new BorderLayout(0, 0));
setContentPane(contentPanel);
// Testing new components
// JPanel fieldsetLayoutPanel = new JPanel();
// BoxLayout fieldsetLayout = new BoxLayout(fieldsetLayoutPanel, BoxLayout.PAGE_AXIS);
// fieldsetLayoutPanel.setLayout(fieldsetLayout);
JPanel fieldsetLayoutPanel_2 = new AltBoxLayout_JPanel(BoxLayout.PAGE_AXIS);
this.fieldset = new Fieldset("Fieldset title", fieldsetLayoutPanel_2, null);
this.button = new JButton("Button text");
this.jTextArea = new JTextArea("JTextArea here!");
this.jTextArea.setRows(5);
this.jTextArea.setColumns(20);
this.button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("Action executed: " + arg0.toString());
}});
fieldset.add(button);
fieldset.add(jTextArea);
contentPanel.add(fieldset);
System.out.println("Analysis of the components of the contentPanel:");
analyzeLayoutsOfComponentAndSubComponents(contentPanel, 0);
pack();
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
StackOverflowQuestion frame = new StackOverflowQuestion();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/** A method that studies the {@link JPanel} and its sub-{@link JPanel}s, iterating through all the component tree.
*
* @param c
* @param tab
*/
public static void analyzeLayoutsOfComponentAndSubComponents(JPanel c, int tab) {
System.out.println(repeatChar('\t', tab) + c.toString());
for(int i=0 ; i<c.getComponentCount() ; i++)
if( c.getComponent(i) instanceof JPanel )
analyzeLayoutsOfComponentAndSubComponents( (JPanel)c.getComponent(i), tab+1 );
}
public static String repeatChar(char c, int tab) {
StringBuffer support = new StringBuffer();
for(int i=0 ; i<tab ; i++) support.append(c);
return support.toString();
}
public class Fieldset extends JPanel {
private static final long serialVersionUID = -1464052286624448783L;
/** The {@link Border} of {@code this} {@link Fieldset}. */
private Border border;
/** The {@link JPanel} responsible for the layout management. */
public JPanel layoutPanel;
/** Creates a new {@link Fieldset}.
*
* @param colorBorder The {@link Color} of the border. If {@code null}, {@code Color.BLACK} is chosen.
*
*/
public Fieldset(String titleFieldset, JPanel panel, Color colorBorder) {
super();
this.border = new TitledBorder(new LineBorder(colorBorder == null ? Color.BLACK : colorBorder), titleFieldset);
this.setBorder(border);
this.layoutPanel = panel;
super.add(layoutPanel);
}
public Component add(Component component) {
this.layoutPanel.add(component);
return component;
}
}
public class AltBoxLayout_JPanel extends JPanel {
private static final long serialVersionUID = -8204033141468207723L;
private JPanel layoutPanel;
/** Creates a new {@link AltBoxLayout_JPanel}.
*
* @param axisOrientation A constant from the {@link BoxLayout} class representing the chosen insertion axis.
*/
public AltBoxLayout_JPanel(int axisOrientation) {
// ===== INNER PANEL =====
this.layoutPanel = new JPanel(); //This is the nested panel
layoutPanel.setLayout(new BoxLayout(layoutPanel, axisOrientation));
// ===== OUTER PANEL =====
this.setLayout(new BorderLayout());
this.add(layoutPanel, BorderLayout.PAGE_START);
System.out.println("Analysis of the components of the AltBoxLayout_JPanel:");
analyzeLayoutsOfComponentAndSubComponents(this, 0);
}
public void add(JComponent component) {
layoutPanel.add(component);
}
}
}
【问题讨论】:
-
StackOverflowQuestion您将发布到 Stack Overflow 的 next 代码示例称为什么?StackOverflowQuestion2?请给出与问题相关的示例代码有意义的名称。例如(看标题)PanelEatsButtonExample之类的东西会很好。另一个优点是其他人不太可能使用该名称,因此尝试代码的人可以将两个示例放在他们 IDE 中的同一个包中。 一般提示: 1) 以最小尺寸提供 ASCII 艺术或预期 GUI 布局的简单绘图,如果可调整大小,.. -
.. 具有更多的宽度和高度 - 显示应该如何使用额外的空间。 2) 曾经只需要源代码中的一个空白行。
{之后或}之前的空行通常也是多余的。 3)包括进口。为整个包添加导入只会添加 4 行代码。 4)但是删除IDE插入的多余的cmets。代码本身应该在有意义的属性和方法名称中包含足够的信息,并结合...代码本身。对于特殊说明,请继续使用如上所示的单行 cmets。 .. -
5) 对于布局/组件缺失问题,去掉所有不处理的方法。例如。
repeatChar(..)方法似乎毫无必要。 6) 我认为没有充分的理由扩展JFrame或JPanel。这样做的逻辑是什么?
标签: java swing user-interface jbutton jtextarea