Java,JFC,Swing
问题描述:
我遇到JscrollPane问题。试图在一行中添加几个按钮,并继续使用一组行,它最初显示为正常,但只要单击滚动条,所有按钮都会得到包装在框架中的单词。我想使用setBounds(没有任何布局),因为我必须根据元素的数量来扩展/缩小行。Java,JFC,Swing
我在做什么错?
产品图出现在这里:https://drive.google.com/drive/u/0/folders/0B-m6SCJULOTRdHZ6cUwxX041SHM
public class JFrameWindow extends JFrame {
private int num = 0;
private JPanel container = null;
private JScrollPane jsp = null;
public JFrameWindow(String framName) {
super(framName);
container = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
//g2.setPaint(Paint);
//g2.fillOval(0, 0, getWidth(), getHeight());
g2.dispose();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(1200, 800);
};
};
//container.setLayout(mgr);
jsp = new JScrollPane();
jsp.setViewportView(container);
jsp.setPreferredSize(new Dimension(1200, 800));
container.setPreferredSize(new Dimension(1200, 800));
//jsp.setBorder(new EtchedBorder(Color.BLACK, Color.GREEN));
//jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
this.add(jsp);
setSize(1200, 800);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void addButtons(String id, int a, int b) {
JButton button1 = new JButton(id);
button1.setBounds(10, num*30 + 10, 100, 20);
container.add(button1);
JButton stateButton = new JButton("ABC");
stateButton.setBounds(120, num*30 + 10, 100, 20);
container.add(stateButton);
JButton custButton = new JButton("DEF");
custButton.setBounds(230, num*30 + 10, 100, 20);
container.add(custButton);
JButton int1Button = new JButton("" + a);
int1Button.setBounds(340, num*30 + 10, 100, 20);
container.add(int1Button);
JButton int2Button = new JButton("" + b);
int2Button.setBounds(450, num*30 + 10, 100, 20);
container.add(int2Button);
num++;
container.repaint();
jsp.repaint();
this.repaint();
}
public static void main(String [] args) {
JFrameWindow window = new JFrameWindow ("Test");
window.addButtons("Test1", 1, 2);
window.addButtons("Test2", 1, 2);
window.addButtons("Test3", 1, 2);
window.addButtons("Test4", 1, 2);
window.addButtons("Test5", 1, 2);
window.addButtons("Test6", 1, 2);
window.addButtons("Test7", 1, 2);
window.addButtons("Test8", 1, 2);
window.addButtons("Test9", 1, 2);
window.addButtons("Test10", 1, 2);
window.addButtons("Test11", 1, 2);
window.addButtons("Test12", 1, 2);
window.addButtons("Test13", 1, 2);
window.addButtons("Test14", 1, 2);
window.addButtons("Test15", 1, 2);
window.addButtons("Test16", 1, 2);
window.addButtons("Test17", 1, 2);
window.addButtons("Test18", 1, 2);
window.addButtons("Test19", 1, 2);
window.addButtons("Test20", 1, 2);
window.addButtons("Test21", 1, 2);
window.addButtons("Test22", 1, 2);
window.addButtons("Test23", 1, 2);
window.addButtons("Test24", 1, 2);
window.addButtons("Test25", 1, 2);
window.addButtons("Test26", 1, 2);
window.addButtons("Test27", 1, 2);
window.addButtons("Test28", 1, 2);
window.addButtons("Test29", 1, 2);
window.addButtons("Test30", 1, 2);
}
}
答
,但只要我点击滚动条上,所有的按钮获得排序字包裹在框架。
当您开始滚动时,面板会重新生效),这将导致布局管理器被调用,然后布局管理器将确定所有组件的大小和位置。 JPanel
的默认布局管理器是FlowLayout
,因此当当前行已满时组件将换行到新行。
请勿尝试使用setBounds(...)。相反,您可以按照设计使用的方式使用布局管理器。
我想使用setBounds(没有任何布局),因为我必须根据元素数量来扩展/减少行数。
这不是不使用布局管理器的原因。
例如,您可能有一个垂直的主面板BoxLayout
。然后,每次需要添加一行组件时,都会创建一个使用FlowLayout
的子面板。将组件添加到子面板,然后将子面板添加到主面板。
然后滚动条和滚动条将正常工作。
container.setPreferredSize(new Dimension(1200, 800));
不要试图控制面板的首选尺寸。这又是布局经理的工作。使用人工首选尺寸时,scrollpane无法正常工作。
欢迎来到SO。您的标题没有提示任何内容,这是标签的内容。请使用更具体的内容对其进行编辑。 – Mordechai