简体   繁体   English

JButton 没有出现在应用程序的西侧

[英]JButtons are not appearing at the West side of the application

When I tried to run my program, it is always an error.当我试图运行我的程序时,它总是一个错误。 The 5 JButtons are not appearing at the west (left) of the MainFrame. 5 个 JButton 没有出现在 MainFrame 的西部(左侧)。 I used the BoxLayout for the JButtons so that it can be displayed from top to bottom and called the FirstPanel class to the MainFrame so that I can position it on the west side.我为 JButtons 使用了 BoxLayout,以便它可以从上到下显示,并将 FirstPanel class 调用到 MainFrame,以便我可以在西侧进行 position。 It is supposed to be like this, but my application is not running.它应该是这样的,但我的应用程序没有运行。 Please help me with how can I achieve this请帮助我如何实现这一目标在此处输入图像描述

MainFrame.java主机.java

public class MainFrame extends JFrame {
    TitlePanel title;
    FirstPanel first;
    
 
    
    public MainFrame() {
        
        title = new TitlePanel();
        add(title, BorderLayout.NORTH);
        
        first = new FirstPanel();
        add(first, BorderLayout.WEST);
 
        
    
        setSize(5000,5000);
    setVisible(true);
    this.pack();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
    }
    
    
    
    public static void main(String[] args) {
        new MainFrame();
    }
}

FirstPanel.java FirstPanel.java

   public class FirstPanel extends JPanel {
   JButton b1;
   JButton b2;
   JButton b3;
   JButton b4;
   JButton b5;
   FirstPanel fp;
    
    public FirstPanel() {

       fp = new FirstPanel();
       BoxLayout layout = new BoxLayout(fp, BoxLayout.Y_AXIS);
       fp.setLayout(layout);
       
       b1 = new JButton();
       b2 = new JButton();
       b3 = new JButton();
       b4 = new JButton();
       b5 = new JButton();
       
       fp.add(b1);
       fp.add(b2);
       fp.add(b3);
       fp.add(b4);
       fp.add(b5);
    }
    
    
}

This is the ERROR that I got这是我得到的错误

Exception in thread "main" java.lang.StackOverflowError
    at java.awt.Component.setFont(Component.java:1907)
    at java.awt.Container.setFont(Container.java:1753)
    at javax.swing.JComponent.setFont(JComponent.java:2748)
    at javax.swing.LookAndFeel.installColorsAndFont(LookAndFeel.java:208)
    at javax.swing.plaf.basic.BasicPanelUI.installDefaults(BasicPanelUI.java:66)
    at javax.swing.plaf.basic.BasicPanelUI.installUI(BasicPanelUI.java:56)
    at javax.swing.JComponent.setUI(JComponent.java:660)
    at javax.swing.JPanel.setUI(JPanel.java:153)
    at javax.swing.JPanel.updateUI(JPanel.java:126)
    at javax.swing.JPanel.<init>(JPanel.java:86)
    at javax.swing.JPanel.<init>(JPanel.java:109)
    at javax.swing.JPanel.<init>(JPanel.java:117)
    at FirstPanel.<init>(FirstPanel.java:26)
    at FirstPanel.<init>(FirstPanel.java:28)

It would be better to first call pack and then make the JFrame visible.最好先调用pack ,然后使 JFrame 可见。

pack();
setVisible(true);

Also in the main use:主要用途还有:

SwingUtilities.invokeLater(() -> new MainFrame());

or what I like:或者我喜欢的:

SwingUtilities.invokeLater(() -> new MainFrame().setVisible(true));

This ensures the correct thread.这确保了正确的线程。

The error is having a FirstPanel fp inside the FirstPanel.错误是在 FirstPanel 中有一个 FirstPanel fp。 This is an endlessly nested object.这是一个无限嵌套的 object。 Hence never completely created.因此从未完全创造。

   BoxLayout layout = new BoxLayout(this, BoxLayout.Y_AXIS);
   setLayout(layout);
   ...
   add(b1);

One also often sees making just an anonymous JPanel to a variable, and using that.人们还经常看到只为变量创建一个匿名 JPanel 并使用它。 In the case of a no-name like "FirstPanel" that would make sense.在像“FirstPanel”这样的无名的情况下,这是有道理的。 However "ButtonPanel" or such is okay.但是“ButtonPanel”之类的没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM