[英]How do I adjust the button sizes and move them to be in a different place?
I'm trying to get some of the buttons to be bigger and be able to move them around to get them more to what my professor wants them to be but I'm not sure how to do it.我试图让一些按钮变得更大,并能够移动它们以使它们更符合我的教授希望它们成为的样子,但我不知道该怎么做。
I decided to use a GridBagLayout
but my professor never talked about it so I'm not sure if I'm missing anything or how exactly it works.我决定使用
GridBagLayout
但我的教授从未谈论过它,所以我不确定我是否遗漏了任何东西或者它是如何工作的。
The image is what he wants us to get it too.图像也是他希望我们得到的。 Exactly like this.
正是这样。
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GUI {
public static JPanel buttonPanel;
private static JFrame frame;
public static void main(String[] args) {
frame = new JFrame("Layout Question");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
frame.add(mainPanel);
buttonPanel = new JPanel();
mainPanel.add(buttonPanel);
buttonPanel.add(new JButton("hi"));
buttonPanel.add(new JButton("long name"));
buttonPanel.add(new JButton("bye"));
buttonPanel.add(new JButton("1"));
buttonPanel.add(new JButton("2"));
buttonPanel.add(new JButton("3"));
buttonPanel.add(new JButton("4"));
buttonPanel.add(new JButton("5"));
buttonPanel.add(new JButton("6"));
buttonPanel.add(new JButton("7"));
buttonPanel.add(new JButton("Cancel"));
}
}
There are some improvements you can do to your code:您可以对代码进行一些改进:
Don't use static
variables, and place your program on the EDT, an easy way to do this is:不要使用
static
变量,并将程序放在 EDT 上,一个简单的方法是:
public static void main(String[] args) { SwingUtilities.invokeLater(new LayoutManagersExample()::createAndShowGUI); }
Don't call setSize(...)
on your JFrame
, it's going to make your window smaller than you think, it's taking the frame decorations into the calculation for the size, instead call frame.pack()
, or override the getPreferredSize()
of your JPanel
, see Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?不要在您的
JFrame
上调用setSize(...)
,这会使您的 window 比您想象的要小,它会将框架装饰纳入尺寸的计算中,而不是调用frame.pack()
或覆盖getPreferredSize()
的JPanel
,请参阅我应该避免在 Java Swing 中使用 set(Preferred|Maximum|Minimum)Size 方法吗? for a more in-depth explanation.以获得更深入的解释。
Don't call frame.setVisible(true)
before you've added all your components to your JFrame
, otherwise you'll get strange bugs related to invisible components, that line should be the last one on your code.在将所有组件添加到
JFrame
之前不要调用frame.setVisible(true)
,否则您会遇到与不可见组件相关的奇怪错误,该行应该是代码中的最后一行。
Divide and conquer, you can use multiple JPanels
with different Layout Managers , and you can combine them and join them together later on.分而治之,您可以将多个
JPanels
与不同的Layout Manager一起使用,您可以将它们组合起来,稍后再将它们连接在一起。
One possible approach (which isn't exactly as your teacher wants it to be but is close enough) is this one:一种可能的方法(这与您的老师想要的不完全一样,但足够接近)是这样的:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class LayoutManagersExample {
private JFrame frame;
private JPanel pane;
public static void main(String[] args) {
SwingUtilities.invokeLater(new LayoutManagersExample()::createAndShowGUI);
}
private void createAndShowGUI() {
frame = new JFrame("Layout Question");
pane = new JPanel();
JPanel topPanel = getTopPanel();
JPanel boxesPanel = getBoxesPanel();
JPanel buttonsPanel = getButtonsPanel();
pane.add(boxesPanel);
pane.add(buttonsPanel);
frame.add(pane);
frame.add(topPanel, BorderLayout.NORTH);
frame.add(new JButton("Cancel"), BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setSize(500, 500);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JPanel getButtonsPanel() {
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new GridLayout(2, 2));
buttonsPanel.add(new JButton("1"));
buttonsPanel.add(new JButton("2"));
buttonsPanel.add(getInnerButtonsPanel());
buttonsPanel.add(new JButton("7"));
return buttonsPanel;
}
private JPanel getInnerButtonsPanel() {
JPanel innerButtonsPanel = new JPanel();
innerButtonsPanel.setLayout(new GridLayout(2, 2));
innerButtonsPanel.add(new JButton("3"));
innerButtonsPanel.add(new JButton("4"));
innerButtonsPanel.add(new JButton("5"));
innerButtonsPanel.add(new JButton("6"));
return innerButtonsPanel;
}
private JPanel getBoxesPanel() {
JPanel boxesPanel = new JPanel();
boxesPanel.setLayout(new BoxLayout(boxesPanel, BoxLayout.PAGE_AXIS));
boxesPanel.add(new JCheckBox("Bold"));
boxesPanel.add(new JCheckBox("Italic"));
boxesPanel.add(new JCheckBox("Underline"));
boxesPanel.add(new JCheckBox("Strikeout"));
return boxesPanel;
}
private JPanel getTopPanel() {
JPanel topPanel = new JPanel();
JPanel topButtonsPanel = new JPanel();
topButtonsPanel.setLayout(new GridLayout());
topButtonsPanel.add(new JButton("hi"));
topButtonsPanel.add(new JButton("long name"));
topButtonsPanel.add(new JButton("bye"));
topPanel.add(new JLabel("Buttons: "));
topPanel.add(topButtonsPanel);
return topPanel;
}
}
Play around with the code, and try to find a different approach by combining the layouts, divide each piece of the window in your head and see how to apply a different layout manager to each of them, divide them in methods as I did to make things easier to follow.玩弄代码,并尝试通过组合布局找到不同的方法,在你的脑海中划分 window 的每一块,看看如何对它们中的每一个应用不同的布局管理器,按照我所做的那样将它们划分为方法事情更容易理解。
Find a way to left align the elements in the JCheckBoxes
for example, and other things找到一种方法来左对齐
JCheckBoxes
中的元素,例如,和其他东西
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.