java - 图像的永久位置?

标签 java swing user-interface jpanel jlabel

我的工作有问题。 我想要发生的是我希望将我的图像放在特定或永久的位置。 但每次我调整窗口大小时,图像也会改变其位置。当我使用 .setResizable(false) 时,我看不到文本和图像。

import java.awt.*;
import javax.swing.*;

public class ProgDraft extends JFrame {
private ImageIcon image1;
private JLabel label1;

ProgDraft() {

    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout());

    ImageIcon pics = new ImageIcon(getClass().getResource("l.png"));

    JLabel logo = new JLabel(pics);
    panel.setBounds(100,100,100,100);
    panel.add(logo);


    JLabel title = new JLabel("Testing Title");
    panel.add(title);

    getContentPane().setLayout(new BorderLayout());


    getContentPane().add(panel);
    getContentPane().add(logo, BorderLayout.CENTER);
    getContentPane().add(title, BorderLayout.NORTH);
}
}

这里是主要内容

   import javax.swing.*;
   import java.awt.*;

    public class ProgDraftMain {
    public static void main(String args[]) {
    ProgDraft gui = new ProgDraft();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    gui.setResizable(false);
    gui.setVisible(true);

    //gui.pack();
    gui.setSize(500 , 300);
    }
    } 

谢谢!

最佳答案

您已经将 Logo 添加到面板中,但您尝试再次将 Logo 添加到内容 Pane 中(与标题相同)。第一次添加到面板的操作被否定,这很糟糕,因为您需要 FlowLayout 来保持位置。每个组件只能有一个父组件。不要将 Logo 添加到内容 Pane 。只需添加面板,并将面板的布局设置为new FlowLayout(FlowLayut.LEADING)。这会将标签放置在面板的最左侧。然后可以为标签添加空边框,为标签添加间距

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ProgDraftMain {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                ProgDraft gui = new ProgDraft();
                gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
                gui.setResizable(false);
                gui.setSize(500 , 300);
                gui.setVisible(true);           
            }
        });
    }
}

class ProgDraft extends JFrame {
    private ImageIcon image1;
    private JLabel label1;

    ProgDraft() {

        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout(FlowLayout.LEADING));

        ImageIcon pics = new ImageIcon(getClass().getResource("stackoverflow.png"));

        JLabel logo = new JLabel(pics);
        logo.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 0));
        panel.add(logo);
        panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

        JLabel title = new JLabel("Testing Title", JLabel.CENTER);
        Font font = new Font("impact", Font.PLAIN, 30);
        title.setFont(font);

        getContentPane().setLayout(new BorderLayout());

        getContentPane().add(panel, BorderLayout.CENTER);
        getContentPane().add(title, BorderLayout.PAGE_START);
    }
}

学习使用不同的布局管理器。不要试图依赖像素完美位置。请参阅Laying out Components Within a Container

enter image description here

另请查看How to Use Borders

关于java - 图像的永久位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25621312/

相关文章:

java - 为什么我的 JPanel 不显示?

java - 输出为假时如何重复 "if"语句

java - 如何在 Java 中使用 mustache 遍历 map

java - 通过for循环添加不同名称的对象

java - 打开新的 jFrame 时清空 ArrayList

JavaFX VBox 布局

user-interface - 很棒的首次运行体验

java - 安装时自动从 Sourceforge 下载

java - 在 Swing 应用程序中在运行时更改字体大小

java - 将 JLabel 溢出到另一个 JLabel