Java 图像的永久位置?
我的工作有问题。 我想做的是,我希望我的形象有一个特定的或永久的位置。 但每次我调整窗口大小时,图像也会改变其位置。当我使用.setresizeable(false)时,我看不到文本和图像Java 图像的永久位置?,java,swing,user-interface,jpanel,jlabel,Java,Swing,User Interface,Jpanel,Jlabel,我的工作有问题。 我想做的是,我希望我的形象有一个特定的或永久的位置。 但每次我调整窗口大小时,图像也会改变其位置。当我使用.setresizeable(false)时,我看不到文本和图像 import java.awt.*; import javax.swing.*; public class ProgDraft extends JFrame { private ImageIcon image1; private JLabel label1; ProgDraft() { JPan
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);
}
}
谢谢大家! 您已经将徽标添加到面板中,但是您尝试再次将徽标添加到内容窗格中(与标题相同)。对面板的第一次添加被否定,这是不好的,因为您需要FlowLayout来保持位置。每个组件只能有一个父组件。不要将徽标添加到内容窗格中。只需添加面板,并使面板的布局
成为新的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);
}
}
学习使用不同的布局管理器。不要试图依赖像素的完美位置。看
还要看看这会产生问题,因为它是相同的:getContentPane().add(panel);getContentPane().add(logo,BorderLayout.CENTER);这两个组件都添加了BorderLayout.CENTER(默认为CENTER)酷!这是一个很大的帮助!谢谢,我一定会阅读你分享的链接。再次感谢你!