Position a JButton properly within a GridBagLayout | Java 8

huangapple go评论84阅读模式
英文:

Position a JButton properly within a GridBagLayout | Java 8

问题

我想将一个名为 "Start"`JButton` 定位在我的GUI的下方中央
不幸的是我设置的 `GridBagConstraints` 似乎没有效果
我正在使用 `jdk1.8.0_191``Eclipse IDE`。

编辑我将我的代码片段改为了一个 "最小可复现示例"
以下是我的代码片段

    package gui.main;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Test {
        
        public static void main(String[] args) {
            
            JFrame frame = new JFrame(); 
            frame.setSize(646, 509);
            frame.setResizable(false);
            frame.setTitle("Adventure Time");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            JPanel panel = new JPanel();
            panel.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            
            gbc.gridx = 1;
            gbc.gridy = 5;
            
            JButton button = new JButton("Start");    
            button.setBackground(Color.black);        
            button.setForeground(Color.white);    
            button.setPreferredSize(new Dimension(110, 60));
            button.setMinimumSize(new Dimension(110, 60));
            button.setVisible(true);
            
            panel.add(button, gbc);
            panel.setVisible(true);
        
            frame.add(panel);
            frame.setVisible(true);
            
        }
    }

这是我希望"Start"按钮位于的大致位置的图片。

Position a JButton properly within a GridBagLayout | Java 8


<details>
<summary>英文:</summary>
I want to position a `JButton` &quot;Start&quot; in the lower center of my GUI.
Sadly the `GridBagConstraints` that I set seem to have no effect. 
I&#39;m working with `jdk1.8.0_191` and the `Eclipse IDE`.
EDIT: I switched my snippet to a &quot;minimal reproducible example&quot;.
Here the snippet of my code:
package gui.main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test{
public static void main(String[] args) {
JFrame frame = new JFrame(); 
frame.setSize(646, 509);
frame.setResizable(false);
frame.setTitle(&quot;Adventure Time&quot;);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 5;
JButton button = new JButton(&quot;Start&quot;);	
button.setBackground(Color.black);		
button.setForeground(Color.white);	
button.setPreferredSize(new Dimension(110, 60));
button.setMinimumSize(new Dimension(110, 60));
button.setVisible(true);
panel.add(button, gbc);
panel.setVisible(true);
frame.add(panel);
frame.setVisible(true);
}
}
Here is a picture where I approximately want the &quot;Start&quot; Button instead.
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/nkby6.jpg
</details>
# 答案1
**得分**: 2
我创建了一个简单的可运行示例,在`JPanel`的下四分之一位置放置了一个启动按钮的GUI。这是示例GUI。
![启动按钮GUI][1]
使用Swing组件。除非您打算覆盖类中的某个方法,否则不要扩展Swing组件或任何其他Java类。
我在`JPanel`中使用了`BorderLayout`。我使用了一个计算出的空边框,将启动按钮从中心向下移动。
在此示例中,我设置了`JPanel`的大小。我假设您将根据图像的大小来设置游戏面板的大小。
以下是代码。
```java
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class StartButtonExample implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new StartButtonExample());
}
@Override
public void run() {
JFrame frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new BorderLayout());
Dimension panelSize = new Dimension(400, 400);
panel.setPreferredSize(panelSize);
JButton button = new JButton("Start");
Dimension buttonSize = button.getPreferredSize();
int height = panelSize.height - buttonSize.height;
int width = panelSize.width - buttonSize.width;
int top = height * 3 / 4;
int left = (width - buttonSize.width) / 2;
int bottom = height / 4;
int right = left;
panel.setBorder(BorderFactory.createEmptyBorder(
top, left, bottom, right));
panel.add(button, BorderLayout.CENTER);
return panel;
}
}
英文:

I created a simple, runnable example of a GUI with a start button on the lower fourth of the JPanel. Here's the example GUI.

Position a JButton properly within a GridBagLayout | Java 8

Use Swing components. Don't extend Swing components, or any other Java class, unless you intend to override one of the methods in the class.

I used a BorderLayout for the JPanel. I used a calculated empty border to shift the start button down from the center.

I set the size of the JPanel in this example. I'm assuming you'll set the size of the game panel based on the size of your image.

Here's the code.

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class StartButtonExample implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new StartButtonExample());
}
@Override
public void run() {
JFrame frame = new JFrame(&quot;Game&quot;);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new BorderLayout());
Dimension panelSize = new Dimension(400, 400);
panel.setPreferredSize(panelSize);
JButton button = new JButton(&quot;Start&quot;);
Dimension buttonSize = button.getPreferredSize();
int height = panelSize.height - buttonSize.height;
int width = panelSize.width - buttonSize.width;
int top = height * 3 / 4;
int left = (width - buttonSize.width) / 2;
int bottom = height / 4;
int right = left;
panel.setBorder(BorderFactory.createEmptyBorder(
top, left, bottom, right));
panel.add(button, BorderLayout.CENTER);
return panel;
}
}

答案2

得分: 1

依我业余的看法:

最终我在我的小应用程序中使用了不同的insets
在我看来,这样看起来更加整洁。

// 上边距 400,其余 3
gbc.insets = new Insets(400,3,3,3);
gbc.gridy = 0;
getWrapperPanel().add(getBtnStart02(), gbc);

// 所有后续 GUI 对象的四周边距均为 3
gbc.insets = new Insets(3,3,3,3);
gbc.gridy = 1;
getWrapperPanel().add(getBtnLoad(), gbc);

运行得很好。

英文:

In my amateurish opinion:

I ended up using different insets for my small application.
It is more clean looking in my opinion.

   //margin 400 top, rest 3
gbc.insets = new Insets(400,3,3,3);
gbc.gridy = 0;
getWrapperPanel().add(getBtnStart02(), gbc);
//margin 3 at all sides for all following gui objects
gbc.insets = new Insets(3,3,3,3);
gbc.gridy = 1;
getWrapperPanel().add(getBtnLoad(), gbc);

Works like a charm.

huangapple
  • 本文由 发表于 2020年8月15日 22:48:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63427308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定