Java GUI:我的面板都没有显示在框架上,但背景颜色和标题是可见的。

huangapple go评论78阅读模式
英文:

Java GUI: None of my panels are showing up on my Frame, but its background color and title are visible?

问题

我正在创建一个基于GUI的计算器程序。我已经将区域分成了几个面板,每个面板有以下用途:

  • inputPanel:用户将输入数字的地方;
  • numericPanel:包含数字 0-9 的面板;
  • operatorPanel:包含运算符号(+、-、*、/)的面板;
  • formatPanel:包含CLEAR、ENTER和EXIT按钮的面板。

在使窗口可见之前,我将所有这些面板添加到了框架中,但当我点击运行按钮时,除了背景颜色外什么都没有显示出来。我是否应该以不同的方式进行格式化?

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Flow;

public class calculator extends Frame implements ActionListener{

    private static JTextField firstField= new JTextField(25);
    private static JTextField num2 = new JTextField(25);

    //------------------------------------------------------------------------------------
    private static JButton b0 = new JButton("0");
    private static JButton b1 = new JButton("1");
    private static JButton b2 = new JButton("2");
    private static JButton b3 = new JButton("3");
    private static JButton b4 = new JButton("4");
    private static JButton b5 = new JButton("5");
    private static JButton b6 = new JButton("6");
    private static JButton b7 = new JButton("7");
    private static JButton b8 = new JButton("8");
    private static JButton b9 = new JButton("9");
    //-----------------------------------------------------------------------------------
    private static JButton enterButton = new JButton("Enter");
    private static JButton clearButton = new JButton("Clear");
    private static JButton exitButton = new JButton("Close");

    private static JButton addB = new JButton("+");
    private static JButton subB = new JButton("-");
    private static JButton multiB = new JButton("x");
    private static JButton divideB = new JButton("÷");

    private static JPanel inputPanel;
    private static JPanel numericPanel;
    private static JPanel operatorPanel;
    private static JPanel formatPanel;

    private static JLabel resultLabel;
    private static JLabel nameLabel = new JLabel("Created by: Yuri Videz");

    //-----------------------------------------------------------------------------------
    public void setInputPanel(JPanel panel) {
        panel.setLayout(new FlowLayout());
        JLabel firstNum = new JLabel("Enter your number:");
        panel.add(firstNum);
        panel.add(firstField);
    }

    public void setNumericPanel(JPanel panel) {
        panel.setLayout(new GridLayout(6, 3));
        panel.add(b0);
        panel.add(b1);
        panel.add(b2);
        panel.add(b3);
        panel.add(b4);
        panel.add(b5);
        panel.add(b6);
        panel.add(b7);
        panel.add(b8);
        panel.add(b9);
    }

    public void setOperatorPanel(JPanel panel) {
        JPanel operatorPanel = new JPanel();
        //operatorPanel.setLayout(new FlowLayout());
        panel.add(addB);
        panel.add(subB);
        panel.add(multiB);
        panel.add(divideB);
    }

    public void setFormatPanel(JPanel panel) {
        JPanel formatPanel = new JPanel();
        panel.setLayout(new FlowLayout());
        panel.add(enterButton);
        panel.add(clearButton);
    }

    public calculator() {
        inputPanel = new JPanel();
        setInputPanel(inputPanel);
        numericPanel = new JPanel();
        setNumericPanel(numericPanel);
        operatorPanel = new JPanel();
        setOperatorPanel(operatorPanel);
        formatPanel = new JPanel();
        setFormatPanel(formatPanel);

        setTitle("Calculator Program");
        JFrame frame = new JFrame();
        frame.getContentPane().setLayout(new GridLayout(3, 3));
        frame.add(nameLabel);
        frame.add(inputPanel);
        frame.add(numericPanel);
        frame.add(operatorPanel);
        frame.add(formatPanel);
        setBackground(Color.gray);
        setSize(350, 500);
        frame.pack();
        setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args){
        calculator calcuObject = new calculator();
    }

    @Override
    public void actionPerformed(ActionEvent ac) {
    }
}
英文:

I am creating a GUI-based Calculator program. I've separated the areas into panels, which have the ff purposes:

  • inputPanel: Where users will input numbers;
  • numericPanel: Panel that holds the digits 0-9
  • operatorPanel: Panel that holds operator symbols(+, -, *, /)
  • formatPanel: Panel that will hold CLEAR, ENTER, and EXIT buttons

I added all these panels to the frame before making it visible, but when I press play, nothing shows up except for the background color. Should I be formatting them a different way?

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Flow;
/**
* @author Graeven Yuri Videz
* This is a program that simulates a simple calculator with arithmetic functions.
*/
public class calculator extends Frame implements ActionListener{
private static JTextField firstField= new JTextField(25);
private static JTextField num2 = new JTextField(25);
//------------------------------------------------------------------------------------
private static JButton b0 = new JButton("0");
private static JButton b1 = new JButton("1");
private static JButton b2 = new JButton("2");
private static JButton b3 = new JButton("3");
private static JButton b4 = new JButton("4");
private static JButton b5 = new JButton("5");
private static JButton b6 = new JButton("6");
private static JButton b7 = new JButton("7");
private static JButton b8 = new JButton("8");
private static JButton b9 = new JButton("9");
//-----------------------------------------------------------------------------------
private static JButton enterButton = new JButton("Enter");
private static JButton clearButton = new JButton("Clear");
private static JButton exitButton = new JButton("Close");
private static JButton addB = new JButton("+");
private static JButton subB = new JButton("-");
private static JButton multiB = new JButton("x");
private static JButton divideB = new JButton("÷");
private static JPanel inputPanel;
private static JPanel numericPanel;
private static JPanel operatorPanel;
private static JPanel formatPanel;
private static JLabel resultLabel;
private static JLabel nameLabel = new JLabel("Created by: Yuri Videz");
//-----------------------------------------------------------------------------------
public void setInputPanel(JPanel panel) {
panel.setLayout(new FlowLayout());
JLabel firstNum = new JLabel("Enter your number:");
panel.add(firstNum);
panel.add(firstField);
}
public void setNumericPanel(JPanel panel) {
panel.setLayout(new GridLayout(6, 3));
panel.add(b0);
panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(b4);
panel.add(b5);
panel.add(b6);
panel.add(b7);
panel.add(b8);
panel.add(b9);
}
public void setOperatorPanel(JPanel panel) {
JPanel operatorPanel = new JPanel();
//operatorPanel.setLayout(new FlowLayout());
panel.add(addB);
panel.add(subB);
panel.add(multiB);
panel.add(divideB);
}
public void setFormatPanel(JPanel panel) {
JPanel formatPanel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(enterButton);
panel.add(clearButton);
}
public calculator() {
inputPanel = new JPanel();
setInputPanel(inputPanel);
numericPanel = new JPanel();
setNumericPanel(numericPanel);
operatorPanel = new JPanel();
setOperatorPanel(operatorPanel);
formatPanel = new JPanel();
setFormatPanel(formatPanel);
setTitle("Calculator Program");
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new GridLayout(3, 3));
frame.add(nameLabel);
frame.add(inputPanel);
frame.add(numericPanel);
frame.add(operatorPanel);
frame.add(formatPanel);
setBackground(Color.gray);
setSize(350, 500);
frame.pack();
setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args){
calculator calcuObject = new calculator();
}
@Override
public void actionPerformed(ActionEvent ac) {
}
}

答案1

得分: 1

你的代码中有两个不同的 JFrame 实例。
你将所有组件添加到 JFrame frame = new JFrame();,但你通过 setVisible(true); 使另一个实例可见。
解决方案:
setVisible(true); 更改为 frame.setVisible(true);
计算器没有必要扩展 JFrame


附注:
1. 请查看[Java命名规范](https://www.geeksforgeeks.org/java-naming-conventions/)。
2. 点击[此链接](https://ide.geeksforgeeks.org/nwtWItC9s5)查看代码的重构版本。

英文:

You have two different JFrame instances in your code.<br/>
You add all components to JFrame frame = new JFrame(); but you make the other one visible by setVisible(true);<br/>
Solution:
change setVisible(true); to frame.setVisible(true);<br/>
There is no need for calculator to extend JFrame.
<hr>
Side notes: <br/>

  1. see Java naming conventions<br/>
  2. Follow this link for a refactored version of your code

huangapple
  • 本文由 发表于 2020年3月16日 20:54:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/60706408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定