gpt4 book ai didi

java - Jframe 中的 JPanel 未显示

转载 作者:行者123 更新时间:2023-12-01 04:58:26 25 4
gpt4 key购买 nike

我正在为计算器做一个简单的布局,实际上我是java新手并正在学习基础知识。我的问题是,当我运行此代码时,仅打开一个 JFrame,并且不显示其他面板及其按钮。请帮忙,我哪里出了问题。

    import java.awt.*;
import javax.swing.*;

public class Layouts extends JFrame{
public Layouts(){
super("Calculator");
setLookAndFeel();
setSize(350,350);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout border = new BorderLayout();
setLayout(border);

GridLayout numbers = new GridLayout(2,2);
row2.setLayout(numbers);
row2.add(one);
row2.add(two);
row2.add(three);
row2.add(four);

GridLayout operators = new GridLayout(2,2);
row3.setLayout(operators);
row3.add(plus);
row3.add(subtract);
row3.add(multiply);
row3.add(equals);
setVisible(true);
}

private void setLookAndFeel()
{
try
{
IManager.setLookAndFeel("com.sun.java.lang.plaf.nimbus.NimbusLookAndFeel");
}
catch(Exception exc)
{
}
}

//row 1
JPanel row1 = new JPanel();
JTextField text = new JTextField(20);

//row 2
JPanel row2 = new JPanel();
JButton one = new JButton("1");
JButton two = new JButton("2");
JButton three = new JButton("3");
JButton four = new JButton("4");

//row3
JPanel row3 = new JPanel();
JButton plus = new JButton("+");
JButton subtract = new JButton("-");
JButton multiply = new JButton("*");
JButton equals = new JButton("=");

public static void main(String[] args)
{
Layouts l1 = new Layouts();
}

}

最佳答案

记住添加所有组件(即 row2、row3 等...)

示例:

add(row2,BorderLayout.CENTER)
add(row3,BorderLayout.SOUTH)

关于java - Jframe 中的 JPanel 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13730840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com