2012-09-01 96 views
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); 
     } 
    } 
} 
+0

我們不neccessarily需要看到完整的源代碼,只是張貼相關的代碼 – Jimmt

+0

我認爲相關位是滾動面板的東西我剛加入 - – user1501247

+0

frame.getContentPane()加(panel_3,BorderLayout的。南); scrollPane = new JScrollPane(); frame.getContentPane()。add(scrollPane,BorderLayout.CENTER); panel_2 = new ImagePanel(); scrollPane.setViewportView(panel_2); – user1501247

回答

2

使用其他構造,即包裝另一個組件之一:JScrollPane(Component)

+0

這就是它!我再次提醒重新驗證,以及您的建議,它看起來不錯。謝謝你的幫助。 Mike – user1501247

+3

不要只是感謝他,點擊'勾號'接受;) – Reimeus

+1

+1過了一段時間,你可以通過點擊[空格勾號](http://meta.stackexchange.com/)來接受這個答案。問題/ 5234/how-does-accepting-an-answer-work/5235#5235)。 – trashgod

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); 
    } 

}