java - 添加 JScrollPane 以避免组件变形

标签 java swing user-interface scroll jscrollpane

我正在尝试制作一个可变大小的界面。然而,目前,如果所有组件的大小都大于窗口的尺寸,那么这些组件就会开始
扭曲!因此,我想添加一个JScrollPane,当窗口大小不再足够时显示,以避免这种扭曲。 我刚开始添加一个 JScrollPane (如下面的代码所示),但它似乎不起作用,我不知道现在要更改什么。

JScrollPane myScrollPane = new JScrollPane();
myWindow.getContentPane().add(myScrollPane, BorderLayout.CENTER);

编辑1:这是我尝试过的一个简单示例:

static void one(String[] ari, JLabel sess_nb, Box boxy, long[] res){
    int p;
    int length = ari.length;
    Border cadre = BorderFactory.createLineBorder(Color.black);
    Font police = new Font("Monospaced 13", Font.BOLD, 18);
    Font police1 = new Font("Monospaced 13", Font.BOLD, 15);
    Font font = new Font("Monospaced 13", Font.ITALIC, 13);
    for (p=0;p<length;p++){
        Box boxz = Box.createVerticalBox();
        JLabel Rate = new JLabel("Rating Group "+r);
        JLabel rg_reser = new JLabel();
        JLabel switsh = new JLabel("1");
        JLabel reser = new JLabel(); //in this label the resrvation for each update and each rating group will be stocked
        JLabel consump = new JLabel(); //in this label will be stocked the consumption
        //----------------------------------------------------

        //choice of MB or kB
        JComboBox combo = new JComboBox();
        combo.addItem("Megabytes");
        combo.addItem("Bytes");
        combo.addItem("kilobytes");
        combo.addItem("Gigabytes");
        JLabel upload = new JLabel("Upload consumption:");
        JTextField upload_entry = new JTextField("7.5");
        JLabel download = new JLabel("Download consumption:");
        JTextField download_entry = new JTextField("7.5");
        JTextField total_entry = new JTextField();
        JLabel total = new JLabel("Total consumption:");
        JButton rg = new JButton("Next");
        JLabel update = new JLabel("Result here");
        boxz.add(Rate);
        boxz.add(total);
        boxz.add(total_entry);
        boxz.add(upload);
        boxz.add(upload_entry);
        boxz.add(download);
        boxz.add(download_entry);
        boxz.add(combo);
        boxz.add(rg);
        boxz.add(update);
        boxy.add(boxz);
        scrollPane1.add(boxy);        
    }

编辑2:这是一个新的测试代码,但它也不起作用:

public Fenetre(){
    this.setTitle("Data Simulator");
    this.setSize(300, 300);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);
    String hello = "hello";
    int number = 69;
    JPanel content = new JPanel();
    content.setBackground(Color.LIGHT_GRAY);
    //Box imad = Box.createHorizontalBox();
    JTextArea textArea = new JTextArea(10, 10);
    JLabel imad = new JLabel();
    imad.setText(hello + " your favorite number is " + number + "\nRight?");
    JScrollPane scrollPane = new JScrollPane();
    setPreferredSize(new Dimension(450, 110));

    scrollPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_ALWAYS);
    scrollPane.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
    scrollPane.setEnabled(true);
    scrollPane.setWheelScrollingEnabled(true);
    scrollPane.setViewportView(textArea);
    scrollPane.setViewportView(imad);
    add(scrollPane, BorderLayout.CENTER);
    //---------------------------------------------
    //On ajoute le conteneur
    content.add(imad);
    content.add(textArea);
    content.add(scrollPane);
    this.setContentPane(content);
    this.setVisible(true);
    this.setResizable(false);
}

出现一个小的白色恐惧(我认为这是滚动条或滚动面板?)并且窗口中的组件超出了窗口大小,但当我尝试滚动(使用鼠标或通过点击白色恐惧)时没有任何反应)。我不知道出了什么问题。

最佳答案

不要使用scrollPanel.add(boxy)-您需要将boxy添加到scrollPanel的视口(viewport),而不是scrollPanel本身。请改用 scrollPanel.setViewportView(boxy),或者最好在创建 boxy 后创建 ScrollPanel,并使用采用 Component 参数的构造函数。

关于java - 添加 JScrollPane 以避免组件变形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32209353/

相关文章:

java - 在 for 循环中同时迭代映射和数组

java - ViewModel 如何在配置更改时持久化

java - 另一个 JFrame 中的 JFrame

java - 为什么 setSize 在 Java 中不起作用

java - 从 JOptionPane 返回 JFrame

java - 我怎样才能使这个 JButton 可见?当我有逐行扫描背景 JWindow() 时?

java - 如何以独立于平台的方式访问 java 资源?

java - Okhttp + Retrofit @Body 请求 - 传输编码 : chunked added

java - 当我需要时更改 JList 项目颜色

html - 在图片背景上显示文字