Here's my JPanel. The first button is always visible, but the secound is visible only when you place a cursour on it. Where the problem can be?
P.S. Please use simple english, if u can, because I don't speak english well
public class GamePanel extends JPanel implements KeyListener{
GamePanel(){
setLayout(null);
}
public void paint(Graphics g){
JButton buttonShip1 = new JButton();
buttonShip1.setLocation(10, 45);
buttonShip1.setSize(40, 40);
buttonShip1.setVisible(true);
add(buttonShip1);
JButton buttonShip2 = new JButton();
buttonShip2.setLocation(110, 145);
buttonShip2.setSize(440, 440);
buttonShip2.setVisible(true);
add(buttonShip2);
}
}
If you want to avoid problems and learn Java Swing correctly, go check out their tutorials here.
There are too many problems to discuss here, so I'll try to keep it simple.
You're using a null layout. null layouts are avoided for the most part because there is usually a layout that does exactly what you want. It takes some time to get it working, but there are some defaults in this tutorial that are fairly simple to use. There are some nice pictures there that show you what you can do with each layout, too. If you use a layout manager, you generally don't need to use setLocation, setSize or setVisible on most components like JButtons.
You're calling the paint method in a Swing application. You want to call paintComponent because you're using Swing and not Awt. You also need to call the super.paintComponent(g) method on the first line of the paintComponent method in order to correctly override the other paintComponent method.
The paint/paintComponent related methods are called very often. You don't want to create/initialize objects in them. The paint/paintComponent methods aren't a one time method like they may sound. They're continuously called and you should design your GUI around this. Design your paint-related methods to be event-driven rather than sequential. In other words, program the paintComponent method with the mindset that your GUI is reacting to things continuously rather than running in sequential order like a normal program. That's a very basic approach to it and hopefully doesn't confuse you, but if you go check out that tutorial you'll see what I mean eventually.
There are two basic types of GUIs in Java. One is Swing and the
other is Awt. Check out this answer on stackoverflow for a
nice description of the two.
Here is an example of what two buttons on a JPanel look like.
public class Test
{
public static void main(String[] args)
{
JFrame jframe = new JFrame();
GamePanel gp = new GamePanel();
jframe.setContentPane(gp);
jframe.setVisible(true);
jframe.setSize(500,500);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static class GamePanel extends JPanel{
GamePanel() {
JButton buttonShip1 = new JButton("Button number 1");
JButton buttonShip2 = new JButton("Button number 2");
add(buttonShip1);
add(buttonShip2);
//setLayout(null);
//if you don't use a layout manager and don't set it to null
//it will automatically set it to a FlowLayout.
}
public void paintComponent(Graphics g){
super.paintComponent(g);
// ... add stuff here later
// when you've read more about Swing and
// painting in Swing applications
}
}
}
Don't use a null layout
Don't create components in a painting method. The paint() method is for custom painting only. There is no need for you to override the paint() method.
Read the section from the Swing tutorial How to Use FlowLayout for a simple example that uses buttons and a layout manager.
Related
I have a problem, I set the size for frame, but doesn't work. I found a topic about this, but it's not helpful for me...
I tried with getContentPane().getSize(500,500), but nothing.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Mere extends JFrame{
private JPanel b;
public Mere(String titlu){
setSize(500,500);
setLayout(null);
this.getSmth();
pack();
show();
}
public void getSmth(){
JLabel user=new JLabel("User");
user.setBounds(10,10,80,25);
getContentPane().add(user);
JTextField userText=new JTextField(20);
userText.setBounds(100,10,160,25);
getContentPane().add(userText);
JPasswordField pas=new JPasswordField(20);
pas.setBounds(100,40,160,25);
getContentPane().add(pas);
JButton n=new JButton("Register");
n.setBounds(180,80,100,30);
getContentPane().add(n);
JLabel pass=new JLabel("Pass");
pass.setBounds(10,40,100,25);
getContentPane().add(pass);
getContentPane().setVisible(true);
}
public static void main(String[]args){
new Mere("yas");
}
}
You're calling pack(); after calling setSize().
From the Oracle docs
Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. The resulting width and height of the window are automatically enlarged if either of dimensions is less than the minimum size as specified by the previous call to the setMinimumSize method.
So you rather override the getPreferredSize() method or remove the pack() call. See Should I avoid the use of setPreferred/Maximum/MinimumSize? (YES)
However the best practice would be to use a layout manager (as explanined below) and then call pack() and let the manager do it's job while calculating the preferredSize for you
Also you're using a null layout. Swing was designed to work with different PLAFs and screen sizes and resolutions, while pixel perfect GUIs might seem like the best and faster approach to make a complex GUI the more you advance in this the more problems you'll have in maintain it due to this, so go ahead and use a proper Layout Manager or combinations of them. See Null Layout is Evil and Why is it frowned upon to use a null layout in Java Swing?
Another thing is that you're not placing your program on the Event Dispatch Thread (EDT), see SwingUtilities.invokeLater() why is it needed? and this answer for an example on how to use it
Another thing I see is that you're extening JFrame which translated to english it says that your class is a JFrame, JFrame isn't flexible when you need to add it to another Component, instead you should create a JFrame instance and if you really need to extend something, extend from a JPanel.
I'm doing a program that is composed by multiple panels in a JFrame.
I need to do every elements in differents classes (It's because in my school, we need to have every elements separeated in different classes for clean code), but every example that I see with my kind of problem, they do everything in one class.
And I think that my problem comes from having multile classes so I show you my classes.
I have a panel in wich I need to put 2 panel, here is the code :
public class Inscription extends JPanel{
private PanneauBoutons panneauBoutons = new PanneauBoutons();
private PanneauFormulaire panneauFormulaire = new PanneauFormulaire();
public Inscription(){
this.setLayout(new BorderLayout());
this.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
this.add(panneauFormulaire,BorderLayout.CENTER);
this.add(panneauBoutons,BorderLayout.SOUTH);
this.setVisible(true);
}
}
And here is the Panel panneauFormulaire :
public class PanneauFormulaire extends JPanel{
private JLabel labelMatricule;
private JTextField zoneTexteMatricule;
public PanneauFormulaire(){
this.setLayout(new GridLayout(8,2,10,10));
this.setBorder(BorderFactory.createLineBorder(Color.black));
labelMatricule = new JLabel("Matricule : ");
this.add(labelMatricule);
zoneTexteMatricule = new JTextField(30);
this.add(zoneTexteMatricule);
this.setVisible(true);
}
So the problem Inscription don't appear on the main Frame if I don't do setBounds, but I want a BorderLayout...
(I tested and with a set bounds I can see the borders, so I think that it means the panel are really added to the Frame so why without setBounds I see anything?).
And the other problem is that the panel PanneauFormulaire don't appear on the Inscription panel...
So if I miss something, can you help me? thank you
And here it is the class that extends JFrame :
public class FenetrePrincipale extends JFrame {
private Container cont;
private Inscription inscriptionForm;
public FenetrePrincipale(){
super("IESN");
setBounds(100,100,1200,960);
getContentPane().setLayout(null);
setLocationRelativeTo(null);
setResizable(false);
...
inscription.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
cont.removeAll();
inscriptionForm = new Inscription();
inscriptionForm.setOpaque(true);
cont.add(inscriptionForm);
invalidate();
repaint();
}
});
You should NOT be using a null layout and setBounds(). Swing was designed to be used with layout managers.
but when I click on an option in the menu, the current panel need to be change by another one,
Then you should be using a CardLayout.
Read the section from the Swing tutorial on How to Use CardLayout for working examples. So download the example and use it as the starting point of your project. The code will be better structured then what you currently have and it is easier to change working code than it is to fix broken code.
so why without setBounds I see anything?
That is because you set your layout to null in getContentPane().setLayout(null);.
Java containers comes with a default layout which you are allowed to set to a different one. How the components are arranged in the container are dependent on the layout you use. The layout will directly affects the location, alignment, spacing, dimension, preferredSize of the components.
However, if you choose not to use any layout (.setLayout(null)). Swing will not know how you want the components to be arranged, hence you see nothing in your content pane.
Since you wanted "absolute control" over the components, you will be expected to set the bounds (location and dimension) of each added component manually by yourself. This is why you are not seeing any components (even if you already added it) until you set the bounds for them.
Java, elements don't appear in a Panel with a GridLayout or FlowLayout, but with a setBounds they do
Every layout has their own characteristics and for some of them the order of your codes does makes a difference. Hence, I will advise you to go through what each layout can do for you. Then, depending on your needs, choose one (or a combination of a few) and study how to use it.
And here it is the class that extends JFrame :
You probably won't want to extends to a JFrame. You can always make a customized Container like JPanel and add it to the frame.
(Why would you want to paint your paintings on a frame instead of a piece of paper?)
I'm very new to Java but have some experience with C++. This is a homework assignment so I'm really just looking for someone to point me in the right direction.
The assignment requires a JFrame with JPanel objects displaying every card in a deck in a 13x4 grid. The Professor has supplied us with some code to get us started:
import javax.swing.*;
import java.awt.*;
public class Main
public static void main(String[] args)
{
//load the card image from the gif file.
final ImageIcon cardIcon = new ImageIcon("cardImages/tenClubs.gif");
//create a panel displaying the card image
JPanel panel = new JPanel()
{
//paintComponent is called automatically by the JRE whenever
//the panel needs to be drawn or redrawn
public void paintComponent(Graphics g) {
super.paintComponent(g);
cardIcon.paintIcon(this, g, 20, 20);
}
};
//create & make visible a JFrame to contain the panel
JFrame window = new JFrame("Title goes here");
window.add(panel);
window.setPreferredSize(new Dimension(200,200));
window.pack();
window.setVisible(true);
}
}
I have tried out a few things, but I can't seem to get multiple panels to display. Should I use a gridLayout() feature? or just create multiple panels and specify each one's location in the frame?
Again if someone can just point me in the right direction that would be awesome.
For displaying elements at the same size, evenly distributed within the container, then yes, GridLayout would be a good choice.
If you need to display the components in the grid at there preferred size (which may be different for each component) then GridBagLayout would be a better choice
If the code was supplied by a your professor, then you need to go back and make them fix it.
Firstly, a JLabel would be easier and provide better support for what you are trying to achieve...
Secondly, because the JPanel doesn't override getPreferredSize, most of the layout managers will set the size of the component to 0x0
There is a way to display multiple JPanels in one JFrame. Unlucky you the way is not so easy. Java has many diffrent LayoutManagers.
For your purpose I would recommend GridBagLayout, it is more complex, but definately the thing you need.
Here is a good tutorial, which helped me to understand it:
GridBagLayout
Hope it is a help.
I have a hybrid swing application which can be run as applet or java application with the following structure:
public class Gui extends JApplet {
public void init() {...}
public void paint(Graphics g) {
...
g.drawImage(image, 0, 0, this);
}
public static void main(String args[]) {
JFrame frame = new JFrame();
JDialog dialog = new JDialog(frame);
Gui gui = new Gui();
gui.init();
gui.start();
dialog.add("Center", gui);
...
}
}
The whole gui consists of one drawing area which is continously updated.
I would like to embed a panel which is always on top and independent of the underlying drawing process.
I tried using JLayeredPane and add it between JDialog and JApplet, but Japplet cant be added to the Pane because it is a top level container.
I also tried realising it with the glasspane but no success at all.
Is there a solution without refactoring to much since the structure should be kept as far as possible.
Don't override paint() of a top level container, like JApplet or JFrame. Custom painting is done by overriding the paintComponent() method of a JPanel (or JComponent). Then you add the panel to the frame.
dialog.add("Center", gui);
The is not the way to add a component to a panel. Read the Container API to find the proper add(...) method to use. Also don't hardcode string values. Every layout manager contains variables that can be used as the constraint values.
I would like to embed a panel which is always on top and independent of the underlying drawing process.
Not sure this makes sense. If the panel is always on top, then it would cover the painting.
I tried using JLayeredPane
That sounds like the proper approach. You add the layered pane to the frame or applet. Then you can have a background painting panel and another transparent panel on top. Read the Swing tutorial on Using Layered Panes for a working example.
My question boils down to this: is it standard structure in Swing programming to give listeners control over new components (e.g a new JPanel) for display and input, and to give that new component's listeners control over new components for display and input, and so on to infinity? Or does Java need to revert back to some sort of unifying class that ties all Swing components together in a procedural order?
At present, in my application that uses one JFrame only, in my listeners, my initial JFrame object is being passed as a parameter to all my JPanels so their listeners can call removeall() to clear the frame for a new JPanel. For example, short code as follows
public class MainFrame {
JFrame jfrm;
public MainFrame() {
jfrm = new JFrame("Main Frame");
JPanel mainPanel = new MainPanel(jfrm);
}
}
public class MainPanel extends JPanel {
public MainPanel(final JFrame mainFrame) {
JButton example = new JButton("Example");
example.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
mainFrame.removeall();
JPanel 2ndPanel = new 2ndPanel(mainFrame);
mainFrame.add(2ndPanel);
mainFrame.validate();
}
});
}
}
Is this the right structure - where it's the listeners that generate the new panels and not some unifying class? But if that's the case, how does Java's compiler ever get to mainFrame.validate() if there's a cascading infinity of listeners? I'm an old-school procedural programmer trying to program a Swing application in Java, and I reckon I might not have grasped the basic concepts of Swing programming. Look forward to any helpful answers, and thanks in advance!
I wouldn't do it like that. The Observer pattern is used in Swing to send notifications, usually as a result of an user action.
Take your example: The user 'clicks' on the button because he wants a new panel in MainFrame. But the button doesn't know what to do with a click. All he can do is notify event listeners, that it has been selected.
So we need some component that is interested in notifications. This component can register a listener with the button and receive notifications. The listener is this other components 'ear' (or it's 'eye'). But an ear (or eye) will not take action. It is just the other components sensor.
Which takes us back to the main question: who wants to be informed, if the button is clicked and who has to take action. This is a major design question. It's definitly not the listener that creates and adds a panel to main frame. It could be the main frames role to create and add sub panels or the role of a third component, which is responsible of creating a frame with sub panels.
But the listner is a bad place for this code.
Instead of destroying and creating panels you might want to look at hiding/showing them. There is a CardLayout layout manager that can do this: http://journals.ecs.soton.ac.uk/java/tutorial/ui/layout/card.html
Basically the idea is build and add all your panels at the start of your prog, then use the layout manager to flip between views.
As said by others, you might want to add some sort of model to your design, which is responsible for maintaining the state of the system.
First of all you shouldn't let the listener manipulate the frame directly, use a nested JPanel or it's ContentPane.
Your question depends on where your listener is located. You should only add components to a JFrame from the class itself. In your case it's OK, since the logic is confined to the class itself.
I don't think there is an infinity of listeners. They are in a list and are executed sequentially. Your listener should use SwingUtilities.invokeLater for manipulating the main frame.
I find the passing of the JFrame as an argument a little redundant. Why not create the listener from outside the constructor?
final JPanel mainPanel = new MainPanel();
JButton example = new JButton("Example");
example.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MainFrame.this.removeAll();
MainFrame.this.add(mainPanel);
}
});;
}
});
add(example);
It's not the best of examples, but I am note sure what you are trying to do.
The bottom line is you should create the components outside the listener, and then add it using the listener.
There is some confusion by the, not unusual, way you have organised your code. As a general rule of Java, don't subclass where you have no reason to. It rarely makes sense to subclass JPanel or JFrame.
It also rarely makes sense to assign components to fields in the class that creates them. In fact, do less in constructors.
Also do less in anonymous inner classes. Detangle the event data and call a method that makes sense for the enclosing class to have as an operation.