移动左侧css的50%按钮

时间:2017-07-15 19:09:58

标签: html5 css3

为什么这不起作用?

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class TimerMemoryLeak {

    public static void main(String[] args) {
        TimerMemoryLeak timer = new TimerMemoryLeak();
        timer.buildUI();
    }

    public void buildUI() {
        showPanel1();

        frame.setSize(600, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void showPanel1() {
        Page1 page1 = new Page1();
        if (currentPanel != null) {
            pane.remove(((Page2) currentPanel).getPanel());
        }
        pane.add(page1.getPanel());
        currentPanel = page1;
        page1.startTimer();

        page1.setNextAction(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPanel2();
            }
        });

        pane.revalidate();
        pane.repaint();
    }

    public void showPanel2() {
        Page2 page2 = new Page2();
        if (currentPanel != null) {
            ((Page1) currentPanel).stopTimer(); // Comment this for memory leak
            pane.remove(((Page1) currentPanel).getPanel());
        }
        pane.add(page2.getPanel());
        currentPanel = page2;

        page2.setPreviousAction(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPanel1();
            }
        });

        pane.revalidate();
        pane.repaint();
    }

    private JFrame frame = new JFrame();
    private Container pane = frame.getContentPane();

    private Object currentPanel;
}

class Page1 {

    public Page1() {
        panel.add(title, BorderLayout.NORTH);
        panel.add(textTimer);
        panel.add(btnNext, BorderLayout.SOUTH);
    }

    public void setNextAction(ActionListener listener) {
        btnNext.addActionListener(listener);
    }

    public JPanel getPanel() {
        return panel;
    }

    public void startTimer() {
        timer.setInitialDelay(0);
        timer.start();
    }

    public void stopTimer() {
        timer.stop();
    }

    private JPanel panel = new JPanel(new BorderLayout());
    private JLabel title = new JLabel("Panel 1");
    private JButton btnNext = new JButton("Next");
    private JLabel textTimer = new JLabel();
    private int timerInterval = 1000;
    private ActionListener timerAction = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            textTimer.setText(Math.random() + "");
        }
    };
    private Timer timer = new Timer(timerInterval, timerAction);
}

class Page2 {

    public Page2() {
        panel.add(title, BorderLayout.NORTH);
        panel.add(btnPrev, BorderLayout.SOUTH);
    }

    public void setPreviousAction(ActionListener listener) {
        btnPrev.addActionListener(listener);
    }

    public JPanel getPanel() {
        return panel;
    }

    private JPanel panel = new JPanel(new BorderLayout());
    private JLabel title = new JLabel("Panel 2");
    private JButton btnPrev = new JButton("Previous");
}

1 个答案:

答案 0 :(得分:2)

您没有详细了解CSS,但lefttopbottomright仅适用于定位元素。这意味着将位置设置为absoluterelativefixedsticky的元素。

https://developer.mozilla.org/en-US/docs/Web/CSS/left

了解详情