我正在开始这个新问题,因为this question已被投票结束,因为它没有MVCE。
我现在已经将问题包含在SSCCE中了。
请查看以下代码
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;
}
});
}
}
当我运行此代码时,我得到一个如下所示的框架
如果我添加
,请使用相同的代码 frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
然后框架位于任务栏上方。我希望我的JFrame停在窗口任务栏的上方。
答案 0 :(得分:1)
这基本上有点hacky。
这样做是使用屏幕边界减去它的插图,不仅可以计算窗口的高度,还可以计算鼓励窗口出现在可用桌面空间内的位置。
主要原因是我将任务栏固定在屏幕顶部...
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;
}
}
您可能还希望考虑使用Scrollable
界面来限制可见区域,例如......
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;
}
}
}