如何使用Java中的ImageIcon在JLabel上修复JScrollBar?

时间:2017-06-07 19:24:40

标签: java swing awt jlabel

任何人都可以帮助我吗?我正在尝试创建一个JFrame我可以调整JScrollBar并使用JLabelImageIcon的顶部到底部更改视图,包括另一个JLabel 1}} MouseEvent JFileChooser

不幸的是,我的代码没有像我想象的那样工作。任何想法为什么这样做?或者我应该注意哪些遗漏代码?我唯一的目标是让JScrollBar同时适用JLabels

public class DocVieweR extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private JPanel contentPane;
    private int yPos = 0;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    DocVieweR frame = new DocVieweR();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public DocVieweR() {
        setTitle("DocVieweR");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(816, 705);
        setLocationRelativeTo(null);        
        setResizable(false);
        contentPane = new JPanel();
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JScrollBar scrollBar = new JScrollBar();
        scrollBar.addAdjustmentListener(new AdjustmentListener() {
            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                yPos =+ e.getValue();               
            }
        });
        scrollBar.setBounds(793, 0, 17, 676);
        contentPane.add(scrollBar);     

        JLabel lblPHOTO1x1 = new JLabel("SELECT IMAGE");
        lblPHOTO1x1.setFont(new Font("Segoe UI", Font.BOLD, 16));
        lblPHOTO1x1.setHorizontalAlignment(SwingConstants.CENTER);
        lblPHOTO1x1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                JFileChooser chosenFILE = new JFileChooser();
                chosenFILE.showOpenDialog(null);
                File getFILE = chosenFILE.getSelectedFile();
                ImageIcon pic = new ImageIcon(getFILE.getAbsolutePath());
                lblPHOTO1x1.setIcon(pic);
            }
        });
        lblPHOTO1x1.setBounds(585, 228, 129, 128);
        contentPane.add(lblPHOTO1x1);       

        JLabel lblBIODATA = new JLabel("");
        ImageIcon img = new ImageIcon("resources/BIODATA_96dpi.jpg");
        lblBIODATA.setIcon(img);
        lblBIODATA.setBounds(0, yPos, 793, 1122);
        contentPane.add(lblBIODATA);
    }
}

1 个答案:

答案 0 :(得分:0)

当我应该使用JScrollPane时,我一直在使用JScrollBar。另外,我偶然发现你必须添加额外的代码行,例如" scrollPane.setViewportView(JLabelNAME);"为了使它工作,我还注意到,一旦你使用JFileChooser导入图像后移动滚动条,图像消失但不会成为一个问题(一些调试可能会有所帮助)。 contentPane.setLayout(null);和setBounds(...)不是问题,但仍然给了我这个想法,尽管如此。谢谢你! :d

公共类DocVieweR扩展了JFrame {

private static final long serialVersionUID = 1L;
private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                DocVieweR frame = new DocVieweR();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public DocVieweR() {
    setTitle("DocVieweR");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(818, 705);
    setLocationRelativeTo(null);        
    setResizable(false);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);        

    JLabel lblPHOTO1x1 = new JLabel("SELECT IMAGE");
    lblPHOTO1x1.setFont(new Font("Segoe UI", Font.BOLD, 16));
    lblPHOTO1x1.setHorizontalAlignment(SwingConstants.CENTER);
    lblPHOTO1x1.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            JFileChooser chosenFILE = new JFileChooser();
            chosenFILE.showOpenDialog(null);
            File getFILE = chosenFILE.getSelectedFile();
            ImageIcon pic = new ImageIcon(getFILE.getAbsolutePath());
            lblPHOTO1x1.setIcon(pic);
        }
    });     
    lblPHOTO1x1.setBounds(585, 228, 129, 128);
    contentPane.add(lblPHOTO1x1);
    //scrollPane.setViewportView(lblPHOTO1x1);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(0, 0, 812, 676);
    contentPane.add(scrollPane);        

    JLabel lblBIODATA = new JLabel("");
    ImageIcon img = new ImageIcon("resources/BIODATA_96dpi.jpg");
    lblBIODATA.setIcon(img);
    lblBIODATA.setSize(793, 1122);
    scrollPane.setViewportView(lblBIODATA);
    contentPane.add(lblBIODATA);

}

}