0
我有兩個JPanels
,每個都有一堆JButtons
。包含另外兩個的JPanel
具有BoxLayout
,另外兩個JPanels
是FlowLayout
。問題在於窗口大小調整以使按鈕環繞時,第一個和第二個面板中的按鈕(p0
,p2
)被其相應面板的邊緣覆蓋。有任何想法嗎?隱藏在JPanel中的Java組件
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Scrollable;
import javax.swing.border.TitledBorder;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p0 = new JPanel();
p0.setLayout(new FlowLayout());
p0.setBorder(new TitledBorder("p0"));
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout());
p1.setBorder(new TitledBorder("p1"));
CustomPanel panel = new CustomPanel();
JScrollPane sp = new JScrollPane(panel);
panel.add(p0);
panel.add(p1);
frame.add(sp);
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
p0.add(new JButton("Hello World!"));
p0.add(new JButton("Hello World!"));
p0.add(new JButton("Hello World!"));
p0.add(new JButton("Hello World!"));
p0.add(new JButton("Hello World!"));
p0.add(new JButton("Hello World!"));
p0.add(new JButton("Hello World!"));
p0.add(new JButton("Hello World!"));
p1.add(new JButton("Hello World!"));
p1.add(new JButton("Hello World!"));
p1.add(new JButton("Hello World!"));
p1.add(new JButton("Hello World!"));
p1.add(new JButton("Hello World!"));
p1.add(new JButton("Hello World!"));
p1.add(new JButton("Hello World!"));
p1.add(new JButton("Hello World!"));
frame.setVisible(true);
}
}
class CustomPanel extends JPanel implements Scrollable {
@Override
public Dimension getPreferredScrollableViewportSize() {
return null;
}
@Override
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
return 0;
}
@Override
public boolean getScrollableTracksViewportHeight() {
return false;
}
@Override
public boolean getScrollableTracksViewportWidth() {
return true;
}
@Override
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
return 0;
}
}
謝謝!很棒! – 2014-10-03 16:58:45