【问题标题】:Creating two buttons in JPanel side by side在 JPanel 中并排创建两个按钮
【发布时间】:2021-11-29 09:27:10
【问题描述】:

我正在尝试在JPanel 中并排制作两个按钮。到目前为止,我有以下代码:

JFrame frame = new JFrame("Display Shapes");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JButton button = new JButton();
button.setBounds(50,100,80,30);

button.setText("Load Shapes");
panel.add(button);
JButton button2 = new JButton();
button2.setBounds(100,100,80,30);

button.setText("Sort Shapes");
panel.add(button2);

frame.add(panel);
frame.setSize(600, 600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

当我运行代码时,下面是我看到的输出。我不确定为什么第二个按钮没有正确显示。这可能是什么原因?

【问题讨论】:

  • javaswing 标签与您最后提出的两个问题相关。请务必添加相关标签,以便更多人看到问题。

标签: java swing jframe jpanel jbutton


【解决方案1】:

你写button.setText("Sort Shapes");的地方,就是button2.setText("Sort Shapes");

它正在更改之前已设置文本的第一个按钮的文本

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 2013-09-26
    • 2011-11-07
    • 2013-03-05
    相关资源
    最近更新 更多