有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java、JFC、Swing

我在JscrollPane上遇到了问题。尝试在一行中添加几个按钮,然后继续使用一组行,最初显示效果很好,但当我单击滚动条时,所有按钮都会有一种文字环绕在框架中的感觉。我想使用setBounds(没有任何布局),因为我必须根据元素的数量扩展/减少行

我做错了什么

图片出现在这里: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);
}

}


共 (1) 个答案

  1. # 1 楼答案

    , but as soon as I click on the scroll bar, all the buttons get sort of word wrapped in the frame.

    面板将在您开始滚动时重新验证),这将导致调用布局管理器,然后布局管理器将确定所有组件的大小和位置。JPanel的默认布局管理器是FlowLayout,因此当当前行已满时,组件将换行到新行

    不要试图使用setBounds(…)。相反,您可以按照设计的使用方式使用布局管理器

    I would like to use setBounds (without any layout) as I have to extend/reduce the rows depending on the number of elements.

    这不是不使用布局管理器的原因

    例如,您可能有一个带有垂直BoxLayout的主面板。然后,每次需要添加一行组件时,都会创建一个子面板,该子面板使用FlowLayout。将零部件添加到子面板,然后将子面板添加到主面板

    然后滚动条和滚动窗格将正常工作

    container.setPreferredSize(new Dimension(1200, 800));
    

    不要试图控制面板的首选尺寸。布局管理器的工作也是如此。滚动窗格无法使用人为的首选大小正常工作