import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Character {
private JFrame characterWindow;
private JPanel allPanels, imagePanelHero, imagePanelVillain;
private JButton HeroBtn, VillainBtn;
private JLabel textLabel, heroTextLabel, villainTextLabel, imageLabel;
public Character()
{
allPanels = new JPanel();
allPanels.setLayout(null);
allPanels.setBackground(new Color(255,255,255));
imagePanelHero = new JPanel();
imagePanelHero.setBackground(Color.BLACK);
imagePanelHero.setBounds(205,150,300,300);
ImageIcon pic1 = new ImageIcon("src/superman1.jpg");
imagePanelHero.add(new JLabel(pic1));
this.add(imagePanelHero);
this.pack();
this.setVisible(true);
allPanels.add(imagePanelHero);
imagePanelVillain = new JPanel();
imagePanelVillain.setBackground(Color.BLACK);
imagePanelVillain.setBounds(790,150,300,300);
allPanels.add(imagePanelVillain);
ImageIcon pic2 = new ImageIcon("src/Lex1.jpg");
imagePanelVillain.add(new JLabel(pic2));
this.pack();
this.setVisible(true);
textLabel = new JLabel("Pick: Hero or Villain? ", JLabel.CENTER);
textLabel.setBounds(20,40,1280,30);
allPanels.add(textLabel);
HeroBtn = new JButton("Hero");
HeroBtn.setBounds(300,485,100,50);
HeroBtn.setBackground(Color.BLUE);
HeroBtn.setForeground(Color.WHITE);
heroTextLabel = new JLabel("Fight for the side of Good and save the people of the world. ", JLabel.CENTER);
heroTextLabel.setBounds(0,575,715,30);
allPanels.add(heroTextLabel);
VillainBtn = new JButton("Villain");
VillainBtn.setBounds(890,485,100,50);
VillainBtn.setBackground(Color.BLUE);
VillainBtn.setForeground(Color.WHITE);
villainTextLabel = new JLabel("Fight for evil and chaos creating mayhem wherever you go.? ", JLabel.CENTER);
villainTextLabel.setBounds(100,575,1705,30);
allPanels.add(villainTextLabel);
allPanels.add(HeroBtn);
allPanels.add(VillainBtn);
HeroBtn.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
callHero();
}
});
VillainBtn.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
callVillain();
}
});
characterWindow = new JFrame();
characterWindow.setTitle("Hero or Villain?");
characterWindow.setSize(1280, 720);
characterWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
characterWindow.setVisible(true);
characterWindow.add(allPanels);
characterWindow.setResizable(true);
characterWindow.setLocationRelativeTo (null);
ImageIcon pic3 = new ImageIcon("src/Metropolis.jpg");
characterWindow.add(new JLabel(pic3));
this.pack();
this.setVisible(true);
}
private void setVisible(boolean b) {
// TODO Auto-generated method stub
}
private void pack() {
// TODO Auto-generated method stub
}
private void add(JPanel imagePanelHero2) {
// TODO Auto-generated method stub
}
protected void callHero() {
// TODO Auto-generated method stub
}
protected void callVillain() {
// TODO Auto-generated method stub
}
public static void main (String args [])
{
Character character = new Character();
}
}
我正在尝试创建一个具有城市背景图像的JFrame,但是当我运行我的程序时,jframe上的背景颜色出现在图像的前面,图像正在显示,因为当我调整窗口大小时,我可以看到背景颜色背后的图像边缘,是否有人有任何简单的想法如何解决这个问题,即使背景颜色透明或移动图像前面的颜色,我是一个初学者与代码所以我不确定在某些方面,这就是为什么一个简单的答案将不胜感激。