這基本上是一個小黑客。
這樣做是使用屏幕邊界減去它的插入來計算不僅窗口的高度,而且還鼓勵窗口出現在可用桌面空間內的位置。
主要的原因,就是我有我的任務欄釘在我屏幕的頂部...
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;
}
}
}
爲什麼不在「JList」中顯示這些標籤? –
@sethu請你真的想做什麼 – mKorbel
我想我的JFrame擴展到任務欄的高度。寬度應該足夠適合其中的選項卡窗格。當我將滾動窗格中的面板添加到選項卡窗格中並將選項卡窗格放置在框架中時,面板中的最後一個組件會像圖像中顯示的那樣被剪切。 – sethu