1

我正在尝试让滚动条工作。

代码显示我选择的图像。而且我刚刚(在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);
        }
    }
} 
4

2 回答 2

2

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

于 2012-09-01T20:29:42.270 回答
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);
    }

}
于 2017-11-28T10:02:18.273 回答