简体   繁体   English

在Swing中使用具有绝对布局的滚动条

[英]Using scrollbars with absolute layout in Swing

I am not able to use scroll bars with absolute layout in Swing. 我无法在Swing中使用具有绝对布局的滚动条。

I don't wish to use this layout but I have to display dynamic objects on my panel on click of a button and align them using setBounds which can be done using this layout only (I guess). 我不想使用这种布局,但是我必须在单击按钮时在我的面板上显示动态对象并使用setBounds对齐它们,这可以仅使用此布局来完成(我猜)。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class clothes2 extends javax.swing.JFrame {

    JTextField n=null;
    JButton m=null;

    public clothes2(){
        initComponents();
    }

    public void initComponents() {
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        final JPanel jp = new JPanel();
        contentPane.setPreferredSize(new Dimension(320,200));
        jp.setLayout(null);
        m=new JButton("add");
        m.setBounds(0,0,50,50);
        jp.add(m);
        m.addMouseListener( new MouseAdapter() {

            int x=0;
            int y=0;

            public void mouseClicked(MouseEvent me){
                x+=100;
                y+=100;
                jp.add(n=new JTextField("Name"));
                n.setBounds(x, y, 50, 50);
                jp.add(n=new JTextField("code"));
                x+=100;
                n.setBounds(x,y, 50, 50);
                jp.revalidate();
                jp.repaint();
                x=0;
            }
        });

        int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
        int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
        JScrollPane jsp = new JScrollPane(jp, v, h);
        contentPane.add(jsp, BorderLayout.CENTER);
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame f= new clothes2();
                f.setVisible(true);
                f.setSize(640,320);
            }
        });
    }
}

display dynamic objects .. which can be done using this layout only (I guess). 显示动态对象..只能使用此布局(我猜)。

You guess wrong. 你错了。

See this GUI , that can not only change PLAFs at run-time, but also dynamically add new components 1 . 请参阅此GUI ,它不仅可以在运行时更改PLAF,还可以动态添加新组件1 Click to.. 点击进..

Add Another Label 添加另一个标签

  1. This example adds the new labels to a GridLayout - but the principle is the same for any layout (or any component). 此示例将新标签添加到GridLayout - 但任何布局(或任何组件)的原理都是相同的。

设置容器的首选大小。

JScrollBar uses the preferred size of the component inside it to determine how large the scroll bars should be, and if they should be displayed. JScrollBar使用其中组件的首选大小来确定滚动条应该有多大,以及它们是否应该显示。

Usually, the layout manager handles this using the preferredLayoutSize method. 通常,布局管理器使用preferredLayoutSize方法处理此问题。 This can be overriden by explicitly setting the preferred size of the component. 可以通过显式设置组件的首选大小来覆盖它。

So either you have to set the preferred size, or use a custom layout manager that calculates it for you. 因此,您必须设置首选大小,或使用自定义布局管理器为您计算它。

see also here 另见这里

might help you. 可能会帮助你。

添加布局
jp.setLayout(new FlowLayout());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM