簡體   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