【发布时间】:2017-01-31 17:16:23
【问题描述】:
我正在尝试构建一个如下所示的 GUI:
主要思想是在侧面有按钮和显示结果的表格。
这是我目前的代码:
public class GUI {
private JButton usdJpyButton = new JButton("USD/JPY");
private JButton usdGbpButton = new JButton("USD/GBP");
public void mainScreen(){
JFrame frame = new JFrame("Window");
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.setSize(900,600);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
GridLayout layout = new GridLayout(0,4);
frame.setLayout(layout);
JPanel sidePanel = new JPanel(new GridBagLayout());
sidePanel.setBackground(Color.black);
GridBagConstraints cons = new GridBagConstraints();
cons.insets = new Insets(50,0,0,0);
cons.gridy = 1;
cons.anchor = GridBagConstraints.NORTH;
sidePanel.add(usdJpyButton);
cons.gridy = 2;
sidePanel.add(usdGbpButton);
frame.add(sidePanel);
frame.setVisible(true);
}
按钮未对齐,我不确定应该使用哪个布局管理器以获得最佳效果。此外,每个按钮都会有一个针对不同表的动作侦听器,所以我需要为每个表创建单独的 JPanel 吗??
【问题讨论】:
-
使用您在构建
JPanel时指定的GridBagConstraints,例如sidePanel.add(usdJpyButton, cons);
标签: java swing user-interface layout layout-manager