gpt4 book ai didi

java - 如何去除 JFrame 边框让图像触及边缘

转载 作者:搜寻专家 更新时间:2023-10-31 19:29:22 25 4
gpt4 key购买 nike

我有一个 friend 为我制作的程序做了一个背景,这样它看起来就不会那么平淡,我认为放置图像的最佳方式是制作一个 JLabel,用图像填充它,然后设置它到屏幕的大小。这工作正常,除了 JFrame 周围有一个小边框,我无法让 JLabel 接触框架的边缘。想法?我附上了一张照片。

Border Problem

public class ProgramDriver extends JFrame {

private JPanel contentPane;
private static CardLayout cardLayout;
private JTextField addGradeN;
private JTextField addGradeD;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ProgramDriver frame = new ProgramDriver();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

//Global Variables
...
manager = new StateManager(gb);

//JFrame Settings
setTitle("Grade Book");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 656, 530);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
cardLayout = new CardLayout(0,0);
contentPane.setLayout(cardLayout);
setResizable(false);

//Home Panel
final JPanel Home = new JPanel();
contentPane.add(Home, "Home");
Home.setLayout(null);

JButton btnSeeGrades = new JButton("See Grades");
...

//Grades Panel
JPanel Grades = new JPanel();
contentPane.add(Grades, "Grades");
Grades.setLayout(null);'

最佳答案

问题不在于 JFrame,而在于您的代码。我们可以用余生来猜测问题所在,或者您可以发布一些示例代码。

现在由您决定,我们可以继续尝试对您进行错误的猜测,让我们所有人都感到沮丧,或者您可以帮助我们帮助您...

这是我做的两个例子。第一个使用 JLabel 作为 JPanel 的主要内容,其中放置了子组件。漂亮而简单。

第二个使用自定义 JPanel 将图像绘制到组件的背景上。然后我用它来替换框架内容 Pane 。这有点复杂,但它具有易于更新的额外好处(替换内容 Pane 不会影响程序的其余部分)

示例 1:JLabel 用作背景

JLabel as background

public class TestBackground {

public static final String BACKGROUND_PATH = "/Volumes/Macintosh HD2/Dropbox/MT015.jpg";

public static void main(String[] args) {
new TestBackground();
}

public TestBackground() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new LabelPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

}
});

}

protected class LabelPane extends JPanel {

public LabelPane() {

BufferedImage bg = null;
try {
bg = ImageIO.read(new File(BACKGROUND_PATH));
} catch (IOException ex) {
ex.printStackTrace();
}

JLabel label = new JLabel(new ImageIcon(bg));
setLayout(new BorderLayout());

add(label);

label.setLayout(new GridBagLayout());

JLabel lblMessage = new JLabel("Look at me!");
lblMessage.setForeground(Color.WHITE);
lblMessage.setFont(lblMessage.getFont().deriveFont(Font.BOLD, 48));

label.add(lblMessage);

}
}
}

示例 2:图像用作背景,替换内容 Pane ...

Background Content Pane

public class TestBackground {
public static final String BACKGROUND_PATH = "/Volumes/Macintosh HD2/Dropbox/MT015.jpg";

public static void main(String[] args) {
new TestBackground();
}

public TestBackground() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new BackgroundPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

protected class BackgroundPane extends JPanel {

private BufferedImage bg = null;

public BackgroundPane() {
try {
bg = ImageIO.read(new File(BACKGROUND_PATH));
} catch (IOException ex) {
ex.printStackTrace();
}

setLayout(new GridBagLayout());

JLabel lblMessage = new JLabel("Look at me!");
lblMessage.setForeground(Color.WHITE);
lblMessage.setFont(lblMessage.getFont().deriveFont(Font.BOLD, 48));

add(lblMessage);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(1153, 823);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (bg != null) {
g.drawImage(bg, 0, 0, this);
}
}
}
}

关于java - 如何去除 JFrame 边框让图像触及边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12762894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com