这是我在这里发表的第一篇文章,试试看。 我有一个带JLabel的小JFrame窗口,图像作为JFrame的背景。 JFrame还有2个JButtons。到目前为止它运作良好,直到我决定添加JComboBox。此操作的结果现在是当我运行我的JFrame时它显示空白,没有来自JLabel的背景图像,没有可见的ComboBox,显示了JButton。当我调整大小这个窗口时,JLabel背景图像出现,一切都很好,但它应该没有调整大小。我在这里错过了什么?我非常喜欢挥杆,我正在做#34;游戏"对于我的java类项目。这是截图和代码:
public class View_Startup extends JFrame {
JLabel lBackground;
JButton bStart,bExit;
JComboBox cbResolutions;
ImageIcon iBackground,iStart,iExit;
Image icon;
String resolutions[] = {"1280x720 HD","1366x768 WXGA","1600x900HD+","1920x1080 fullHD"};
public View_Startup() {
iBackground = new ImageIcon("xdddddddddd/resolution_background.jpg");
iStart = new ImageIcon("xddddddddddd/iStart2.png");
iExit = new ImageIcon("xdddddddddd/iExit2.png");
this.setSize(656,399);
this.setTitle("xddddddddd");
this.icon = Toolkit.getDefaultToolkit().getImage("C:xdddddddddd\\images.jpg");
this.setIconImage(icon);
this.setVisible(true);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
int x=(int) rect.getMaxX();
int y=(int) rect.getMaxY();
this.setLocation(x/2-328,y/2-199);
bStart = new JButton(iStart);
bStart.setBounds(490,240,150,50);
bStart.setOpaque(false);
bStart.setContentAreaFilled(false);
//bStart.setBorderPainted(false);
add(bStart);
bExit = new JButton(iExit);
bExit.setBounds(490,300,150,50);
bExit.setOpaque(false);
bExit.setContentAreaFilled(false);
//bExit.setBorderPainted(false);
add(bExit);
cbResolutions = new JComboBox(resolutions);
cbResolutions.setBounds(490,180,150,50);
add(cbResolutions);
lBackground = new JLabel(iBackground);
lBackground.setBounds(0,0,640,360);
add(lBackground);
}
}
答案 0 :(得分:2)
这里有几个问题:
setVisible(true)
,您已经完全构建了它。只在添加所有组件后调用此方法。而是考虑
paintComponent
方法显示背景图片.setVisible(true)
。这样的事情:
可以使用以下代码创建:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class LayoutExample extends JPanel {
private static final long serialVersionUID = 1L;
private BufferedImage background;
private JButton startButton = new JButton("Start");
private JButton exitButton = new JButton("Exit");
private JComboBox<String> combo = new JComboBox<>(new String[] {"1280 x 780 HD"});
public LayoutExample(BufferedImage background) {
this.background = background;
JPanel rightLowerPanel = new JPanel(new GridLayout(0, 1, 5, 5));
rightLowerPanel.setOpaque(false);
rightLowerPanel.add(combo);
rightLowerPanel.add(startButton);
rightLowerPanel.add(exitButton);
JPanel rightPanel = new JPanel(new BorderLayout());
rightPanel.setOpaque(false);
rightPanel.add(rightLowerPanel, BorderLayout.PAGE_END);
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setLayout(new BorderLayout());
add(rightPanel, BorderLayout.LINE_END);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
g.drawImage(background, 0, 0, this);
}
}
@Override
public Dimension getPreferredSize() {
if (background != null) {
int w = background.getWidth();
int h = background.getHeight();
return new Dimension(w, h);
}
return super.getPreferredSize();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
private static void createAndShowGui() {
String imgPath = "https://pbs.twimg.com/media/DRHUe_tV4AA96G4.jpg";
BufferedImage img = null;
try {
URL imageUrl = new URL(imgPath);
img = ImageIO.read(imageUrl);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
LayoutExample mainPanel = new LayoutExample(img);
JFrame frame = new JFrame("LayoutExample");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}