Can anyone help me I wanted to show a background image for GUI but now it has went on top of all my labels, textfields, output window....
How can I get the background image to show on the GUI with all the labels etc. ???
Thanks.
package newpackage10;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class NewClass10 extends JFrame{
public NewClass10() {
initiateComponents();
}
private JPanel innerPanel;
private JMenuBar MainMenuBar;
private JMenu FileMenu;
private JMenuItem NewScenarioMenuItem;
private JMenuItem OpenScenarioMenuItem;
private JMenuItem SaveScenarioMenuItem;
private JMenuItem ClearOutputWindowMenuItem;
private JMenuItem ExitMenuItem;
private JMenu ScenarioMenu;
private JMenuItem EditScenarioMenuItem;
private JMenuItem CheckSyntaxScenarioMenuItem;
private JMenuItem OpenScenarioGeneratorMenuItem;
private JMenu RunMenu;
private JMenuItem RunSimulationMenuItem;
private JMenuItem MakeQueryMenuItem;
private JMenu HelpMenu;
private JMenuItem ReadMeMenuItem;
private JMenuItem AboutMenuItem;
private JLabel TitleLabel;
private JLabel AlgorithmLabel;
private JLabel ScenarioLabel;
private JLabel GoalLabel;
private JLabel TimeLabel;
private JLabel PredicateLabel;
private JTextField AlgorithmTextField;
private JTextField ScenarioTextField;
private JTextField GoalTextField;
private JTextField TimeTextField;
private JTextField PredicateTextField;
private JButton SearchButton;
private JButton HoldsButton;
private JTextArea OutputWindow;
private void initiateComponents() {
getContentPane().setLayout(null);
setTitle("Dynamic Environment Reasoning System");
setSize(new Dimension(1025, 703));
setLocation(0,0);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setBackground(new Color(0,191,255));
getContentPane().add(getInnerPanel());
setJMenuBar(getMainMenuBar());
getContentPane().add(getTitleLabel());
populateInnerPanel();
setContentPane(new ContentPanel("C:\\Documents and Settings\\Flo\\My Documents\\My Pictures\\bigstockphoto_Binary_Globe_55596.jpg"));
}
private JMenuBar getMainMenuBar() {
MainMenuBar = new JMenuBar();
FileMenu = new JMenu();
FileMenu.setText(" File");
FileMenu.setFont(new Font("Bookman Old Style",1,14));
FileMenu.setPreferredSize(new Dimension(49,20));
MainMenuBar.add(FileMenu);
NewScenarioMenuItem = new JMenuItem();
NewScenarioMenuItem.setText("New Scenario");
NewScenarioMenuItem.setFont(new Font("Bookman Old Style",0,14));
NewScenarioMenuItem.setPreferredSize(new Dimension(120,20));
FileMenu.add(NewScenarioMenuItem);
OpenScenarioMenuItem = new JMenuItem();
OpenScenarioMenuItem.setText("Open Scenario");
OpenScenarioMenuItem.setFont(new Font("Bookman Old Style",0,14));
OpenScenarioMenuItem.setPreferredSize(new Dimension(120,20));
FileMenu.add(OpenScenarioMenuItem);
SaveScenarioMenuItem = new JMenuItem();
SaveScenarioMenuItem.setText("Save Scenario");
SaveScenarioMenuItem.setFont(new Font("Bookman Old Style",0,14));
SaveScenarioMenuItem.setPreferredSize(new Dimension(120,20));
FileMenu.add(SaveScenarioMenuItem);
ClearOutputWindowMenuItem = new JMenuItem();
ClearOutputWindowMenuItem.setText("Clear Output Window");
ClearOutputWindowMenuItem.setFont(new Font("Bookman Old Style",0,14));
ClearOutputWindowMenuItem.setPreferredSize(new Dimension(170,20));
FileMenu.add(ClearOutputWindowMenuItem);
ExitMenuItem = new javax.swing.JMenuItem();
ExitMenuItem.setText("Exit");
ExitMenuItem.setFont(new Font("Bookman Old Style",0,14));
ExitMenuItem.setPreferredSize(new Dimension(120,20));
FileMenu.add(ExitMenuItem);
ScenarioMenu = new javax.swing.JMenu();
ScenarioMenu.setText("Scenario");
ScenarioMenu.setFont(new Font("Bookman Old Style",1,14));
ScenarioMenu.setPreferredSize(new Dimension(73,20));
MainMenuBar.add(ScenarioMenu);
EditScenarioMenuItem = new JMenuItem();
EditScenarioMenuItem.setText("Edit Scenario");
EditScenarioMenuItem.setFont(new Font("Bookman Old Style",0,14));
EditScenarioMenuItem.setPreferredSize(new Dimension(110,20));
ScenarioMenu.add(EditScenarioMenuItem);
CheckSyntaxScenarioMenuItem = new JMenuItem();
CheckSyntaxScenarioMenuItem.setText("Check Scenario Syntax");
CheckSyntaxScenarioMenuItem.setFont(new Font("Bookman Old Style",0,14));
CheckSyntaxScenarioMenuItem.setPreferredSize(new Dimension(180,20));
ScenarioMenu.add(CheckSyntaxScenarioMenuItem);
OpenScenarioGeneratorMenuItem = new JMenuItem();
OpenScenarioGeneratorMenuItem.setText("Open Scenario Generator");
OpenScenarioGeneratorMenuItem.setFont(new Font("Bookman Old Style",0,14));
OpenScenarioGeneratorMenuItem.setPreferredSize(new Dimension(200,20));
ScenarioMenu.add(OpenScenarioGeneratorMenuItem);
RunMenu = new javax.swing.JMenu();
RunMenu.setText(" Run");
RunMenu.setFont(new Font("Bookman Old Style",1,14));
RunMenu.setPreferredSize(new Dimension(74,20));
MainMenuBar.add(RunMenu);
RunSimulationMenuItem = new JMenuItem();
RunSimulationMenuItem.setText("Run Simulation");
RunSimulationMenuItem.setFont(new Font("Bookman Old Style",0,14));
RunSimulationMenuItem.setPreferredSize(new Dimension(130,20));
RunMenu.add(RunSimulationMenuItem);
MakeQueryMenuItem = new JMenuItem();
MakeQueryMenuItem.setText("Make Query");
MakeQueryMenuItem.setFont(new Font("Bookman Old Style",0,14));
MakeQueryMenuItem.setPreferredSize(new Dimension(110,20));
RunMenu.add(MakeQueryMenuItem);
HelpMenu = new javax.swing.JMenu();
HelpMenu.setText("Help");
HelpMenu.setFont(new Font("Bookman Old Style",1,14));
HelpMenu.setPreferredSize(new Dimension(80,20));
MainMenuBar.add(HelpMenu);
ReadMeMenuItem = new JMenuItem();
ReadMeMenuItem.setText("Read Me");
ReadMeMenuItem.setFont(new Font("Bookman Old Style",0,14));
ReadMeMenuItem.setPreferredSize(new Dimension(77,20));
HelpMenu.add(ReadMeMenuItem);
AboutMenuItem = new JMenuItem();
AboutMenuItem.setText("About");
AboutMenuItem.setFont(new Font("Bookman Old Style",0,14));
AboutMenuItem.setPreferredSize(new Dimension(77,20));
HelpMenu.add(AboutMenuItem);
return MainMenuBar;
}
private JLabel getTitleLabel() {
TitleLabel = new JLabel();
TitleLabel.setFont(new Font("Arial",1,16));
TitleLabel.setText(" Dynamic Environment Reasoning System");
TitleLabel.setForeground(new Color(139,0,139));
TitleLabel.setBackground(new Color(153,153,255));
TitleLabel.setBorder(BorderFactory.createRaisedBevelBorder());
TitleLabel.setBounds(600,20,340,30);
return TitleLabel;
}
private JPanel getInnerPanel() {
innerPanel = new JPanel();
innerPanel.setLayout(null);
innerPanel.setBackground(new Color(127,255,212));
innerPanel.setBorder(BorderFactory.createLoweredBevelBorder());
innerPanel.setBounds(26,75,960,550);
return innerPanel;
}
private void populateInnerPanel() {
AlgorithmLabel = new JLabel();
AlgorithmLabel.setBackground(new Color(255,255,240));
AlgorithmLabel.setFont(new Font("Bookman Old Style",0,13));
AlgorithmLabel.setText("Selected Inference Algorithm:");
innerPanel.add(AlgorithmLabel);
AlgorithmLabel.setBounds(20,10,210,30);
ScenarioLabel = new JLabel();
ScenarioLabel.setBackground(new Color(255,255,240));
ScenarioLabel.setFont(new Font("Bookman Old Style",0,13));
ScenarioLabel.setText("Selected Scenario:");
innerPanel.add(ScenarioLabel);
ScenarioLabel.setBounds(260,10,170,30);
GoalLabel = new JLabel();
GoalLabel.setBackground(new Color(255,255,240));
GoalLabel.setFont(new Font("Bookman Old Style",0,13));
GoalLabel.setText("Selected Goal:");
innerPanel.add(GoalLabel);
GoalLabel.setBounds(460,10,170,30);
TimeLabel = new JLabel();
TimeLabel.setBackground(new Color(255,255,240));
TimeLabel.setFont(new Font("Bookman Old Style",0,13));
TimeLabel.setText("Selected Time:");
innerPanel.add(TimeLabel);
TimeLabel.setBounds(630,10,170,30);
PredicateLabel = new JLabel();
PredicateLabel.setBackground(new Color(255,255,240));
PredicateLabel.setFont(new Font("Bookman Old Style",0,13));
PredicateLabel.setText("Predicate:");
innerPanel.add(PredicateLabel);
PredicateLabel.setBounds(810,10,170,30);
AlgorithmTextField = new JTextField();
AlgorithmTextField.setBackground(new Color(255,255,240));
innerPanel.add(AlgorithmTextField);
AlgorithmTextField.setBounds(40,60,100,20);
ScenarioTextField = new JTextField();
ScenarioTextField.setBackground(new Color(255,255,240));
innerPanel.add(ScenarioTextField);
ScenarioTextField.setBounds(270,60,100,20);
GoalTextField = new JTextField();
GoalTextField.setBackground(new Color(255,255,240));
innerPanel.add(GoalTextField);
GoalTextField.setBounds(470,60,100,20);
TimeTextField = new JTextField();
TimeTextField.setBackground(new Color(255,255,240));
innerPanel.add(TimeTextField);
TimeTextField.setBounds(640,60,100,20);
PredicateTextField = new JTextField();
PredicateTextField.setBackground(new Color(255,255,240));
innerPanel.add(PredicateTextField);
PredicateTextField.setBounds(820,60,100,20);
SearchButton = new JButton();
SearchButton.setFont(new Font("Bookman Old Style",1,13));
SearchButton.setText("Search");
innerPanel.add(SearchButton);
SearchButton.setBounds(280,100,90,20);
HoldsButton = new JButton();
HoldsButton.setFont(new Font("Bookman Old Style",1,13));
HoldsButton.setText("Holds");
innerPanel.add(HoldsButton);
HoldsButton.setBounds(830,100,80,20);
OutputWindow = new JTextArea();
OutputWindow.setBackground(new Color(255,255,240));
Font equalSpacedFont = new Font("Monospaced",Font.PLAIN,14);
OutputWindow.setFont(equalSpacedFont);
OutputWindow.setEditable(false);
JScrollPane scrollPane = new JScrollPane(OutputWindow);
scrollPane.setBounds(30,145,900,380);
innerPanel.add(scrollPane);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame.setDefaultLookAndFeelDecorated(true);
NewClass10 nc10 = new NewClass10();
nc10.setVisible(true);
}
});
}
class ContentPanel extends JPanel
{
Image bgimage = null;
ContentPanel(String imagename)
{
if(imagename != null)
{
MediaTracker mt = new MediaTracker(this);
bgimage = Toolkit.getDefaultToolkit().getImage(imagename);
mt.addImage(bgimage, 0);
try
{
mt.waitForAll();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
public void paintComponent(Graphics g) {
g.drawImage(bgimage,-100,-250,null);
}
}
}