【问题标题】:Java JFrame: Windows Layout & EmbedJava JFrame:Windows 布局和嵌入
【发布时间】:2021-07-24 05:09:35
【问题描述】:

我需要为 Jframe Window 定义一个布局,如上图所示。

以下是我的方法。 来自我的资源文件夹 (/resources/...jpg) 的图片嵌入在中间 (main) 中。 Top、Bottom、Left、Right 分为四个部分,而它们的内容是一个被拉伸的带标签的按钮,所以我可以稍后在上面映射一些方法,改变主容器内的图片。

我试图显示图片,但我得到了您在我的屏幕截图中看到的结果。我在主容器中看不到它,也没有收到错误消息。

我不知道这是否是因为我使用 JFrame 的方法错误。 下面你可以看到我的代码,如果你也能帮助我解决我错误的设计布局模式,我会很高兴。

MyFrame.java

package ms0.gui;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.*;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class MyFrame extends JFrame {
    public MyFrame () {
        setTitle("This is an example title");
        setSize(600,600);
        setLocation(750,640);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        
    
        
        
        //Main Container
        Container mainContainer = this.getContentPane();
        mainContainer.setLayout(new BorderLayout(8,6));
        mainContainer.setBackground(Color.BLUE);
        this.getRootPane().setBorder(BorderFactory.createMatteBorder(4, 4, 4, 4, Color.green));
        
        
        //JButton Positions
        JButton topButton = new JButton("Oben");
        JButton bottomButton = new JButton("Unten");
        JButton leftButton = new JButton("Links");
        JButton rightButton = new JButton("Rechts");

        
        //Panel Top
        
        JPanel topPanel = new JPanel();
        topPanel.setBorder(new LineBorder(Color.BLACK, 3));
        topPanel.setBackground(Color.ORANGE);
        topPanel.setLayout(new FlowLayout(5));
        topPanel.add(topButton);
        mainContainer.add(topPanel, BorderLayout.NORTH);
        
        //Panel Middle
        
        JPanel middlePanel = new JPanel();
        middlePanel.setBorder(new LineBorder(Color.black, 3));
        middlePanel.setLayout(new FlowLayout(4,4,4));
        middlePanel.setBackground(Color.cyan);
        
        
        //Grid Panel Right
        JPanel rightPanel = new JPanel();
        rightPanel.setLayout(new FlowLayout(4,4,4));
        rightPanel.setBorder(new LineBorder(Color.black, 3));
        rightPanel.setBackground(Color.GREEN);
        rightPanel.add(rightButton);
        
        
        //Grid Panel Left
        
        JPanel gridPanel = new JPanel();
        gridPanel.setLayout(new GridLayout(4,1,5,5));
        gridPanel.setBorder(new LineBorder(Color.black, 3));
        gridPanel.setBackground(Color.red);
        gridPanel.add(leftButton);
        
        
        
        //Center Box
        JLabel label = new JLabel("Center Box", SwingConstants.CENTER);
        label.setOpaque(true);
        label.setBorder(new LineBorder(Color.black,3));
        
        middlePanel.add(gridPanel);
        mainContainer.add(label);
        mainContainer.add(middlePanel, BorderLayout.WEST);
        mainContainer.add(rightPanel, BorderLayout.EAST);

        
        //Panel Bottom
        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new FlowLayout(3));
        bottomPanel.add(bottomButton);
        bottomPanel.setBackground(Color.magenta);
        bottomPanel.setBorder(new LineBorder(Color.BLUE, 3));
        mainContainer.add(bottomPanel, BorderLayout.SOUTH);
        
    //Siegel
    
        
        
            String filepath = "/resources/siegel.jpg";      
            int picWidth = 150;
            int picHeight = 150;
            
            ImageIcon image1 = new ImageIcon(getClass().getResource(filepath));
            //Image scaledImage = img.getScaledInstance(picWidth, picHeight, Image.SCALE_DEFAULT);
            //ImageIcon icon = new ImageIcon(scaledImage);
            
            mainContainer.add(new JButton(image1));
    
    
    }
}

【问题讨论】:

    标签: java swing jpanel jlabel layout-manager


    【解决方案1】:

    所以,作为一个非常基本的例子,除了BorderLayout

    import java.awt.BorderLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingUtilities;
    import javax.swing.border.EmptyBorder;
    
    public class MyFrame extends JFrame {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    MyFrame frame = new MyFrame();
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public MyFrame() {
            setTitle("This is an example title");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    
            setLayout(new BorderLayout());
    
            add(new JButton("Top button (stretched)"), BorderLayout.NORTH);
            add(new JButton("Left button (stretched)"), BorderLayout.WEST);
            add(new JButton("Right button (stretched)"), BorderLayout.EAST);
            add(new JButton("Bottom button (stretched)"), BorderLayout.SOUTH);
    
            JLabel label = new JLabel("Picture");
            label.setBorder(new EmptyBorder(100, 100, 100, 100));
    
            add(label);
        }
    }
    

    记住,简单往往是最好的。

    现在,如果您绝对肯定必须将标签/图片放在另一个容器中,您可以简单地使用GridBagLayout,因为它默认将子组件居中,例如...

    JLabel label = new JLabel("Picture");
    label.setBorder(new EmptyBorder(100, 100, 100, 100));
    
    // Automatic center position
    JPanel mainPane = new JPanel(new GridBagLayout());
    mainPane.add(label);
    
    add(mainPane);
    

    而且您不必使用EmptyBorderGridBagLayout 将允许提供 insets 这将做同样的事情

    【讨论】:

    • 工作完美!但是我现在如何从主容器内的资源文件夹中嵌入自定义大小的图像?我想为按钮创建方法,以便我可以更改有关单击按钮的显示图像的方向
    • 这取决于。 JLabel 可以显示图片 - 所以你可以直接使用它,或者你需要派生一个自定义组件并通过自定义绘画自己绘制图像
    猜你喜欢
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    相关资源
    最近更新 更多