Sizing a JScrollPane to take into account windows taskbar [duplicate]

被刻印的时光 ゝ 提交于 2019-12-13 09:41:28

问题


I am starting this new question because this question has been voted to close since it didnt have an MVCE.

I have included the question with an SSCCE now.

Please take a look at the below code

import javax.swing.*;
import java.awt.*;

public class ScrollPaneTest {

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame frame=new JFrame();
            frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

            JTabbedPane tabbedPane=new JTabbedPane();


            tabbedPane.addTab("Tab 1", new JScrollPane(getPanel()));
            tabbedPane.addTab("Tab 2", new JScrollPane(getPanel()));

            frame.setContentPane(tabbedPane);
            frame.pack();
            frame.setVisible(true);
        }



        private JPanel getPanel() {
            JPanel panel=new JPanel();
            Box box = Box.createVerticalBox();
            for (int i = 1; i <= 100; i++) {
                box.add(new JLabel("This is Label #" + i));
            }
            panel.add(box);
            return panel;
        }
    });
  }
} 

When I run this code I get a frame shown as below

In the same code if I add a

 frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 

Then the frame comes above the task bar. I would like my JFrame to stop just above the taskbar of windows.


回答1:


This is basically a little hacky.

What this does is uses the screen bounds minus it's insets to calculate not only the height of the window, but also the position to encourage the window to appear within the available desktop space.

The main reason for this, is I have my taskbar pinned to the top of my screen...

import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class ScrollPaneTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

                JTabbedPane tabbedPane = new JTabbedPane();

                tabbedPane.addTab("Tab 1", new JScrollPane(getPanel()));
                tabbedPane.addTab("Tab 2", new JScrollPane(getPanel()));

                frame.setContentPane(tabbedPane);
                frame.pack();
                Rectangle viewBounds = getScreenViewableBounds();
                frame.setSize(frame.getWidth(), viewBounds.height);
                frame.setLocation(viewBounds.x, viewBounds.y);
                frame.setVisible(true);
            }

            private JPanel getPanel() {
                JPanel panel = new JPanel();
                Box box = Box.createVerticalBox();
                for (int i = 1; i <= 100; i++) {
                    box.add(new JLabel("This is Label #" + i));
                }
                panel.add(box);
                return panel;
            }
        });
    }

    public static Rectangle getScreenViewableBounds() {

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();

        Rectangle bounds = new Rectangle(0, 0, 0, 0);

        if (gd != null) {

            GraphicsConfiguration gc = gd.getDefaultConfiguration();
            bounds = gc.getBounds();

            Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);

            bounds.x += insets.left;
            bounds.y += insets.top;
            bounds.width -= (insets.left + insets.right);
            bounds.height -= (insets.top + insets.bottom);

        }

        return bounds;

    }
}

You may also wish to consider using the Scrollable interface to restrict the viewable area, for example...

import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.Scrollable;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class ScrollPaneTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

                JTabbedPane tabbedPane = new JTabbedPane();

                tabbedPane.addTab("Tab 1", new JScrollPane(getPanel()));
                tabbedPane.addTab("Tab 2", new JScrollPane(getPanel()));

                frame.setContentPane(tabbedPane);
                frame.pack();
                frame.setVisible(true);
            }

            private JPanel getPanel() {
                JPanel panel = new ScrollablePane();
                Box box = Box.createVerticalBox();
                for (int i = 1; i <= 100; i++) {
                    box.add(new JLabel("This is Label #" + i));
                }
                panel.add(box);
                return panel;
            }
        });
    }

    public static class ScrollablePane extends JPanel implements Scrollable {

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

        @Override
        public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 32;
        }

        @Override
        public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 32;
        }

        @Override
        public boolean getScrollableTracksViewportWidth() {
            return false;
        }

        @Override
        public boolean getScrollableTracksViewportHeight() {
            return false;
        }

    }
}


来源:https://stackoverflow.com/questions/21247623/sizing-a-jscrollpane-to-take-into-account-windows-taskbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!