调整JScrollPane的大小以考虑Windows任务栏 [英] Sizing a JScrollPane to take into account windows taskbar
问题描述
我开始这个新问题,是因为此问题由于没有MVCE,因此已被投票关闭.
I am starting this new question because this question has been voted to close since it didnt have an MVCE.
我现在已将问题包含在SSCCE中.
I have included the question with an SSCCE now.
请看下面的代码
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
如果添加相同的代码,
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
然后,框架位于任务栏上方.我希望JFrame停在Windows任务栏上方.
Then the frame comes above the task bar. I would like my JFrame to stop just above the taskbar of windows.
推荐答案
这基本上是个小问题.
这是使用屏幕边界减去其插图来计算窗口的高度,还计算鼓励窗口出现在可用桌面空间中的位置.
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;
}
}
}
这篇关于调整JScrollPane的大小以考虑Windows任务栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!