Java 按钮不会显示在FlowLayout中

Java 按钮不会显示在FlowLayout中,java,button,user-interface,layout,Java,Button,User Interface,Layout,我正在为一个班级做一个节目。这样做的目的是显示一幅图像并上/下对比度。这是一个例子。我可以使图像出现,并在图像上方出现一个灰色条,如示例中所示,但我的按钮不在那里。我不知道如何通过FlowLayout正确地做到这一点,或者是否需要使用其他布局。我是GUI的新手,所以任何帮助都将不胜感激 这就是我的样子。我做错了什么? 导入javax.imageio.imageio; 导入javax.swing.*; 导入java.util.*; 导入java.io.*; 导入java.awt.*; 导入ja

我正在为一个班级做一个节目。这样做的目的是显示一幅图像并上/下对比度。这是一个例子。我可以使图像出现,并在图像上方出现一个灰色条,如示例中所示,但我的按钮不在那里。我不知道如何通过FlowLayout正确地做到这一点,或者是否需要使用其他布局。我是GUI的新手,所以任何帮助都将不胜感激

这就是我的样子。我做错了什么?

导入javax.imageio.imageio;
导入javax.swing.*;
导入java.util.*;
导入java.io.*;
导入java.awt.*;
导入java.awt.image.*;
导入java.awt.event.*;
导入java.applet.applet;
/**
*这个类扩展了JPanel,可以将一个图像加载到它的
*原版尺寸。
*
*@作者郭大海
*
*/
类ImagePanel扩展了JPanel{
私有缓冲图像;
公共图像面板(BuffereImage图像){
这个。图像=图像;
}
/**
*绘制图像。
*/
公共组件(图形g){
超级组件(g);
g、 drawImage(image,0,0,image.getWidth(),image.getHeight(),this);
}
}
/**
*此类的对象可以加载图像并启用用户
*增加和减少图像的对比度。
*
*注:此类只能处理灰度对比度
*
*@作者郭大海
*
*/
公共类HistGrameQualifizerApp扩展JFrame{
整数计数=0;
私人流动布局;
私人边界布局;
私人图像面板图像面板;
私有JButton递增对比度按钮;
私有JButton递减按钮;
私有缓冲图像;
/**
*累积密度函数
*/
私有整数[]cdf=新整数[256];
/**
*当前最大灰度级。
*/
私有int imageMaxGrayLevel=0;
/**
*当前最小灰度。
*/
私有int imageMinGrayLevel=300;
/**
*灰度CDF中的最小非零单元
*这是图像的一部分。
*/
私有int minCdfValue;
/**
*首次加载图像时的最大灰度。
*/
private int OriginalimaxGray;
/**
*首次加载图像时的最小灰度。
*/
私人内部原创管理;
私有整数最大灰度=255;
私有整数最小灰度=0;
/**
*设置GUI组件并为其注册操作侦听器
*用于增加和减少对比度的两个按钮
*@param filename图像的文件名
*@throws IOException在加载映像时出现问题时引发
*/
公共HistgramEqualizerApp(字符串文件名)
抛出IOException{
//插入您的代码
试一试{
image=ImageIO.read(新文件(文件名));
}捕获(IOE异常){
System.err.println(“加载图像时出现问题”);
系统出口(1);
}
//在屏幕上显示图像
添加(新JLabel(新图像图标(文件名));
myLayout=新的FlowLayout(FlowLayout.CENTER,20,40);
设置布局(myLayout);
//对比度控制按钮
IncreaseControlButton=新JButton(“>>”);

decreaseContrastButton=new JButton(“这里有一种方法可以让它工作:

public class HistgramEqualizerApp extends JFrame {

    private JButton increaseContrastButton = new JButton(">>");;
    private JButton decreaseContrastButton = new JButton("<<");;

    public HistgramEqualizerApp(String filename) throws IOException {

        JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 40));
        buttonsPanel.add(decreaseContrastButton);
        buttonsPanel.add(increaseContrastButton);
        getContentPane().add(new JLabel(new ImageIcon(filename)));
        getContentPane().add(buttonsPanel, BorderLayout.PAGE_START);
    }

    public static void main(String args[]) throws IOException {

        HistgramEqualizerApp histApp = new HistgramEqualizerApp(args[0]);
        histApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        histApp.pack();
        histApp.setVisible(true);
    }
}
公共类HistGrameQualifizerApp扩展JFrame{
私有JButton increaseControlButton=新JButton(“>>”);;
private JButton decreaseContrastButton=新JButton(“这是我的两分钱

正如我之前所说:“你应该使用边框布局,将按钮添加到面板上,然后将所述面板添加到主框架,然后将图像面板添加到中心。”

import java.awt.*;
导入java.io.IOException;
导入java.util.array;
导入java.util.List;
导入javax.swing.*;
@抑制警告(“串行”)
公共类HistorogrameQualifizerApp扩展JFrame{
私人按钮面板按钮面板;
私人图像面板图像面板;
私有JButton递增对比度按钮;
私有JButton递减按钮;
私有缓冲图像;
公共HistGrameQualifizerApp(){
容器内容=this.getContentPane();
setLayout(新的BorderLayout());
//对比度控制按钮
IncreaseControlButton=新JButton(“>>”);

decreaseContrastButton=new JButton(“您的代码不完整。请发布一个。我添加了完整代码,对不起!MCVE中的M表示最小值,“完整代码”正好相反。为什么我们需要所有功能代码来检查按钮为什么不显示?删除对问题不重要的所有内容(而且不要只是以使代码无法编译的方式删除它)“,然后设置布局。您应该使用边框布局,将按钮添加到面板,然后将所述面板添加到主框架,然后将图像面板添加到中心。@Mr.Polywhill,您帮了大忙。我已经解决了!
public class HistgramEqualizerApp extends JFrame {

    private JButton increaseContrastButton = new JButton(">>");;
    private JButton decreaseContrastButton = new JButton("<<");;

    public HistgramEqualizerApp(String filename) throws IOException {

        JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 40));
        buttonsPanel.add(decreaseContrastButton);
        buttonsPanel.add(increaseContrastButton);
        getContentPane().add(new JLabel(new ImageIcon(filename)));
        getContentPane().add(buttonsPanel, BorderLayout.PAGE_START);
    }

    public static void main(String args[]) throws IOException {

        HistgramEqualizerApp histApp = new HistgramEqualizerApp(args[0]);
        histApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        histApp.pack();
        histApp.setVisible(true);
    }
}
import java.awt.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.swing.*;

@SuppressWarnings("serial")
public class HistogramEqualizerApp extends JFrame {
    private ButtonPanel buttonPanel;
    private ImagePanel imagePanel;

    private JButton increaseContrastButton;
    private JButton decreaseContrastButton;
    private BufferedImage image;

    public HistgramEqualizerApp() {
        Container content = this.getContentPane();
        content.setLayout(new BorderLayout());

        // Buttons for contrast control
        increaseContrastButton = new JButton(">>");
        decreaseContrastButton = new JButton("<<");
        buttonPanel = new ButtonPanel(
                Arrays.asList(decreaseContrastButton, increaseContrastButton));
        content.add(buttonPanel, BorderLayout.NORTH);

        image = randomImage(80, 120, 4);
        imagePanel = new ImagePanel(image);
        content.add(imagePanel, BorderLayout.CENTER);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(512, 400);
        this.setResizable(true);
    }

    public static void main(String args[]) throws IOException {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                HistgramEqualizerApp histApp = new HistgramEqualizerApp();
                histApp.setVisible(true);
            }
        });
    }

    static class ImagePanel extends JPanel {
        private BufferedImage image;

        public ImagePanel(BufferedImage image) {
            this.image = image;
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), this);

        }
    }

    static class ButtonPanel extends JPanel {
        private List<JButton> buttons;

        public ButtonPanel(List<JButton> buttons) {
            this.setLayout(new FlowLayout());
            this.buttons = buttons;
            for (JButton button : buttons) {
                this.add(button);
            }
        }
    }

    public static BufferedImage randomImage(int rows, int cols, int pixelRatio) {
        int width = pixelRatio * cols, height = pixelRatio * rows;
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D g = (Graphics2D) img.getGraphics();
        int x, y = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                x = i * pixelRatio;
                y = j * pixelRatio;
                if ((i * j) % 6 == 0) {
                    g.setColor(Color.GRAY);
                } else if ((i + j) % 5 == 0) {
                    g.setColor(Color.BLUE);
                } else {
                    g.setColor(Color.WHITE);
                }
                g.fillRect(y, x, pixelRatio, pixelRatio);
            }
        }
        g.dispose();

        return img;
    }
}