我有一个简单的应用程序,我想在可滚动面板中显示一个大图像。我正在使用http://today.java.net/pub/a/today/2007/03/27/navigable-image-panel.html
中的NavigableImagePanel首先是我得到的结果 -
图像目前是顶部按钮附近的一个非常小的面板。这是BorderLayout.CENTER
NavigableImagePanel的代码:
和我的OpenImage.java代码:
import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class OpenImage extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 9066218264791891436L;
Image img;
public OpenImage() throws IOException{
super("Resize and Rotate");
setDefaultCloseOperation(EXIT_ON_CLOSE);
BorderLayout grid = new BorderLayout();
//this.setLayout(grid);
setSize(900,700);
setVisible(true);
//Row 1 holds some important buttons
//FlowLayout layout1 = new FlowLayout();
//MigLayout mig = new MigLayout();
JPanel row1 = new JPanel();
//LayoutManager grid = new BoxLayout(row1, BoxLayout.X_AXIS);
//row1.setLayout(grid);
//row1.setLayout(BorderLayout.NORTH);
//row1.setMaximumSize(new Dimension(100,100));
BorderLayout border = new BorderLayout();
//row1.setPreferredSize(new Dimension(0, 400));
JButton open = new JButton ("Open");
open.addActionListener(this);
JButton rotate = new JButton("Rotate");
rotate.addActionListener(this);
JButton resize = new JButton("Resize");
resize.addActionListener(this);
JButton exit = new JButton ("Exit");
exit.addActionListener(this);
row1.add(open);
row1.add(rotate);
row1.add(resize);
row1.add(exit);
//This section has a workable picture panel, but it is too large.
//ImagePanel imagepanel = new ImagePanel();
BorderLayout grid1 = new BorderLayout();
Container cp = getContentPane();
cp.setLayout(grid1);
//add(row1);
//JScrollPane row2 = new JScrollPane(imagepanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
BufferedImage img = ImageIO.read(new File("/home/adam/snow.jpg"));
NavigableImagePanel imagepanel = new NavigableImagePanel(img);
JPanel row2 = new JPanel();
row2.add(imagepanel);
//row2.repaint();
JButton save = new JButton("Save");
JPanel row3 = new JPanel();
row3.add(save);
cp.add(BorderLayout.NORTH, row1);
cp.add(BorderLayout.CENTER, row2);
cp.add(BorderLayout.SOUTH, row3);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command == "Exit"){
System.exit(0);
}
if (command == "Open"){
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(this);
}
}
//This method below is now being deprecat
public static void main(String [] args){
try {
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
UIManager.put("swing.boldMetal", Boolean.FALSE);
try {
JFrame frame = new OpenImage();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 0 :(得分:5)
Image
。setVisible(true);
必须是构造函数中的最后一行代码。setBackground()
都可以帮助您找到任何问题。pack()
代替setSize(900,700);
imagepanel.setPreferredSize(new Dimension(600, 400));
,错误JComponents
必须返回首选尺寸,然后pack()
才能正常工作。
来自代码
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class OpenImage extends JFrame implements ActionListener {
private static final long serialVersionUID = 9066218264791891436L;
private Image img;
public OpenImage() {
super("Resize and Rotate");
JButton open = new JButton("Open");
open.addActionListener(this);
JButton rotate = new JButton("Rotate");
rotate.addActionListener(this);
JButton resize = new JButton("Resize");
resize.addActionListener(this);
JButton exit = new JButton("Exit");
exit.addActionListener(this);
JPanel row1 = new JPanel();
row1.setBackground(Color.red);
row1.setLayout(new FlowLayout());
row1.setBorder(new LineBorder(Color.black, 1));
row1.add(open);
row1.add(rotate);
row1.add(resize);
row1.add(exit);
JPanel imagepanel = new JPanel();
imagepanel.setLayout(new BorderLayout());
imagepanel.setBackground(Color.blue);
imagepanel.setBorder(new LineBorder(Color.black, 1));
imagepanel.setPreferredSize(new Dimension(600, 400));
JPanel row2 = new JPanel();
row2.setLayout(new BorderLayout(10, 10));
row2.setBorder(new LineBorder(Color.black, 1));
row2.add(imagepanel, BorderLayout.CENTER);
row2.setBackground(Color.red);
JButton save = new JButton("Save");
JPanel row3 = new JPanel();
row3.setBorder(new LineBorder(Color.black, 1));
row3.setBackground(Color.green);
row3.setLayout(new FlowLayout());
row3.add(save);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout(10, 10));
add(BorderLayout.NORTH, row1);
add(BorderLayout.CENTER, row2);
add(BorderLayout.SOUTH, row3);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command == "Exit") {
System.exit(0);
}
if (command == "Open") {
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(this);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
OpenImage openImage = new OpenImage();
}
});
}
}