I've finished my store management program with many classes (many JForm panels actually. Thanks to people in this forum whom helped me so much).
I just need to call JPanel Login when I click Run Project.
Any idea how to call it? what code I have to insert to main project?
You cannot display JPanel independently.
So you have to create a JFrame and then add this JPanel object to this JFrame and then display the JFrame.
write these lines in your main method.
JFrame frame = new JFrame("Title");
frame.setSize(500,500);
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // as per your requirement.
frame.add(new Login(), BorderLayout.CENTER);
frame.setVisible(true);
it will work properly.
JFrame myFrame = new JFrame("MyTitle");
myFrame.setSize(width, height);
myFrame.setLocation(x, y);
myFrame.setContentPane(new Login());
myFrame.setVisible(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// if you want add your jcomponent
myFrame.getContentPane().add(yourJComponent);
try it!
If you want add JComponent(JButton)
JButton myBtn = new JButton("btn name");
myBtn.setLocation(x, y);
myFrame.getContentPane().add(myBtn);
If you want add JComponent(JText)
JText myText = new JText("text");
myText.setLocation(x, y);
myFrame.getContentPane().add(myText);
JComponent [http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html]
Related
I have a panel that I am drawing some stuff on and I want to have an interface on top of it. I created an interface as a JPanel on netbeans, visually. But interface is not displayed properly.
Here is my code
public static void main(String[] args) {
JFrame frame = new JFrame("WorldGen");
Interface inter = new Interface();
JLayeredPane lpane = new JLayeredPane();
frame.setPreferredSize(new Dimension(600, 400));
frame.setLayout(new BorderLayout());
frame.add(lpane, BorderLayout.CENTER);
lpane.setBounds(0, 0, 600, 400);
lpane.add(panel, new Integer(0), 0);
lpane.add(inter, new Integer(1), 0);
panel.setBounds(0,0,600,400);
frame.setSize(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main = new Main();
}
Panel is declared as a static JPanel.
static JPanel panel = new JPanel()
Here is my result:
This is the Interface class that is created in netbeans visually
When I add this line:
inter.setBounds(0,0,600,400);
inter.setOpaque(true);
this is what I get:
Just a blank screen. I don't expect it to be transparent since I set it to opaque myself but It seems I have another problem. The button is not showing whether I set it to opaque or not.
Why is the button not showing? I am hoping that the button will still be visible when I set opaque to false, after I resolve this problem.
I've solved it by creating a JFrame in netbeans visually, adding a JPanel to it. Then using that panel(by overriding the paint method) to draw my image.
I'm doing a mini-program in Java, that would manage files in the computer.
I tried put the JFileChooser.showSaveDialog() in the middle of the frame, by creating a panel and put it in the middle of the frame :
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(150,150) );
JFileChooser chooseFile = new JFileChooser();
chooseFile.showSaveDialog(panel);
frame.getContentPane().add(BorderLayout.CENTER,panel);
frame.setSize(400,400);
frame.setVisible(true);
But actually, when the showSaveDialog() command happens, the frame becomes not visible.
Can I change it?
It is not that the JFrame is not visible, but rather it was never set to be visible. I assume you want the JFrame to be visible when the showSaveDialog() is called. Due to the lack of detail, this is what I have to offer:
javax.swing.JFrame frame = new javax.swing.JFrame();
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
javax.swing.JPanel panel = new javax.swing.JPanel();
panel.setPreferredSize(new java.awt.Dimension(150,150) );
javax.swing.JFileChooser chooseFile = new javax.swing.JFileChooser(); frame.getContentPane().add(java.awt.BorderLayout.CENTER,panel);
frame.setSize(400,400);
frame.setVisible(true);
chooseFile.showSaveDialog(panel);
EDIT: I reread your question and have the idea that you may want to place the JFileChooser on the panel for the question is ambiguous. You may have wanted this:
javax.swing.JFrame frame = new javax.swing.JFrame();
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
javax.swing.JPanel panel = new javax.swing.JPanel();
panel.setPreferredSize(new java.awt.Dimension(150,150) );
javax.swing.JFileChooser chooseFile = new javax.swing.JFileChooser(); frame.getContentPane().add(java.awt.BorderLayout.CENTER,panel);
chooseFile.setPreferredSize(new java.awt.Dimension(400, 400));
frame.setSize(400,440);
frame.setVisible(true);
panel.add(chooseFile);<br><br>
The save dialog FileChooser is a modal dialog. This means that when we call chooseFile.showSaveDialog(panel); the file dialog is given focus and the user cannot interact with the panel until the dialog is closed. This is usually the behavior we want.
If we want to create some sort of file manager, then maybe you want to try adding the JFileChooser to your panel instead. Since JFileChooser extends component, you can do this, but it requires a bit more code and more of understanding of swing. This SO question addresses adding a JFileChooser to JPanel.
JFrame frame1 = new JFrame("frame");
frame1.setVisible(true);
frame1.setPreferredSize(new Dimension(800, 600));
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel Panel1 = new JPanel();
JPanel Panel2 = new JPanel();
frame1.add(Panel1,BorderLayout.SOUTH);
frame1.add(Panel2,BorderLayout.North);
How do i do something like this that if something happens the frame is blank
if(SOMETHINGHAPPENS)
{
//remove all panels from frame 1 so i have a blank frame
//now i want to add some new panels
}
Simple answer, don't.
Instead, use a CardLayout on the JFrame
This will allow you to setup a series of "views" which you can switch between as needed
Try this. Jframe.remove(JPanel). Then call JFrame.pack()
, and possibly JFrame.repaint() in order to refresh the graphics if necessary.
JPanel extends JComponent, so it should work just fine. Remember to substitute JFrame and JPanel in the code above with the names of the frames/panels you are using the methods on.
I am trying to make a java game. And I am confronted with a problem:
public void setUp () {
JFrame frame = new JFrame ("Key test");
//MyDrawPanel4 dp4 = new MyDrawPanel4();
//frame.setContentPane(dp4);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible (true);
JPanel p = new JPanel ();
p.setLayout(new BorderLayout());//why this sentence is necessary
p.addKeyListener (this);
p.add (dp);
frame.getContentPane().add(p);
frame.pack();
}
And the result is a small snack.
I want to use dp4 as the backgound of game. But what actually happen is
So my question is :
1.Why this happen?
2.How to make background of game?
Thanks in advance.
Edit: what I really mean is the two black body of the snack disappear, and it cannot move when I press the key (I use the key listener to fulfill it).
JFrame frame=new JFrame("arjun");
JPanel panel=new MyDrawPanel();
panel.setSize(100,100);
image=new ImageIcon("C:/raptor.jpeg").getImage(); // use this
frame.getContentPane().add(BorderLayout.CENTER,panel);
frame.setSize(300,300);
frame.setVisible(true);
JFrame frame = new JFrame ("Key test");
//MyDrawPanel4 dp4 = new MyDrawPanel4();
//frame.setContentPane(dp4);
-->FIRST
**Panel p = new Panel ();
p.setLayout(new BorderLayout());//it's a container
p.addKeyListener (this);
p.add (dp);
frame.getContentPane().add(p);**
-->AFTER
frame.setSize (300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible (true);
The image that you try to load is too large.
When you load an image, you MUST resize it to Image (or simile) control sizes.
i have two Panels and want them to be shown in my JFrame, but when i try it like this, i can only see the second one. Can someone please help me? :(
import javax.swing.JFrame;
public class MainWindow {
CardLayout layout;
JFrame frame;
Player panel1;
Block panel2;
public MainWindow() {
frame = new JFrame("Rechteck");
panel1 = new Player();
panel2 = new Block();
panel1.addKeyListener(new KeyListen(panel1));
frame.add(panel1);
frame.add(panel2);
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}}
You have added both your panels to the BorderLayout.CENTER of your JFrame—only one can occupy that location,. This will be the last one added, panel2 in this case.
To allow the panels occupy the space evenly, you could use GridLayout:
frame.setLayout(new GridLayout(2, 1));
Aside: Better to use Key Bindings when registering key events for components in Swing.
Create a JPanel add that to the JFrame. Add panel1 and panel2 to the new panel. JFrame can only have one child, usually set by calling JFrame.setContentPane().