简体   繁体   English

Java Jframe没有编译错误,但是我的JPanel中什么都没有出现

[英]Java Jframe no compilation error, but nothing appears in my JPanel

Hello I am making a project where I have to put 4 buttons using JFrame but nothing inside of my buttons are appearing and I don't understand why because I have no errors and I don't see what can cause the problem. 您好,我正在做一个项目,我必须使用JFrame放置4个按钮,但是按钮内部什么都没有出现,而且我不明白为什么,因为我没有错误,也看不到什么会导致问题。 I have to give it as a project but it's not working here's the code: 我必须将其作为一个项目提供,但是它无法正常工作,这里是代码:

    public class MathoQuest extends JFrame implements ActionListener  {
    JButton boutConvert, boutGeo, boutFonc, boutOut;

    public MathoQuest() {
        setTitle("Bienvenue a MathoQuest");
        setSize(250,500);
        JPanel simplePanel = new JPanel();
        simplePanel.setLayout(null);
        Font helvb14 = new Font("Arial" , Font.BOLD , 30);


        boutConvert = new JButton("Convertir");
        boutConvert.setFont(helvb14);
        boutConvert.setForeground(Color.white);
        boutConvert.setBackground(new Color(63,107,220));
        simplePanel.add(boutConvert);
        boutConvert.setBounds(25,50,200,80);
        boutConvert.addActionListener(this);

        boutGeo = new JButton("Geometrie-\nEN CONSTRUCTION-");
        boutGeo.setFont(helvb14);
        boutConvert.setForeground(Color.white);
        boutGeo.setBackground(new Color(145,110,220));
        simplePanel.add(boutGeo);
        boutGeo.setBounds(25,150,200,80);
        boutGeo.addActionListener(this);

        boutFonc = new JButton("Fonction");
        boutFonc.setFont(helvb14);
        boutFonc.setForeground(Color.white);
        boutFonc.setBackground(new Color(150,200,80));
        simplePanel.add(boutFonc);
        boutFonc.setBounds(25,250,200,80);
        boutFonc.addActionListener(this);

        boutOut = new JButton("Quitter");
        boutOut.setFont(helvb14);
        boutOut.setForeground(Color.white);
        boutOut.setBackground(new Color(245,130,0));
        simplePanel.add(boutOut);
        boutOut.setBounds(25,350,200,80);
        boutOut.addActionListener(this);        


    }
    public static void main(String[] args) {
        MathoQuest mathframe = new MathoQuest();
    mathframe.setVisible(true);
        mathframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }    

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == boutConvert) {
                    ConversionFrame frame = new ConversionFrame();
                    frame.setVisible(true);
        }
        if (e.getSource() == boutGeo) {
             System.out.println("Ce mode est encore en construction merci de reessayer plus tard");
        }
        if (e.getSource() == boutFonc) {
                FonctionFrame dess = new FonctionFrame();
        dess.setVisible(true);
        }
        if (e.getSource() == boutOut)
            System.out.println("Au revoir et merci d'avoir utilise cette application");
            System.exit(0);
    }


}

You have added buttons to JPanel but forgot to add this JPanel to JFrame . 您已经向JPanel添加了按钮,但是忘记了将此JPanel添加到JFrame

Add following line in constructor of the class that extends JFrame 在扩展JFrame的类的constructor中添加以下行

add(simplePanel);

i think you missed to add your JPanel simplePanel to the Frame with this.add(simplePanel). 我认为您错过了使用this.add(simplePanel)将JPanel simplePanel添加到Frame的过程。

Best regards, Daniel 最好的问候,丹尼尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM