2

日単位で累積時間経過を記録するカスタムタイマーを作成しようとしています。私はすべてのタイマー作業を行うカスタムJPanelを持っています。このJPanelとのGUIインターレースを7回表現したいと思います。ただし、JPanelまたはJFrameのいずれかに複数のカスタムJPanelを追加すると、それらは表示されません。レイアウトを設定して、考えられるすべてのものに設定しようとしましたが、何も機能しません。

パネルの基本的な設定は次のとおりです。

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));
    }
}

これは、JPanelを複数回(ここでは2回だけ)表示するための私の最新の試みです。

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);
}

この後に実行されるGUIは700x110であり、そのうちの左端の100x100のみが私のTimerPaneパネルの1つに使用されています。同じコードでGridLayoutも試しましたが、2番目の「スポット」のTimerPaneのみが表示されます。助言がありますか?

4

1 に答える 1

1

まず、staticメンバー変数、、、buttonおよびを削除してください。そうでなければ、それらを持っているということは、それらがすべてのインスタンスによって共有されていることを意味します。これで2つのタイマーパネルが表示されます。labeltLabeltitlestaticTimerPane

次に、をインスタンスに変更BorderLayoutし、FlowLayoutのインスタンスをいくつか追加できますTimerPane

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

panel.add(new TimerPane());
panel.add(new TimerPane());
于 2012-09-11T18:52:02.330 に答える