java - 在 JPanel 中并排创建两个按钮
问题描述
我正在尝试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);
当我运行代码时,下面是我看到的输出。我不确定为什么第二个按钮没有正确显示。这可能是什么原因?
解决方案
你写作的地方,button.setText("Sort Shapes");
就是button2.setText("Sort Shapes");
它正在更改之前已设置文本的第一个按钮的文本
推荐阅读
- android - 单击 EditText 使设备崩溃
- node.js - 除非全局安装,否则节点包似乎无法在本地工作
- linux - 控制台上的 SSH 谷歌云权限被拒绝(公钥)与 google-cloud-sdk 文件错误
- sql - 加入 ID 和 TIMESTAMP 列时,为什么我会在 LEFT JOIN 中获得额外的行?
- c# - 带有正斜杠的 Json 字段名称
- r - 如何在rgl中绘制到整个窗口?
- kotlin - 我可以将具有反射值的对象变量设置为另一个对象的属性吗?
- html - How can I set the widths of html table columns without individual styling of cells?
- arrays - Can't use string as an ARRAY ref while strict refs in use
- google-apps-script - 如何正确调试部署在谷歌脚本中的 WebApp?