简体   繁体   English

自定义JPanel对象在添加到JFrame时会重叠

[英]Custom JPanel objects overlap when added to JFrame

I'm trying to create a custom timer that will record cumulative time passage separated by days. 我正在尝试创建一个自定义计时器,该计时器将记录以天为单位的累积时间流逝。 I have a custom JPanel that does all of the timer work for me. 我有一个自定义的JPanel,可以为我完成所有计时器工作。 I would like to have a GUI interace with this JPanel represented 7 times. 我想用这个JPanel进行7次GUI交互。 However, when I add more than one custom JPanel to either a JPanel or a JFrame, they don't show up. 但是,当我向一个JPanel或JFrame中添加多个自定义JPanel时,它们不会显示。 I've tried setting the layout and setting them to everything I can think of, but nothing works. 我尝试设置布局并将其设置为我能想到的所有内容,但没有任何效果。

This is the basic setup of the Panel: 这是面板的基本设置:

public class TimerPane extends JPanel{
    private static JButton button = new JButton("Start");
    private static JLabel label = new JLabel("Time elapsed:");
    private static JLabel tLabel = new JLabel("0:0:0");
    private static JLabel title = new JLabel("Timer");

    public TimerPane(){
        button.addActionListener(new ButtonListener());

        this.add(title);
        this.add(label);
        this.add(tLabel);
        this.add(button);
        this.setOpaque(false);

        this.setPreferredSize(new Dimension(100,100));
        this.setMaximumSize(new Dimension(100,100));
    }
}

This is my latest attempt at getting the JPanel to display multiple times (just twice here): 这是我最近一次尝试使JPanel显示多次(这里只是两次)的尝试:

public static void main(String[] args){
    JFrame frame = new JFrame("Timer");
    JPanel panel = new JPanel();
    frame.setPreferredSize(new Dimension(700,110));

    panel.setLayout(new BorderLayout());

    panel.add(new TimerPane(), BorderLayout.EAST);
    panel.add(new TimerPane(), BorderLayout.WEST);

    frame.getContentPane().add(panel);


    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

The GUI that executes after this is 700x110 of which only 100x100 on the far left is used for exactly one of my TimerPane panels. 此后执行的GUI为700x110,其中最左边的100x100仅用于我的TimerPane面板之一。 I have also tried GridLayout on the same code, but then only the TimerPane in the second "spot" shows up. 我也在相同的代码上尝试了GridLayout,但随后仅在第二个“位置”中显示了TimerPane。 Any suggestions? 有什么建议么?

First of all, please remove the static from the member variables: button , label , tLabel and title . 首先,请从成员变量中删除static变量: buttonlabeltLabeltitle Otherwise, having them static , it means they are shared by all the TimerPane instances. 否则,将它们设置为static ,意味着它们被所有TimerPane实例共享。 You will see 2 timer panels now. 您现在将看到2个计时器面板。

Next, you can change the BorderLayout to a FlowLayout for instance and add several instances of TimerPane . 接下来,您可以将BorderLayout更改为FlowLayout ,并添加几个TimerPane实例。

panel.setLayout(new FlowLayout(FlowLayout.LEFT));

panel.add(new TimerPane());
panel.add(new TimerPane());

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

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