简体   繁体   English

如何调整按钮大小并将它们移动到不同的位置?

[英]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:您可以对代码进行一些改进:

  1. 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); }
  2. 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.以获得更深入的解释。

  3. 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) ,否则您会遇到与不可见组件相关的奇怪错误,该行应该是代码中的最后一行。

  4. 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.

相关问题 我如何将星星移到它所属的地方 - How do I move the star into the place it belongs 如何渲染不同大小的图块? - How do I Render Tiles at different sizes? 如何为不同的屏幕尺寸使用不同的操作栏尺寸? - How do I do to use different action bar sizes for different screen sizes? 如何使底部 Sheet 在不同的屏幕尺寸上调整其大小? - How to make the bottom Sheet adjust its size on different screen sizes? 如何在键盘应用程序中调整按钮大小? - How do I adjust Button size in Keyboard application? 我如何将前导辅音移动到字符串的末尾? - How do I take the leading consonants an move them to the end of the string? 如何在JFrame和JPanel中调整大小? - How to adjust the sizes in JFrame and JPanel? 如何按月份过滤目录中的文件,zip 它们基于月份,重命名它们,将它们放在包含 zipfiles 的文件夹中? - How do I filter files in a directory by month, zip them based on month, rename them, place them in a folder that contains the zipfiles? 如何使用jsp中的按钮移至数据库中的下一条记录 - How do I move to next record in the database using a button in jsp 如何绘制Java中以不同速度移动的多个objetc? - How do I paint multiple objetcs that move at different speeds in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM