使用scrollpane

时间:2012-09-01 20:20:45

标签: java swing scrollpane

我正在努力让滚动条工作。

代码显示我选择的图像。我刚刚阅读(在stackoverflow上)关于将图像添加到面板,然后将面板添加到滚动窗格,以及滚动窗格到框架。所以我试着这样做。

但我还是无法让滚动条显示出来。即使图像大于JFrame窗口。

有人可以帮忙吗?我尝试过重新验证,但它没有用,所以我把它拿出来了。

   import java.awt.*;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MakeResizedImage {
    private JFrame frame;
    private JFileChooser fc;
    private File file;
    private int r;
    private JTextField textField;
    private Image img;
    JScrollPane scrollPane;
    ImagePanel panel_2;

    public static void main(String[] args) {
        MakeResizedImage MRI = new MakeResizedImage();
        MRI.BuildJFrameGui();
    }

    private MakeResizedImage() {
    }

    private void BuildJFrameGui() {
        JPanel panel = new JPanel();
        panel.setLayout(null);

        frame = new JFrame("View/Resize a png, jpg or gif image");
        frame.setSize(400, 400);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        JPanel panel_1 = new JPanel();
        frame.getContentPane().add(panel_1, BorderLayout.NORTH);

        JLabel lblNewLabel = new JLabel("Image:");
        lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);
        panel_1.add(lblNewLabel);

        textField = new JTextField();
        panel_1.add(textField);
        textField.setColumns(10);

        JLabel lblNewLabel_1 = new JLabel("Width:");
        panel_1.add(lblNewLabel_1);

        Dimension Dim = new Dimension(45, 20);
        JSpinner spinner = new JSpinner();
        spinner.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0),
                null, new Integer(1)));
        spinner.setPreferredSize(Dim);
        panel_1.add(spinner);

        JLabel lblHeight = new JLabel("Height:");
        panel_1.add(lblHeight);

        JSpinner spinner_1 = new JSpinner();
        spinner_1.setModel(new SpinnerNumberModel(new Integer(0),
                new Integer(0), null, new Integer(1)));
        spinner_1.setPreferredSize(Dim);
        panel_1.add(spinner_1);

        JPanel panel_3 = new JPanel();

        JButton btnNewButton = new JButton("Open");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                chooseFile();
            }
        });

        panel_3.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("Cancel");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.exit(0);
            }
        });
        panel_3.add(btnNewButton_1);

        JButton btnSave = new JButton("Save");
        panel_3.add(btnSave);

        frame.getContentPane().add(panel_3, BorderLayout.SOUTH);

        scrollPane = new JScrollPane();
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);

        panel_2 = new ImagePanel();
        scrollPane.setViewportView(panel_2);

        frame.setVisible(true);
    }

    public void chooseFile() {
        if (aFileChosen()) {
            file = fc.getSelectedFile();
            if (file.exists()) {
                textField.setText(fc.getName());
                try {
                    img = ImageIO.read(file);
                    panel_2.setImg(img);
                } catch (IOException e) {
                }
            }
        } else
            JOptionPane.showMessageDialog(frame, "No file was chosen");
    }

    public boolean aFileChosen() {
        fc = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
                "PNG, JPG & GIF Images", "png", "jpg", "gif");
        fc.setFileFilter(filter);
        fc.setApproveButtonText("Choose");
        fc.setApproveButtonToolTipText("Selects the image you want to resize");
        r = fc.showOpenDialog(frame);
        if (r == JFileChooser.APPROVE_OPTION)
            return true;
        return false;
    }

    class ImagePanel extends JPanel {

        private static final long serialVersionUID = 1L;
        private Image img;

        ImagePanel() {
        };

        ImagePanel(Image img) {
            this.img = img;
        }

        public void setImg(Image img) {
            this.img = img;

            repaint();
        }

        @Override
        public Dimension getPreferredSize() {
            return (new Dimension(300, 300));
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            // Clears the previously drawn image.     
            g.clearRect(0, 0, getWidth(), getHeight());
            //and draws the new one...
            g.drawImage(img, 0, 0, this);
        }
    }
} 

2 个答案:

答案 0 :(得分:2)

使用另一个构造函数,即包含另一个组件的构造函数:JScrollPane(Component)

答案 1 :(得分:0)

    import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

/**
 * This Swing program demonstrates how to scroll a table row to visible area
 * programmatically.
 * @author www.codejava.net
 *
 */
public class JTableRowScrollingExample extends JFrame implements ActionListener {

    private JTable table = new JTable();
    private JLabel label = new JLabel("Row number: ");
    private JTextField fieldRowNumber = new JTextField(5);
    private JButton buttonScroll = new JButton("Scroll to");

    public JTableRowScrollingExample() {
        super("JTable Row Scrolling Example");

        String columnNames[] = {"No#", "Name", "Age", "Job"};
        String[][] data = {
                {"1", "John", "30", "Developer"},
                {"2", "Jane", "31", "Designer"},
                {"3", "Peter", "28", "Programmer"},
                {"4", "Mary", "35", "Consultant"},
                {"5", "Kim", "27", "Developer"},
                {"6", "Geogre", "32", "Leader"},
                {"7", "Dash", "36", "Analyst"},
                {"8", "Tim", "25", "Designer"},
                {"9", "Ana", "29", "Developer"},
                {"10", "Tom", "41", "Manager"},
                {"11", "Sam", "40", "Consultant"},
                {"12", "Patrick", "38", "Manager"},
                {"13", "Jeremy", "24", "Programmer"},
                {"14", "David", "25", "Programmer"},
                {"15", "Steve", "26", "Designer"},
        };

        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        table.setModel(model);

        JScrollPane scrollpane = new JScrollPane(table);
        scrollpane.setPreferredSize(new Dimension(300, 150));

        add(scrollpane, BorderLayout.CENTER);

        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());
        panel.add(label);
        panel.add(fieldRowNumber);
        panel.add(buttonScroll);

        add(panel, BorderLayout.NORTH);

        buttonScroll.addActionListener(this);

        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

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

            @Override
            public void run() {
                new JTableRowScrollingExample().setVisible(true);
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        int rowNumber = Integer.parseInt(fieldRowNumber.getText());
        Rectangle cellRect = table.getCellRect(rowNumber, 0, false);
        System.out.println(cellRect);
        table.scrollRectToVisible(cellRect);
    }

}