设置网格布局JButton的大小 [英] set JButton size in gridLayout
本文介绍了设置网格布局JButton的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何设置网格布局的JButton的大小?否则,按钮的大小有很大的宽度和它的不听的setSize或的setBounds或设置preferedSize。
addBut =的新的JButton(添加); addBut.addActionListener(本);
deleteBut =的新的JButton(删除);
deleteBut.addActionListener(本);
selectionPanel =新JPanel();
selectionPanel.setLayout(新的GridLayout(2,2));
的TitledBorder selectionBorder =新的TitledBorder(「购股权」);
selectionBorder.setTitleColor(Color.BLUE);
selectionPanel.setBorder(selectionBorder);
selectionPanel.add(新的JLabel(部门名称));
selectionPanel.add(新的JTextField(DEPTNAME));
selectionPanel.add(addBut);
selectionPanel.add(deleteBut); selectionPanel.set preferredSize(新尺寸(900100));
解决方案
我相信设置网格布局(2,2)将覆盖到面板制成大小的变化。为了更precise,使用的GridBagConstraints;
私人的JTextField字段1 =新的JTextField();
私人的JButton addBtn =的新的JButton(拯救); 公共无效addComponents(集装箱窗格){
pane.setLayout(新的GridBagLayout());
GridBagConstraints的C =新的GridBagConstraints();
// 组件
c.gridwidth = 1;
c.weightx = 0.01;
c.weighty = 0.2;
c.gridx = 0;
c.gridy = 1;
pane.add(FIELD1,C); c.gridwidth = 1;
c.weightx = 0.01;
c.weighty = 0.2;
c.gridx = 0;
c.gridy = 1;
pane.add(addBtn,C);
}
公众的MainView(){
//创建和设置窗口。
JFrame的帧=新的JFrame(NAME);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置内容窗格中。
addComponents(frame.getContentPane()); //显示窗口。
frame.pack();
frame.setVisible(真);
frame.setResizable(假);
frame.setSize(400,125);
frame.setLocation(400,300);
}
How to set the size of jbutton in gridlayout? otherwise the button size have lot of width and its not listening setSize or setBounds or setPreferedSize.
addBut = new JButton("Add");
addBut.addActionListener(this);
deleteBut = new JButton("Delete");
deleteBut.addActionListener(this);
selectionPanel = new JPanel();
selectionPanel.setLayout(new GridLayout(2,2));
TitledBorder selectionBorder = new TitledBorder("Options");
selectionBorder.setTitleColor(Color.BLUE);
selectionPanel.setBorder(selectionBorder);
selectionPanel.add(new JLabel("Department Name"));
selectionPanel.add(new JTextField(deptName));
selectionPanel.add(addBut);
selectionPanel.add(deleteBut);
selectionPanel.setPreferredSize(new Dimension(900,100));
解决方案
I believe setting GridLayout(2,2) will override size changes made to the panels. To be more precise, use GridBagConStraints;
private JTextField field1 = new JTextField();
private JButton addBtn = new JButton("Save: ");
public void addComponents(Container pane) {
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// Components
c.gridwidth = 1;
c.weightx = .01;
c.weighty = .2;
c.gridx = 0;
c.gridy = 1;
pane.add(field1, c);
c.gridwidth = 1;
c.weightx = .01;
c.weighty = .2;
c.gridx = 0;
c.gridy = 1;
pane.add(addBtn, c);
}
public MainView() {
//Create and set up the window.
JFrame frame = new JFrame("NAME");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponents(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
frame.setSize(400, 125);
frame.setLocation(400, 300);
}
这篇关于设置网格布局JButton的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文