gpt4 book ai didi

java - 在使用 Nimbus 外观时,如果有背景图像,JButton 会完全消失在 JFrame 中

转载 作者:搜寻专家 更新时间:2023-11-01 02:26:25 24 4
gpt4 key购买 nike

我有一个 JFrame。我用过:

  1. JTabbed Pane 。
  2. J 按钮。
  3. 在 JPanel 中添加的 Jlabel 中的背景图像。
  4. Nimbus 的外观和感觉。

我的问题是,每当我使用 Nimbus 时,只要添加了背景图像,JButton 就会消失。但这不会发生在其他外观和感觉上。谁能帮我让按钮可见?

这是我的代码:

import javax.swing.ImageIcon;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.UIManager.LookAndFeelInfo;

import java.awt.*;
import java.awt.event.*;

class ImageTest
{
JTabbedPane tp;
JLabel lab1;
JPanel welcome;
JFrame frame;
ImageIcon image2;
JButton b1;
public void createUI()
{
frame=new JFrame("JTabbedPane Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


b1=new JButton();
welcome= new JPanel(null);
welcome.setPreferredSize(new Dimension(1366,786));
ImageIcon icon = new ImageIcon(ImageTest.class.getResource("icloud.jpg"));

tp=new JTabbedPane();
Container pane = frame.getContentPane();
pane.add(tp);
tp.addTab("Welcome", welcome);
lab1=new JLabel();
lab1.setIcon(icon);

b1.setBounds(100,100,100,100);
lab1.setBounds(0,0,1500,700);
welcome.add(lab1);
welcome.add(b1);
b1.setVisible(true);
frame.setSize(500,500);
frame.setVisible(true);
frame.setTitle("I-Cloud");

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

try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {

}
ImageTest w = new ImageTest();
w.createUI();

}
}

编辑:

 import javax.swing.ImageIcon;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.UIManager.LookAndFeelInfo;

import java.awt.*;
import java.awt.event.*;

class ImageTest
{
JTabbedPane tp;
JLabel lab1;
JPanel welcome,w;
JFrame frame;
ImageIcon image2;
JButton b1;

public void createUI()
{
frame=new JFrame("JTabbedPane Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

b1=new JButton();
welcome= new JPanel(new GridLayout(1,1,15,15));
w=new JPanel (new GridLayout(1, 1, 15, 15));
welcome.setPreferredSize(new Dimension(1366,786));

ImageIcon icon = new ImageIcon(ImageTest.class.getResource("icloud.jpg"));

tp=new JTabbedPane();
Container pane = frame.getContentPane();
pane.add(tp);


lab1=new JLabel();
lab1.setIcon(icon);
w.setOpaque(false);
w.add(b1);


b1.setVisible(true);
welcome.add(lab1);
welcome.add(w);
tp.addTab("Welcome", welcome);

frame.setSize(500,500);
frame.pack();
frame.setVisible(true);
frame.setTitle("I-Cloud");

}
public static void main(String[] args)
{
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {

}

ImageTest w = new ImageTest();
w.createUI();

}
}

谢谢。

最佳答案

您对 Swing GUI 的使用是不平衡的,因为使用了空布局,然后将一个组件添加到另一个组件之上,实质上是用另一个组件(带有图像的 JLabel)覆盖了一个组件(JButton)。

  • 不要像现在这样使用空布局。虽然对于使用空布局和 setBounds 的新手来说似乎是创建复杂 GUI 的最佳方式,但您处理 Swing GUI 创建的次数越多,您就越会发现这样做会使您的 GUI 穿上紧身衣,将其涂成非常狭窄的角落,很难扩展或增强。只是不要这样做。
  • 不要在同一容器中将一个组件添加到另一个组件之上。
  • 而是为带有图像的 JLabel 提供布局管理器。
  • 然后将 JButton 添加到 JLabel。
  • 然后将 JLabel 添加到 JTabbedPane。

编辑
您最近的编辑没有按照上面的建议使用 JLabel 作为容器。因此,如果您使用单独的容器,那么按钮和标签将并排显示。要解决这个问题,您需要给 JLabel 一个布局管理器,然后按照上面的建议将按钮添加到,或者直接在 JPanel 的 paintComponent 中绘制(...) 方法,并将按钮添加到 JPanel。

以后者为例:

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;

public class MyImageTest extends JPanel {
// public static final String SPEC = "https://duke.kenai.com/SunRIP/.Midsize/SunRIP.jpg.png";
public static final String SPEC = "http://upload.wikimedia.org/wikipedia/commons/3/37/"
+ "Mandel_zoom_14_satellite_julia_island.jpg";
private static final int PREF_H = 786;
private static final int GAP = 100;
private BufferedImage img;

public MyImageTest() {
try {
URL imgUrl = new URL(SPEC);
img = ImageIO.read(imgUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
setLayout(new FlowLayout(FlowLayout.LEADING));
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
JButton b = new JButton();
b.setPreferredSize(new Dimension(GAP, GAP));
add(b);
}

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

@Override
public Dimension getPreferredSize() {
if (img == null) {
return super.getPreferredSize();
} else {
int width = (img.getWidth() * PREF_H) / img.getHeight();
return new Dimension(width, PREF_H);
// return new Dimension(img.getWidth(), img.getHeight());
}
}

private static void createAndShowGui() {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
MyImageTest mainPanel = new MyImageTest();

JFrame frame = new JFrame("MyImageTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

enter image description here

关于java - 在使用 Nimbus 外观时,如果有背景图像,JButton 会完全消失在 JFrame 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22017592/

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