Java按钮宽度
我试过使用它 ,它不起作用
singleplayerButton.setBounds(20, 20, 200, 100);
我不知道为什么,有人可以帮我解决这个问题吗?
我的完整页面代码在这里
package gmine; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class gmine implements ActionListener { JFrame interfaceFrame; JButton singleplayerButton, multiplayerButton, optionsButton, quitButton; public gmine() { JFrame.setDefaultLookAndFeelDecorated(false); interfaceFrame = new JFrame("G-Mine B0.4"); interfaceFrame.setSize(800,600); interfaceFrame.setLayout(new GridLayout(9,1, 20, 15)); singleplayerButton = new JButton("SinglePLayer"); singleplayerButton.addActionListener(this); interfaceFrame.add(singleplayerButton); singleplayerButton.setBounds(20, 20, 200, 100); multiplayerButton = new JButton("MultiPlayer"); multiplayerButton.addActionListener(this); interfaceFrame.add(multiplayerButton); optionsButton = new JButton("Options"); optionsButton.addActionListener(this); interfaceFrame.add(optionsButton); quitButton = new JButton("Quit"); quitButton.addActionListener(this); interfaceFrame.add(quitButton); interfaceFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); interfaceFrame.setVisible(true); } public void actionPerformed(ActionEvent a) { } public static void main(String[] args) { new gmine(); } }
我试图完成使按钮更小,所以不要触摸页面的一面。
我个人会使用布局管理器,它可以让您更灵活地决定按钮的布局方式,并努力尊重您的组件首选尺寸,但您可以自由地根据需要进行调整…
对我来说,这是GridBagLayout
public class ButtonsLayout { public static void main(String[] args) { new ButtonsLayout(); } public ButtonsLayout() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException ex) { } catch (InstantiationException ex) { } catch (IllegalAccessException ex) { } catch (UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new MenuPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class MenuPane extends JPanel { public MenuPane() { setLayout(new GridBagLayout()); JButton singleplayerButton = new JButton("SinglePLayer"); JButton multiplayerButton = new JButton("MultiPlayer"); JButton optionsButton = new JButton("Options"); JButton quitButton = new JButton("Quit"); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.ipadx = 20; gbc.ipady = 20; add(singleplayerButton, gbc); gbc.gridy++; add(multiplayerButton, gbc); gbc.gridy++; add(optionsButton, gbc); gbc.gridy++; add(quitButton, gbc); } } }
在这里,我使用了GridBagConstraints
ipadx
和ipady
来通过布局管理器增加组件的宽度和高度,以及使用HORIZONTAL
填充使所有组件的宽度相同。
看一下
- 布局管理器的可视指南
- 使用布局管理器
欲获得更多信息
-
首先,当您使用
LayoutManager
, 组件的位置及其大小由放置组件时直接父Compoenent的LayoutManager
控制 。
例如:
将JButton放在JFrame上,然后JFrame的LayoutManager控制JButton的位置和大小。 -
我还建议您使用由NetBeans团队在2005年开发的
GroupLayout
,您可以使用现在从Google免费获得的WindowsBuilderPro
。
GridLayout
管理器将为所有组件提供相同的大小。 您可以尝试使用其他布局管理器,然后在组件上使用setPreferredSize(Dimension preferredSize)
来正确调整它们的大小。
作为一个不同的示例,您还可以将JPanel
放在JFrame
顶部并将按钮放在那里。
我尝试使用上面的一个答案。 现在程序不会显示。 它只是终止。
package gmine; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class gmine { JFrame interfaceFrame; JButton singleplayerButton, multiplayerButton, optionsButton, quitButton; public void ButtonsLayout() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException ex) { } catch (InstantiationException ex) { } catch (IllegalAccessException ex) { } catch (UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame("G-Mine"); interfaceFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); interfaceFrame.setLayout(new BorderLayout()); interfaceFrame.setSize(800,600); interfaceFrame.add(new MenuPane()); interfaceFrame.pack(); interfaceFrame.setLocationRelativeTo(null); interfaceFrame.setVisible(true); } }); } public class MenuPane extends JPanel { public MenuPane() { setLayout(new GridBagLayout()); JButton singleplayerButton = new JButton("SinglePLayer"); JButton multiplayerButton = new JButton("MultiPlayer"); JButton optionsButton = new JButton("Options"); JButton quitButton = new JButton("Quit"); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.ipadx = 20; gbc.ipady = 20; add(singleplayerButton, gbc); gbc.gridy++; add(multiplayerButton, gbc); gbc.gridy++; add(optionsButton, gbc); gbc.gridy++; add(quitButton, gbc); } } public static void main(String[] args) { new gmine(); }
}