How to make a simple image class file in Java - java

Okai, so basically everything works but the second private class which I named project 2. In this program a grid of 2 by 2 is built out, now in one of the grid spaces (Project3), I want to have an image to be displayed.
public class Project1 extends JFrame
{
private Project2 topleft; // Buttons
private Project3 topright; // Picture
private Project4 bottomleft; // Schedule
//private Project5 bottomright; // Help - Pad
// Constructor
public Project1() throws IOException
{
// Display a title.
setTitle(" UAE University Interactive Course Calculator");
// Specify an action for the close button.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a GridLayout manager.
setLayout(new GridLayout(2, 2));
// Create the custom panels.
topleft = new Project2();
topright = new Project3();
bottomleft = new Project4();
//bottomright = new Project5();
//bottomright = new Project5();
// Create the button panel.
add(topleft);
add(topright);
add(bottomleft);
//add(bottomright);
// setting formatting options
pack();
setResizable(true);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setVisible(true);
}
// Main method
public static void main(String[] args) throws IOException
{
new Project1();
}
}
This is where I got to so far, maybe my logic is wrong. All I need is just an image to be displayed, that's all
public class Project3 extends JPanel
{
private JButton run,fancyButton, hel, but, done_by,exit, past;
public Project3()
{
Icon java1 = new ImageIcon( "untitled1.jpg" );
fancyButton = new JButton( "Fancy Button", java1 );
add(fancyButton);
}
}
EDIT!!!: okai now this works, but it's a buttoned image, I want it to become just an image without the button any ideas?

One of the simplest options to load and show an image on a JPanel:
Create an ImageIcon.
Create a JLabel and pass the ImageIcon as a parameter.
Add the JLabel to the JPanel (or any other swing component).
You can find a detailed example by reading How to use icons.

Related

Why JLabel is not visible at mine JFrame class?

I am trying to learn java,but I met some problems where finding answers is not so simply for me.
Task I wanna do seems to be quite simply.
I wanna add a label to Frame. MyFrame is a JFrame class with some basics modyfications like size, color etc.
Main code looks like this:
public class Main {
public static void main(String[] args)
{
// a GUI window to add components
MyFrame myFrame = new MyFrame();
ImageIcon image = new ImageIcon("Images/background.png");
//a GUI display area for a string of text,image or both
JLabel label = new JLabel("Why it is happening?",image,JLabel.CENTER);
myFrame.add(label);
//label.setText("Why it is happening?");// set text of label
//label.setIcon(image);
label.setHorizontalTextPosition(JLabel.CENTER);
}
}
And the result I get is this which I want:
correct result
When I comment last line about label it is changing appearance of mine UI. It is displaying only JFrame without my label at all.
Not working code:
public class Main {
public static void main(String[] args)
{
// a GUI window to add components
MyFrame myFrame = new MyFrame();
ImageIcon image = new ImageIcon("Images/background.png");
//a GUI display area for a string of text,image or both
JLabel label = new JLabel("Why it is happening?",image,JLabel.CENTER);
myFrame.add(label);
//label.setText("Why it is happening?");// set text of label
//label.setIcon(image);
//Commented
//label.setHorizontalTextPosition(JLabel.CENTER);
}
}
Here is graphic result:
not working label
Did I miss some basics information ?
I think both should work the same.
This is an interesting one. At first glance I agreed with #VGR. But then if it is all down to the order of painting etc. - then I would have expected a call to 'repaint()' or even 'invalidate()' to fix the problem. Doesn't seem like it... so perhaps this is a bug in swing.
import javax.swing.*;
public class Main
{
public static void main(String[] args)
{
// a GUI window to add components
JFrame myFrame = new JFrame();
ImageIcon image = new ImageIcon("Images/background.png");
myFrame.setSize(500,250);
myFrame.setVisible(true);
JLabel label = new JLabel("Why it is happening?", image, JLabel.CENTER);
myFrame.add(label);
//label.setHorizontalTextPosition(JLabel.CENTER);
label.repaint(); //Should have the same effect as the line above, but doesn't!
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Adding parameters to a GUI constructor stops GUI pop up

I've started learning Java with Eclipse, and I altered the main function to pass two strings to the similarly altered GUI constructor (there was nothing passed before).
The GUI doesn't pop up on the screen now, but can be accessed from the task bar at the bottom of the screen. I was just wondering why this happened? I've pasted the shortened code below.
I've tried it multiple times, and tried to find the problem with keywords on the web.
public class MainButton
{
public static void main(String[] args)
{
String A = "title"; String B = "Button";
Agui a = new Agui(A,B);
//BEFORE Agui a = new Agui();
}
}
import javax.swing.*;
public class Agui extends JFrame
{
//BEFORE public Agui()
public Agui(String A, String B)
{
setTitle(A);
setSize(400, 400);
// Create JButton and JPanel
JButton button = new JButton(B);
JPanel panel = new JPanel();
// Add button to JPanel
panel.add(button);
// And JPanel needs to be added to the JFrame itself!
this.getContentPane().add(panel);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
It'd be great to get the pop up without going to the task bar at the bottom of the screen, and to understand the logic of why this problem occurs.

Adding jlabel to a jframe using components

I have 2 classes,
My main class creates a frame and I want another class to add content to it. A bit of reading arroudn told me I should use components to do this however when I run my code the frame is empty.
public static void main(String[] args)
{
// create frame
JFrame frame = new JFrame();
final int FRAME_WIDTH = 800;
final int FRAME_HEIGHT = 600;
// set frame attributes
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("My Frame");
frame.setVisible(true);
Component1 Com = new Component1();
Component add = frame.add(Com);
}
My Component class creates a JLabel
public class Component1 extends JComponent {
public void paintComponent()
{
JLabel label = new JLabel("<html>Some Text</html>");
}
}
I don't get any compile errors, however I dont get any text in my JFrame.
Can anyone explain what I'm doing wrong?
Chris
You need to add the JLabel. Also better to extend JPanel instead of JComponent as it has a default layout manager and will make any added components appear without the need to set component sizes. paintComponent is used for custom painting BTW.
public class Component1 extends JPanel {
Component1() {
JLabel label = new JLabel("<html>Some Text</html>");
add(label);
}
}
No need to create a new Component. Just call frame.getContentPane().add(label). And initialize your label before this.

Adding a button component to a java tabbed pane in java

i am trying to create GUI using java swings.I am just a beginner in java swings.
My primary idea was to create two tabs and add a button in one of the tabs.
I wanted to write a separate class for each tab so i created 3 classes out of which one has the main method.and the other two represent the tabs.
In one of the tabs i wanted to add a button in middle and add an action listener to that button.
below is the class which has the main method.
public class abc {
JFrame frame;
JTabbedPane tabPane;
ImageIcon close;
Dimension size;
int tabCounter = 0;
abc_export exp;
abc_import imp;
public static void main(String[] args) {
abc jtab = new abc();
jtab.start();
}
public void start(){
exp=new abc_export();
imp=new abc_import();
tabPane.addTab(null, exp.panel);
tabPane.addTab(null, imp.panel);
tabPane.setTabComponentAt(tabPane.getTabCount()-1, exp.tab);
tabPane.setTabComponentAt(tabPane.getTabCount()-1, imp.tab);
}
public abc() {
// Create a frame
frame = new JFrame();
// Create the tabbed pane.
tabPane = new JTabbedPane();
// Create a button to add a tab
// Create an image icon to use as a close button
close = new ImageIcon("C:/JAVAJAZZUP/tabClose.gif");
size = new Dimension(close.getIconWidth()+1, close.getIconHeight()+1);
//Adding into frame
frame.add(tabPane, BorderLayout.CENTER);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
};
below is code for one of the tabs.although the other tab is also having the same code which represent other tab with different class name.
public class abc_import {
ImageIcon close;
Dimension size;
int tabCounter = 0;
JPanel tab;
final JPanel panel;
public abc_import() {
close = new ImageIcon("C:/JAVAJAZZUP/tabClose.gif");
size = new Dimension(close.getIconWidth()+1, close.getIconHeight()+1);
//Adding into frame
JLabel label = null;
panel = new JPanel();
// Create a panel to represent the tab
tab = new JPanel();
tab.setOpaque(false);
String str = "abc_import";
label = new JLabel(str);
tab.add(label, BorderLayout.WEST);
}
};
as expected both the tabs are created.But i am out of ideas about adding a button inside one of the tabs.
Now my question here is if i wanted to add a button in one of the tabs as i already said.what do i need to do?can anyone help me?
I'm not sure I understand your intent, but you might try the approach shown in the TabComponentsDemo, discussed in How to Use Tabbed Panes: Tabs With Custom Components.
A related example is shown here.
You can try by using setTabComponentAt method.
This methd has the parameter setTabComponentAt(int index, Component component), in which you just mention the component you want.
You can refer a link here.

How to switch between to JPanels within just one JFrame?

I am writing a tetris game. When the application starts Jlabel with button "Play" opens. How do I switch to a different label (Board) within the existing Jframe?
Like this it opens the game directly.. But first I would want to use the ButtonPage class to show some welcome screen with a button instead and then call the game.
public class Tetris extends JFrame {
public Tetris(){
// JFrame Properties
setSize(198, 409);
setResizable(false);
setTitle("Tetris");
setDefaultCloseOperation(EXIT_ON_CLOSE);
// ButtonPage buttons = new ButtonPage();
// add(buttons);
// buttons.setOpaque(true);
Board board = new Board(this);
add(board);
board.start();
} // end of constructor
public static void main(String[] args){
Tetris game = new Tetris();
game.setLocationRelativeTo(null);
game.setVisible(true);
game.setLayout(null);
} // end of main
} // end of class
Here is the ButtonPage class.
public class ButtonPage extends JPanel implements ActionListener{
JButton buttonPLAY = new JButton();
JLabel backgroundImage = new JLabel();
public ButtonPage(){
setLayout(null);
ImageIcon buttonIcon = new ImageIcon(getClass().getResource("PlayButton.png"));
ImageIcon buttonIconHover = new ImageIcon(getClass().getResource("PlayButtonHover.png"));
ImageIcon buttonIconClicked = new ImageIcon(getClass().getResource("PlayButtonClicked.png"));
int buttonHeight = buttonIcon.getIconHeight();
int buttonWidth = buttonIcon.getIconWidth();
buttonPLAY.addActionListener(this);
buttonPLAY.setActionCommand("Play");
buttonPLAY.setIcon(buttonIcon);
buttonPLAY.setRolloverIcon(buttonIconHover);
buttonPLAY.setPressedIcon(buttonIconClicked);
buttonPLAY.setBorderPainted(false);
add(buttonPLAY);
Dimension size2 = getSize();
Dimension size = buttonPLAY.getPreferredSize();
buttonPLAY.setBounds((192 - buttonWidth)/2, 100 ,buttonWidth, buttonHeight);
}// end of constructor
#Override
public void actionPerformed(ActionEvent e) {
if ("Play".equals(e.getActionCommand())) {
Tetris game = new Tetris();
// opens the window in the middle of the screen
game.setLocationRelativeTo(null);
// set the tetris window visible, unless its true - its invisible DUH!
game.setVisible(true);
game.setLayout(null);
}
} // end of actionPerformed
}// end of class
Using the actionPerformed method I can open the game in a new Frame, but I have no idea how to switch the Panels.
Thanks in advance for any tips!
Tetris is intanciated from main, the following line from actionPerformed():
Tetris game = new Tetris();
instanciates a second Tetris, it's really what's you want?
To add several panels to a frame, only one visible at a time, use CardLayout.
Have you tried a card layout?
http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html

Categories

Resources