【问题标题】:Java Jframe no compilation error, but nothing appears in my JPanelJava Jframe 没有编译错误,但我的 JPanel 中没有出现任何内容
【发布时间】:2018-05-03 21:32:11
【问题描述】:

您好,我正在制作一个项目,我必须使用 JFrame 放置 4 个按钮,但我的按钮内部没有出现任何内容,我不明白为什么,因为我没有错误,而且我看不出是什么导致了问题。我必须把它作为一个项目提供,但它不起作用这里的代码:

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


}

【问题讨论】:

  • 将面板simplePanel添加到框架中
  • 对不起,我在哪里添加它
  • 刚试过:JFrame f = new JFrame(); f.add(simplePanel);没用
  • 添加(simplePanel);在 JPanel 之后 simplePanel = new JPanel(); MathoQuest 构造函数中的行。

标签: java swing jframe jpanel actionevent


【解决方案1】:

我认为您错过了使用 this.add(simplePanel) 将 JPanel simplePanel 添加到框架中。

最好的问候, 丹尼尔

【讨论】:

    【解决方案2】:

    您已将按钮添加到JPanel,但忘记将此JPanel 添加到JFrame

    在扩展JFrame的类的constructor中添加以下行

    add(simplePanel);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-29
      • 1970-01-01
      相关资源
      最近更新 更多