繁体   English   中英

为什么我不能在我的 GridLayout JPanel 中添加按钮?

[英]Why can't I add buttons to my GridLayout JPanel?

我正在尝试将 24 个JButtons添加到我的JPanel buttonPanelGridLayout中,但是当我运行它时,我发现没有添加任何按钮。 (至少,它们是不可见的。)。 我尝试给buttonPanel一个背景颜色,它是可见的。 有谁知道我做错了什么?

这是我的代码(还有其他类):

package com.Egg;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;



public class NormalCalc implements ActionListener {
    static JPanel normalCalcPanel = new JPanel();
    JPanel buttonPanel = new JPanel(new GridLayout(6,4,10,10));
    Font font = new Font("Monospaced Bold", Font.BOLD, 18);
    
    JButton powB = new JButton("^");
    JButton sqrtB = new JButton("sqrt(");
    JButton hOpenB = new JButton("(");
    JButton hCloseB = new JButton(")");
    JButton delB = new JButton("DEL");
    JButton acB = new JButton("AC");
    JButton mulB = new JButton("*");
    JButton divB = new JButton("/");
    JButton addB = new JButton("+"); 
    JButton minB = new JButton("-");
    JButton decB = new JButton(".");
    JButton equB = new JButton("=");
    JButton negB = new JButton("(-)");
    
    JButton[] numberButtons = new JButton[10];
    JButton[] functionButtons = new JButton[13];

    
    
    public NormalCalc() {
        functionButtons[0] = powB;
        functionButtons[1] = delB;
        functionButtons[2] = acB;
        functionButtons[3] = sqrtB;
        functionButtons[4] = mulB;
        functionButtons[5] = divB;
        functionButtons[6] = hOpenB;
        functionButtons[7] = addB;
        functionButtons[8] = minB;
        functionButtons[9] = hCloseB;
        functionButtons[10] = decB;
        functionButtons[11] = negB;
        functionButtons[12] = equB;
        
        
        for (int i=0; i<10; i++) {
            numberButtons[i] = new JButton(String.valueOf(i));
            numberButtons[i].setFocusable(false);
            numberButtons[i].setFont(font);
            numberButtons[i].addActionListener(this);
        }
        for (int i=0; i <13; i++) {
            functionButtons[i].setFocusable(false);
            functionButtons[i].setFont(font);
            functionButtons[i].addActionListener(this);
        }
        normalCalcPanel.setBounds(0, 0, 500, 700);
        
        buttonPanel.setBounds(50, 200, 400, 400);
        

        // Adding the buttons;
        buttonPanel.add(functionButtons[0]);
        buttonPanel.add(numberButtons[7]);
        buttonPanel.add(numberButtons[8]);
        buttonPanel.add(numberButtons[9]);
        buttonPanel.add(functionButtons[1]);
        buttonPanel.add(functionButtons[2]);
        buttonPanel.add(functionButtons[3]);
        buttonPanel.add(numberButtons[4]);
        buttonPanel.add(numberButtons[5]);
        buttonPanel.add(numberButtons[6]);
        buttonPanel.add(functionButtons[4]);
        buttonPanel.add(functionButtons[5]);
        buttonPanel.add(functionButtons[6]);
        buttonPanel.add(numberButtons[1]);
        buttonPanel.add(numberButtons[2]);
        buttonPanel.add(numberButtons[3]);
        buttonPanel.add(functionButtons[7]);
        buttonPanel.add(functionButtons[8]);
        buttonPanel.add(functionButtons[9]);
        buttonPanel.add(numberButtons[0]);
        buttonPanel.add(functionButtons[10]);
        buttonPanel.add(functionButtons[11]);
        buttonPanel.add(functionButtons[12]);
        
        buttonPanel.repaint();
        
        normalCalcPanel.add(buttonPanel);
        normalCalcPanel.add(MainMath.exitButton);
        
        MainMath.frame.add(normalCalcPanel);
        MainMath.frame.repaint();
    }



    @Override
    public void actionPerformed(ActionEvent e) {
        
        
    }
}

其他(主)class:

package com.Egg;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class MainMath implements ActionListener {

    static JFrame frame = new JFrame("Math Tools");
    static JButton exitButton = new JButton("Exit");

    JPanel mainPanel = new JPanel();
    JLabel mainLabel = new JLabel("Select your tool.", SwingConstants.CENTER);


    JButton nB = new JButton("Normal Calc.");
    JButton tB = new JButton("Right Triangle Calc.");
    JButton eB = new JButton("Equations Calc.");



public MainMath() { 
    frame.setBounds(300, 300, 500, 700);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(null);
    frame.setResizable(false);
    
    mainPanel.setLayout(null);
    mainPanel.setBounds(0, 0, 500, 700);
    
    mainLabel.setBounds(100, 25, 300, 50);
    mainLabel.setFont(new Font("Monospaced Bold", Font.BOLD, 18));
    
    nB.setBounds(100, 100, 300, 40);
    tB.setBounds(100, 150, 300, 40);
    eB.setBounds(100, 200, 300, 40);
    nB.setFocusable(false);
    tB.setFocusable(false);
    eB.setFocusable(false);
    nB.addActionListener(this);
    tB.addActionListener(this);
    eB.addActionListener(this);
    
    mainPanel.add(mainLabel);
    mainPanel.add(nB);
    mainPanel.add(tB);
    mainPanel.add(eB);
    frame.add(mainPanel);
    frame.setVisible(true);
    
    exitButton.setBounds(16, 10, 80, 35);
    exitButton.setFocusable(false);
    exitButton.addActionListener(this);
    
}
public static void main(String[] args) {
    new MainMath();
    
    System.out.println("");
    
}

public void setMainScreen() {
    frame.remove(exitButton);
    
    frame.remove(NormalCalc.normalCalcPanel);
    frame.add(mainPanel);
    frame.repaint();
}

public static JFrame getFrame() {
    return frame;
}





@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == exitButton) 
        setMainScreen();
    
    if (e.getSource() == nB) {
        frame.remove(mainPanel);
        new NormalCalc();
    }
    if (e.getSource() == tB)
        new TriangleCalc();
    if (e.getSource() == eB)
        new EquationCalc();
}

}

我在您的代码中看到多个问题:

  1. 乍一看,你的按钮排列很奇怪。

  2. normalCalcPanelstatic ,为什么? 永远不应该

  3. setBounds(...)使用布局管理器时,这些语句被忽略,因此不需要它们

  4. repaint()除非您在显示 GUI 之后添加/删除了任何元素,否则这些调用是不必要的,并且应该与revalidate()一起使用。

  5. MainMath.exitButton另一个static组件,STOP

  6. MainMath.frame.add(normalCalcPanel)意味着您的JFrame称为frame也是static ,这又不应该是

而且(我会在这里猜)很可能您正在MainMath中创建第二个实例,但是因为我们没有来自该 class 的代码,所以我们不知道发生了什么

我运行了你的代码,它看起来不错,所以我相信你的MainMath正在创建JFrame的另一个实例

在此处输入图像描述

这是一个计算器安排的示例,它应该可以帮助您以类似的方式构建代码,而不是 GUI,而是组件和类


编辑

好的,我现在明白我不应该使用 static 的东西,但是如何将面板添加到我在另一个 class 中创建的框架中? 我使用“静态”,因为它似乎有效

让我们对这种情况进行类比,假设您的JFrame是一本书,而您的JPanel是那本书的表,当您在这些表上书写时,您不会将书添加/粘贴到表中,而是添加书的床单。

因此,在这种情况下,它是相同的,您的主要 class 应该创建JPanels并将它们添加到您的JFrame ,您的JPanel类不应该知道您的JFrame

就书而言,您的工作表不需要知道属于哪本书,您知道这一点,并且该书知道哪些工作表属于它,反之亦然。

我举了一个例子来向你展示我的意思,我没有重新创建你的 GUI,而是尽可能简单地让你明白这个想法:

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class AnotherCalculator {
    private JFrame frame;
    private CalculatorPanel calculatorPanel;
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new AnotherCalculator()::createAndShowGUI);
    }
    
    private void createAndShowGUI() {
        frame = new JFrame(getClass().getSimpleName());
        calculatorPanel = new CalculatorPanel();
        
        frame.add(calculatorPanel);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

class CalculatorPanel extends JPanel {
    private String[] operations = {"+", "-", "*", "/", "DEL", "AC", "(", ")", "-", "^", "SQRT", ".", "+/-", "="};
    private static final int NUMBER_OF_DIGITS = 10;
    
    private JButton[] buttons;
    
    public CalculatorPanel() {
        this.setLayout(new GridLayout(0, 4, 5, 5));
        
        buttons = new JButton[operations.length + NUMBER_OF_DIGITS];
        for (int i = 0; i < buttons.length; i++) {
            if (i < NUMBER_OF_DIGITS) {
                buttons[i] = new JButton(String.valueOf(i));
            } else {
                buttons[i] = new JButton(operations[i - NUMBER_OF_DIGITS]);
            }
            this.add(buttons[i]);
        }
    }
}

在此处输入图像描述

暂无
暂无

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

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