我处于必须重用和修改其他人的代码来创建图像处理应用程序的位置。以下代码用于设置框架及其 GUI。我遇到的问题是,当在菜单栏上单击文件字时,菜单项列在 originalImage 容器中包含的画布下方。这是代码:
public ImageLoader(){
super("IRIS Application");
JMenu fileMenu = new JMenu("File");
JMenuItem fileOpenChoice = new JMenuItem("Open File");
fileOpenChoice.addActionListener(new FileOpener());
fileMenu.add(fileOpenChoice);
JMenuItem fileSaveChoice = new JMenuItem("Save File");
fileSaveChoice.addActionListener(new FileSaver());
fileMenu.add(fileSaveChoice);
JMenu processMenu = new JMenu("Process");
JMenuItem reduceContrast = new JMenuItem("Reduce Contrast");
reduceContrast.addActionListener(new ReduceContrast());
processMenu.add(reduceContrast);
JMenuItem scale = new JMenuItem("Scale");
scale.addActionListener(new Scaler());
processMenu.add(scale);
JMenuItem sobel = new JMenuItem("Sobel");
sobel.addActionListener(new Sobel());
processMenu.add(sobel);
JMenuItem prewitt = new JMenuItem("Prewitt");
prewitt.addActionListener(new Prewitt());
processMenu.add(prewitt);
originalImage = new BufferedImage(565, 584, BufferedImage.TYPE_BYTE_GRAY);
processedImage = new BufferedImage(565, 584, BufferedImage.TYPE_BYTE_GRAY);
originalRaster = originalImage.getRaster();
processedRaster = processedImage.getRaster();
ic = new ImageCanvas();
pc = new ImageCanvas();
Container content = this.getContentPane();
content.setLayout(new FlowLayout());
JPanel originalContainer = new JPanel();
originalContainer.add(ic);
originalContainer.setSize(ic.getWidth(), ic.getHeight());
originalContainer.setBorder(new EtchedBorder());
JPanel processedContainer = new JPanel();
processedContainer.add(pc);
processedContainer.setSize(pc.getWidth(), pc.getHeight());
processedContainer.setBorder(new EtchedBorder());
JPanel imagePanel = new JPanel();
imagePanel.setLayout(new FlowLayout());
imagePanel.add(originalContainer);
imagePanel.add(processedContainer);
//content.add(imagePanel, BorderLayout.NORTH);
JPanel originalGrid = new JPanel();
originalGrid.setLayout(new GridLayout(0,1,5,0));
JPanel processedGrid = new JPanel();
processedGrid.setLayout(new GridLayout(0,1,5,0));
originalText = new JTextField("Original Text");
JButton originalButton = new JButton("View Coordinates Table");
originalGrid.add(originalText);
originalGrid.add(originalButton);
JTextField processedText = new JTextField("Processed Text");
JButton processedButton = new JButton("View Coordinates Table");
processedGrid.add(processedText);
processedGrid.add(processedButton);
JPanel informationPanel = new JPanel();
informationPanel.setBackground(Color.red);
informationPanel.setLayout(new BorderLayout());
informationPanel.add(originalGrid, BorderLayout.WEST);
informationPanel.add(processedGrid, BorderLayout.EAST);
//content.add(informationPanel, BorderLayout.SOUTH);
ic.addMouseMotionListener(new ImageMouseMotionListener(ic, originalText));
pc.addMouseMotionListener(new ImageMouseMotionListener(pc, processedText));
JMenuBar bar = new JMenuBar();
bar.add(fileMenu);
bar.add(processMenu);
content.add(imagePanel, BorderLayout.NORTH);
content.add(informationPanel, BorderLayout.SOUTH);
setJMenuBar(bar);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1200, 725);
setResizable(false);
setVisible(true);
setLocation(125, 0);
}
我知道菜单栏很好,所以我假设它是我将画布添加到 JPanel 的方式。是否应该在 JPanel 中设置其 z 顺序?我已经阅读了我家里的书籍,没有提到这种问题。谢谢,丹尼尔