问题
I've tried to use this and it doesn't work
singleplayerButton.setBounds(20, 20, 200, 100);
I dont know why though, can anybody help me out with this?
My full page code is here
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();
}
}
im trying to accomplish making the buttons smaller, so not touching the side of the page.
回答1:
I personally would use a layout manager that will give you more flexibility in deciding how the buttons are laid out and makes a great effort to honor your components preferred size, but gives you the freedom to make adjustments to as you need...
For me, that's 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);
}
}
}
Here, I've used ipadx
and ipady
of the GridBagConstraints
to increase the width and height of the components through the layout manager, as well as using the HORIZONTAL
fill to make all the components the same width.
Have a look at
- A Visual Guide to Layout Managers
- Using Layout Managers
For more information
回答2:
First of all when you use
LayoutManager
, then the position of the component and its size is controlled by theLayoutManager
of the direct Parent Compoenent on when the Component is being placed..
Eg:
Placing a JButton on JFrame, then JFrame's LayoutManager controls the position and size of the JButton.I would also recommend you to use
GroupLayout
which was developed by NetBeans team in 2005, and you can use the WindowsBuilderPro, now free from Google.
回答3:
The GridLayout
manager will give equal size to all components. You could try using a different layout manager and then use setPreferredSize(Dimension preferredSize)
on your components to size them correctly.
As a different example, you could also place a JPanel
on top of your JFrame
and place the buttons there.
回答4:
I tried to use one of the answers above. Now the program wont show. it just terminates.
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();
}
}
来源:https://stackoverflow.com/questions/13136701/java-button-width