Images in a JFrame [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Can someone please give me an example code wich has a JFrame with an image and a something else in it, I just can't get the image working and I need an example that works. I tried some thing with buffered image and image icons but nothing works.

public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Test Image");
frame.setLayout(new BorderLayout());
try {
frame.add(new JLabel(new ImageIcon(new URL(YOUR_IMAGE_URL))),
BorderLayout.NORTH);
} catch (MalformedURLException e) {
e.printStackTrace();
}
frame.add(new JButton("Button"), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
});
}

Related

Adding GIF to frame [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I am trying to add a GIF to the frame. Here is my code but when I run it cannot see GIF. Is there a function to add GIF to java?
Any help would be appreciated.
public class confetti extends JFrame
{
private static JFrame frame;
public confetti() {
frame = new JFrame();
frame.setSize(510, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
addConfetti();
}
public static void addConfetti() {
JPanel jpCenter = new JPanel();
JLabel lblgif = new JLabel(new ImageIcon("confetti-40.gif"));
frame.add(jpCenter,BorderLayout.CENTER);
lblgif.setVisible(true);
}
public static void main(String args[]) {
new confetti();
}
}
You never add your label to any frame/panel. In your code it might be something like this:
public static void addConfetti() {
JPanel jpCenter = new JPanel();
JLabel lblgif = new JLabel(new ImageIcon("path/to/your/confetti-40.gif"));
frame.add(jpCenter, BorderLayout.CENTER);
jpCenter.add(lblgif);
lblgif.setVisible(true);
}
Also check your absolute path from your projecto to your gif file, your code in works for me on my PC.

How to change the position of the object when the window size is changed? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
As in the title. I would like objects moving on the board to change position propositionally to change the size of the window. How to do it?
Let's assume that you are working with a JFrame.
You can get the dimensions of the JFrame as follows:
Dimension size = frame.getBounds().getSize()
double height = size.getHeight();
double width = size.getWidth();
Then you can make your object move by a percentage of those values.
Whenever the window's size changes, an event is triggered.
You can update the dimensions whenever that event is triggered with a ComponentListener.
class ResizeListener implements ComponentListener {
public void componentHidden(ComponentEvent e) {}
public void componentMoved(ComponentEvent e) {}
public void componentShown(ComponentEvent e) {}
public void componentResized(ComponentEvent e) {
Dimension newSize = e.getComponent().getBounds().getSize();
}
}
Do not forget to add the ComponentListener to your JFrame.
Sources: How to check current window size in java swing?
You can use the componentResized method to recalculate the pieces' positions based on the actual frame size
public static void main(String[] args) {
JPanel panel = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
};
panel.addComponentListener(new ComponentAdapter() {
#Override
public void componentResized(ComponentEvent e) {
System.out.println("Resized to " + e.getComponent().getSize());
}
#Override
public void componentMoved(ComponentEvent e) {
System.out.println("Moved to " + e.getComponent().getLocation());
}
});
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("test", panel);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(tabbedPane);
frame.pack();
frame.setVisible(true);
}

How to close current JFrame? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have read similar topics but i did find answer there.
I created JFrame with close button. After click I want to close current window. I try setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE), or setVisible(false).
public class Windows {
JFrame frame;
JFrame frame1;
public Windows(){
}
public JFrame getCreateFrame(){
frame1 = new JFrame("Create User");
frame1.setSize(500,500);
frame1.setVisible(true);
frame1.getContentPane().add(new Panels().getwelcomTxtLabelPanel1(), BorderLayout.NORTH);
frame1.getContentPane().add(new Panels().getCreateUser(), BorderLayout.SOUTH);
frame1.getContentPane().add(new Panels().getUserLabel(), BorderLayout.WEST);
frame1.getContentPane().add(new Panels().getUserField(), BorderLayout.CENTER);
return frame1;
}
}
Here is a button.
public JButton getCancelButton(){
cancel = new JButton("cancel");
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
new Windows().getCreateFrame().setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
}
});
return cancel;
}
The problem is the following action (and not only this):
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
new Windows().getCreateFrame().setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
}
});
here you create a new Windows object and call getCreateFrame() which creates a new JFrame and then you call setDefaultCloseOperation() on it.
So you work with different Windows / JFrame instances.
Instead you should create your JFrame in the constructor of Windows and call setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE) of this JFrame in the constructor as well.
Afterwards you can use setVisible(false) in your action - but for this JFrame and not for a new created one.
BTW. getCancelButton() should most probably not create a new button every time it is called.
You have to make the frame invisible and dispose it.
JFrame frame;
frame.setVisible(false);
frame.dispose();
This completely closes the frame. If only this frame is open and no nondeamon threads are running, the program will quit after disposing the frame.

How to initiate an action upon clicking a jbutton in Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I'm wondering how to initiate an action if a jbutton is clicked on in my JFrame.
I've tried searching for answers but haven't had much luck.
This is all i have right now, i basically just want some text to be displayed upon clicking the button.
public class Slots {
public static void main(String[] args){
Slots();
}
public static void Slots(){
//JFRAME
JFrame f = new JFrame("Slots Game");
f.setSize(500, 500);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setResizable(false);
//JButton
JButton Button = new JButton("Start");
f.add(Button, BorderLayout.PAGE_END);
f.setVisible(true);
}
}
There are 3 ways to do that.
Create a class that implements the ActionListener interface. And then add an instance of that class as an action listener to the button.
Making the current class (in your case Slots) implement the ActionListener interface. And then adding "this" as the action listener to the button.
The third method, which is probably the most convenient/efficient method, is using an anonymous inner class like below.
button.addActionListener(new ActionListener() {
public void actionPerfored(ActionEvent e)
{
// your code goes here
}
});
For more details see ActionListener API

How can I refresh or reload the JFrame? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am doing project using Java and in that I need to reload whole JFrame after clicking particular button on that JFrame. How to do this?
Try
SwingUtilities.updateComponentTreeUI(frame);
If it still doesn't work then after completing the above step try
frame.invalidate();
frame.validate();
frame.repaint();
just use
frame.setVisible(false);
frame.setVisible(true);
I've had this problem with JLabels with images, and this solved it
Here's a short code that might help.
<yourJFrameName> main = new <yourJFrameName>();
main.setVisible(true);
this.dispose();
where...
main.setVisible(true);
will run the JFrame again.
this.dispose();
will terminate the running window.
Try this code. I also faced the same problem, but some how I solved it.
public class KitchenUserInterface {
private JFrame frame;
private JPanel main_panel, northpanel , southpanel;
private JLabel label;
private JButton nextOrder;
private JList list;
private static KitchenUserInterface kitchenRunner ;
public void setList(String[] order){
kitchenRunner.frame.dispose();
kitchenRunner.frame.setVisible(false);
kitchenRunner= new KitchenUserInterface(order);
}
public KitchenUserInterface getInstance() {
if(kitchenRunner == null) {
synchronized(KitchenUserInterface.class) {
if(kitchenRunner == null) {
kitchenRunner = new KitchenUserInterface();
}
}
}
return this.kitchenRunner;
}
private KitchenUserInterface() {
frame = new JFrame("Lullaby's Kitchen");
main_panel = new JPanel();
main_panel.setLayout(new BorderLayout());
frame.setContentPane(main_panel);
northpanel = new JPanel();
northpanel.setLayout(new FlowLayout());
label = new JLabel("Kitchen");
northpanel.add(label);
main_panel.add(northpanel , BorderLayout.NORTH);
frame.setSize(500 , 500 );
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private KitchenUserInterface (String[] order){
this();
list = new JList<String>(order);
main_panel.add(list , BorderLayout.CENTER);
southpanel = new JPanel();
southpanel.setLayout(new FlowLayout());
nextOrder = new JButton("Next Order Set");
nextOrder.addActionListener(new OrderUpListener(list));
southpanel.add(nextOrder);
main_panel.add(southpanel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
KitchenUserInterface dat = kitchenRunner.getInstance();
try{
Thread.sleep(1500);
System.out.println("Ready");
dat.setList(OrderArray.getInstance().getOrders());
}
catch(Exception event) {
System.out.println("Error sleep");
System.out.println(event);
}
}
}
You should use this code
this.setVisible(false); //this will close frame i.e. NewJFrame
new NewJFrame().setVisible(true); // Now this will open NewJFrame for you again and will also get refreshed

Categories

Resources