java - 滚动 Pane 不会出现,直到我单击它们并且没有可用的栏

标签 java swing

不确定我是否将其添加到了错误的图层。可能在这方面有不少错误。我将其添加到小程序中,因为我将删除 JFrame。 JFrame 仅用于测试目的。我不知道我是否设置了滚动 Pane 错误或什么。最终目标是使用按钮向 jpanel 添加图片,并且如果图片大于面板,则能够滚动。

public class main {

    public static JFrame f = new JFrame();
    public static JApplet a = new JApplet();
    public static JPanel p = new JPanel();
    public static JButton b = new JButton();
    public static JScrollPane pane = new JScrollPane(p, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    public static JLabel gameimage; 
    public static void main(String[] args) {

        f.setSize(1000,800);

        a.setBounds(0,0,1000,800);
        a.setVisible(true);
        a.setBackground(Color.WHITE);

        p.setBounds(0, 0, 1000, 800);
        p.setVisible(true);
        p.setBackground(Color.WHITE);
        p.setLayout(null);
        p.setOpaque(true);

        pane.getVerticalScrollBar();
        pane.getHorizontalScrollBar();
        pane.setVisible(true);

        b.setBounds(955, 0, 40, 30);
        b.setText("+");
        b.setFont(new Font("Times Roman", Font.BOLD, 30));
        b.setVisible(true);
         b.setBorder(javax.swing.BorderFactory.createLineBorder(Color.WHITE));

        b.setBackground(Color.WHITE);
        b.setForeground(Color.GREEN);

        b.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    JFileChooser file = new JFileChooser();

                    file.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                    file.setAcceptAllFileFilterUsed(true);

                    if(file.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                        int width;
                        int height;
                        File f = file.getSelectedFile();
                        try {
                        BufferedImage bimg = ImageIO.read(new File(f.getAbsolutePath()));
                        width = bimg.getWidth();
                        height = bimg.getHeight();
                        String fname = f.getAbsolutePath();
                        gameimage = new JLabel("", new ImageIcon(fname), JLabel.CENTER);
                        gameimage.setSize(width,height);
                        gameimage.setOpaque(true);

                        a.revalidate();
                        a.repaint();
                        p.removeAll();
                        p.revalidate();
                        p.repaint();
                        pane.revalidate();
                        pane.repaint();
                        p.setSize(width,height);
                        p.add(gameimage);
                        p.add(b);
                        //p.add(pane);
                        a.getContentPane().add(pane);
                        }catch(IOException ioe) {

                        }


                    }else {
                        System.out.println("not working");
                    }
                }
        });

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.setResizable(false);
        f.add(a);
        a.add(p);
        p.add(b);
    }
}

最佳答案

我不会详细讨论,而是对代码进行注释

要点是:

  • 避免使用 null 布局,它们永远不会起作用,而且 Swing API 旨在操作布局管理器 API
  • 小程序已弃用,您将无法从中获得任何 JFrame 或其他基于窗口的组件无法获得的功能

虽然我没有直接执行任务,但 static 也是一个坏主意(在您使用它的上下文中),如果可以的话尽量避免它

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class Main {

    public static JFrame f = new JFrame();
    // Pointless, applets are deprecated
    // Besides, this isn't how you use them
    //public static JApplet a = new JApplet();
    public static JPanel p = new JPanel();
    public static JButton b = new JButton();
    public static JScrollPane pane = new JScrollPane(p, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    public static JLabel gameimage;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {

                f.setSize(1000, 800);

                //a.setBounds(0, 0, 1000, 800);
                //a.setVisible(true);
                //a.setBackground(Color.WHITE);
                // Pointless, the layout manager of the JScrollPane
                // will make these choices
                //p.setBounds(0, 0, 1000, 800);
                p.setVisible(true);
                p.setBackground(Color.WHITE);
                // !! This is root of your problem !!
                //p.setLayout(null);
                p.setLayout(new BorderLayout());
                // Pointless, it already is
                //p.setOpaque(true);

                // Pointless, you don't do anything with the returned value
                //pane.getVerticalScrollBar();
                //pane.getHorizontalScrollBar();
                // Pointless, it already is
                //pane.setVisible(true);
                // Pointless
                //b.setBounds(955, 0, 40, 30);
                b.setText("+");
                b.setFont(new Font("Times Roman", Font.BOLD, 30));
                // Pointless
                //b.setVisible(true);
                b.setBorder(javax.swing.BorderFactory.createLineBorder(Color.WHITE));
                b.setBackground(Color.WHITE);
                b.setForeground(Color.GREEN);

                b.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        JFileChooser file = new JFileChooser();

                        file.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                        file.setAcceptAllFileFilterUsed(true);

                        if (file.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                            int width;
                            int height;
                            File f = file.getSelectedFile();
                            try {
                                BufferedImage bimg = ImageIO.read(new File(f.getAbsolutePath()));
                                width = bimg.getWidth();
                                height = bimg.getHeight();
                                String fname = f.getAbsolutePath();
                                if (gameimage != null) {
                                    p.remove(gameimage);
                                }
                                gameimage = new JLabel("", new ImageIcon(fname), JLabel.CENTER);
                                gameimage.setSize(width, height);
                                gameimage.setOpaque(true);

                                //a.revalidate();
                                //a.repaint();
                                //p.removeAll();
                                // Not really what you want to do right now
                                // Besides, if you use a null layout, it won't do anything
                                //p.revalidate();
                                //p.repaint();
                                //pane.revalidate();
                                //pane.repaint();
                                //p.setSize(width, height);
                                p.add(gameimage);
                                //p.add(b);
                                //p.add(pane);
                                // Presumaly, pane should alredy be added to a container
                                //a.getContentPane().add(pane);
                                p.revalidate();
                                p.repaint();
                            } catch (IOException ioe) {

                            }

                        } else {
                            System.out.println("not working");
                        }
                    }
                });

                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                // Not what you want to call here
                //f.setVisible(true);
                // Probably not the best idea right now
                f.setResizable(false);
                //f.add(a);
                //a.add(p);
                // Good idea to add the scollpane to the container
                f.add(pane);
                p.add(b, BorderLayout.SOUTH);
                f.setVisible(true);
            }
        });
    }
}

关于java - 滚动 Pane 不会出现,直到我单击它们并且没有可用的栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46683456/

相关文章:

java - 使用列表值映射解析 Yaml 时出现 Jackson 异常

javascript - 使用 Active Directory(带 LDAP)在前端的 angularjs/javascript 上进行身份验证 - 流程流程应该是什么?

java - 如何在 JPanel 上保存图形的当前状态

Java - 如何将 JProgressBar 与 SwingWorker 一起使用

java - table.getSelectedRow() 不起作用

java - 有没有办法覆盖 JUnit 以将响应发送回另一个方法?

java - 请求访问 token 时 reddit api 返回 429 错误

java - 来自 Java 的 Base64 字符串损坏

java - JTable新列添加不成功

java - 当 TextField 中的字符串超过 5 个字符时启用按钮