java – 为什么这个GridBagLayout没有按计划出现?
发布时间:2020-12-15 04:24:02 所属栏目:Java 来源:网络整理
导读:我试图达到 Setting an arbitrary width in GridBagLayout所要求的最终结果. 为方便参考,这里是: 这是目前的结果: 按钮编号和行以1,1的形式显示,后跟为此单元格声明的列数(2). 如您所见,它从按钮1,1(3)开始,在它下面1,2(4)宽度相同,同时声明不同数量的列.
我试图达到
Setting an arbitrary width in GridBagLayout所要求的最终结果.
为方便参考,这里是: 这是目前的结果: 按钮编号和行以1,1的形式显示,后跟为此单元格声明的列数(2). 如您所见,它从按钮1,1(3)开始,在它下面1,2(4)宽度相同,同时声明不同数量的列. 任何人都可以确定如何更正代码? 目前的代码: import java.awt.*; import javax.swing.*; import javax.swing.border.EmptyBorder; public class KeyBoardLayout { private JComponent ui = null; KeyBoardLayout() { initUI(); } public void initUI() { if (ui!=null) return; ui = new JPanel(new GridBagLayout()); ui.setBorder(new EmptyBorder(4,4,4)); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 3; gbc.fill = GridBagConstraints.HORIZONTAL; ui.add(new JButton("1,1 (3)"),gbc); gbc.gridx = 3; gbc.gridwidth = 2; ui.add(new JButton("2,1 (2)"),gbc); gbc.gridx = 5; ui.add(new JButton("3,gbc); gbc.gridx = 7; ui.add(new JButton("4,gbc); gbc.gridx = 9; ui.add(new JButton("5,gbc); gbc.gridx = 11; ui.add(new JButton("6,gbc); gbc.gridx = 13; ui.add(new JButton("7,gbc); gbc.gridx = 15; gbc.gridwidth = 3; ui.add(new JButton("8,gbc); gbc.gridx = 18; gbc.gridwidth = 4; ui.add(new JButton("9,1 (4)"),gbc); gbc.gridx = 0; gbc.gridy = 1; ui.add(new JButton("1,2 (4)"),gbc); gbc.gridx = 4; gbc.gridwidth = 2; ui.add(new JButton("2,2 (2)"),gbc); gbc.gridx = 6; ui.add(new JButton("3,gbc); gbc.gridx = 8; ui.add(new JButton("4,gbc); gbc.gridx = 10; ui.add(new JButton("5,gbc); gbc.gridx = 12; ui.add(new JButton("6,gbc); gbc.gridx = 14; ui.add(new JButton("7,gbc); gbc.gridx = 16; ui.add(new JButton("8,gbc); gbc.gridx = 0; gbc.gridy = 2; gbc.gridwidth = 5; ui.add(new JButton("1,3 (5)"),gbc); gbc.gridx = 5; gbc.gridwidth = 2; ui.add(new JButton("2,3 (2)"),gbc); gbc.gridx = 7; ui.add(new JButton("3,gbc); gbc.gridx = 9; ui.add(new JButton("4,gbc); gbc.gridx = 11; ui.add(new JButton("5,gbc); gbc.gridx = 13; ui.add(new JButton("6,gbc); gbc.gridx = 15; ui.add(new JButton("7,gbc); gbc.gridx = 17; ui.add(new JButton("8,gbc); gbc.gridx = 19; gbc.gridwidth = 3; ui.add(new JButton("9,3 (3)"),gbc); gbc.gridx = 0; gbc.gridy = 3; gbc.gridwidth = 3; ui.add(new JButton("1,4 (3)"),gbc); gbc.gridx = 3; ui.add(new JButton("2,gbc); gbc.gridx = 6; gbc.gridwidth = 10; ui.add(new JButton("3,4 (10)"),gbc); gbc.gridx = 16; gbc.gridwidth = 3; ui.add(new JButton("4,gbc); gbc.gridx = 19; ui.add(new JButton("5,gbc); } public JComponent getUI() { return ui; } public static void main(String[] args) { Runnable r = new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception useDefault) { } KeyBoardLayout o = new KeyBoardLayout(); JFrame f = new JFrame("Keyboard Layout"); f.setDefaultCloSEOperation(JFrame.DISPOSE_ON_CLOSE); f.setLocationByPlatform(true); f.setContentPane(o.getUI()); f.pack(); f.setMinimumSize(f.getSize()); f.setVisible(true); } }; SwingUtilities.invokeLater(r); } } 解决方法
认为我得到了它的工作.我创建了一个包含22个组件的行,每个组件占用一列:
import java.awt.*; import javax.swing.*; import javax.swing.border.EmptyBorder; public class KeyBoardLayout { private JComponent ui = null; KeyBoardLayout() { initUI(); } public void initUI() { if (ui!=null) return; ui = new JPanel(new GridBagLayout()); ui.setBorder(new EmptyBorder(4,4)); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; // gbc.gridy = 0; gbc.gridy = 1; gbc.gridwidth = 3; gbc.fill = GridBagConstraints.HORIZONTAL; ui.add(new JButton("1,gbc); gbc.gridx = 0; // gbc.gridy = 1; gbc.gridy = 2; ui.add(new JButton("1,gbc); gbc.gridx = 0; // gbc.gridy = 2; gbc.gridy = 3; gbc.gridwidth = 5; ui.add(new JButton("1,gbc); gbc.gridx = 0; // gbc.gridy = 3; gbc.gridy = 4; gbc.gridwidth = 3; ui.add(new JButton("1,gbc); gbc.gridx = 0; gbc.gridy = 4; gbc.gridwidth = 1; for (int i = 0; i < 22; i++) { gbc.gridx = i; gbc.gridy = 4; ui.add(new JButton()); // ui.add(Box.createHorizontalStrut(15)); } } public JComponent getUI() { return ui; } public static void main(String[] args) { Runnable r = new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception useDefault) { } KeyBoardLayout o = new KeyBoardLayout(); JFrame f = new JFrame("Keyboard Layout"); f.setDefaultCloSEOperation(JFrame.DISPOSE_ON_CLOSE); f.setLocationByPlatform(true); f.setContentPane(o.getUI()); f.pack(); f.setMinimumSize(f.getSize()); f.setVisible(true); } }; SwingUtilities.invokeLater(r); } } 当然你不希望按钮显示,所以你也可以使用不可见的组件. 现在,我有一个自己的问题.为什么按钮出现在第一行,即使我设置gridy = 4? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |