I have this application that I am working on, I have a JFrame that uses a BorderLayout. In the JFrame is a JPanel, the JPanel is using a MigLayout.I am trying to make a JButton stay on the bottom left hand side of the screen , achieving a similar effect as the "position:fixed" property in CSS.

Any tips on how I can achieve this effect by code?

Tried JLayeredPane code from a few other sources, but still doesn't work somehow.

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                    if (info.getName().equals("Nimbus")) {
                        UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
                MainFrame frame = new MainFrame();
                ForumMainPage panel = new ForumMainPage(frame);

                panel.setFocusable(true);
                panel.requestFocusInWindow();
                frame.add(jLabelOnJButton());
                frame.setContentPane(panel);
                frame.pack();


                frame.setVisible(true);


            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public MainFrame() {


    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension dim = tk.getScreenSize();

    this.setSize(dim);
    this.setUndecorated(true);
    this.setExtendedState(Frame.MAXIMIZED_BOTH);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.setLayout(new BorderLayout());
    this.add(jLabelOnJButton(),BorderLayout.CENTER);

}
 private static JComponent jLabelOnJButton(){
        JLayeredPane layers = new JLayeredPane();

        JLabel label = new JLabel("label");
        JButton button = new JButton("button");

        label.setBounds(40, 20, 100, 50);
        button.setBounds(100, 20, 150, 75);

        layers.add(label, new Integer(200));
        layers.add(button, new Integer(100));

        return layers;
    }
   }

Thanks in advance!

有帮助吗?

解决方案

You could add the button to the frame's glass pane. This takes a little bit of work to make happen, as you need to keep track of the scroll pane relative to the glass pane, but it should achieve the desired effect

overlay button

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class OverlayButton {

    public static void main(String[] args) {
        new OverlayButton();
    }

    public OverlayButton() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JScrollPane sp;
        private JButton btn;
        private JTextArea ta;

        private JPanel glassPane;

        public TestPane() {
            setLayout(new BorderLayout());
            btn = new JButton("Print");
            ta = new JTextArea(10, 20);

            BufferedReader br = null;
            try {
                br = new BufferedReader(new FileReader(new File("Script.txt")));
                String text = null;
                while ((text = br.readLine()) != null) {
                    ta.append(text + "\n");
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    br.close();
                } catch (Exception e) {
                }
            }

            ta.setCaretPosition(0);
            sp = new JScrollPane(ta);
            glassPane = new JPanel() {

                @Override
                public void doLayout() {
                    Point p = sp.getLocation();
                    Dimension dim = sp.getSize();
                    p = SwingUtilities.convertPoint(sp, p, this);

                    btn.setSize(btn.getPreferredSize());
                    int barWidth = sp.getVerticalScrollBar().getWidth();
                    int barHeight = sp.getHorizontalScrollBar().getHeight();
                    int x = p.x + (dim.width - btn.getWidth()) - barWidth;
                    int y = p.y + (dim.height - btn.getHeight()) - barHeight;
                    btn.setLocation(x, y);
                }

            };
            glassPane.setOpaque(false);
            glassPane.add(btn);
            glassPane.addComponentListener(new ComponentAdapter() {

                @Override
                public void componentResized(ComponentEvent e) {
                    e.getComponent().doLayout();
                }

            });
            add(sp);
        }

        @Override
        public void addNotify() {
            super.addNotify();
            SwingUtilities.getRootPane(this).setGlassPane(glassPane);
            glassPane.setVisible(true);
            glassPane.revalidate();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.dispose();
        }

    }

}

Take a look at How to use Root Panes for more details

其他提示

Try using the OverlayLayout:

JPanel contentPane = new JPanel();
contentPane.setLayout( new OverlayLayout(ContentPane) );
frame.setContentPane( panel );

JButton button = new JButton(...);
button.setAlignmentX(0.0f);
button.setAlignmentY(1.0f);
contentPane.add( button );

ForumMainPage panel = new ForumMainPage(frame);
contentPane.add( panel );
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top