Eclipse bug doesn't respect the bounds set by GUI - java

So I've been having this little issue with this app that I'm developing for a school project and I believe my eclipse has bugged out, what happens is, whenever I set the bounds to something on the GUI it doesn't respect them and always puts whatever I implement on near the middle to the left of the GUI, if I implement more stuff it just moves it to a random place despite what bounds I set it to.
I don't really know how to fix this and I've tried deleting the class and writing the code all over again but it still happens, I've searched on google for any fixes but can't seem to find anyone with this problem, any help or insight into this issue would be much appreciated.
The issue
JFrame frame = new JFrame();
JLabel ArmaLabel = new JLabel();
JLabel BarC = new JLabel();
JLabel teste = new JLabel("teste");
ArmaP() {
BarC.setBounds(100,100,100,30);
BarC.setText("teste12");
teste.setBounds(400,300,20,20);
ArmaLabel.setBounds(325,20,200,35);
ArmaLabel.setText("Armazenamento");
ArmaLabel.setFont(new Font(null,Font.ROMAN_BASELINE,25));
frame.add(ArmaLabel);
frame.add(BarC);
frame.add(teste);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,600);
frame.setVisible(true);
frame.setLayout(null);
frame.setResizable(false);
}
}

Related

Adding a JPanel to a JFrame changes its size

I am trying to make a game, so I decided to create multiple JPanels, which all do something different, so only one JPanel is needed to render everything. To change them, I just do:
this.getContentPane().removeAll();
CURRENT_CONTENT = new JPanel();
this.getContentPane().add(CURRENT_CONTENT);
this.pack();
However, when I do this, although the JPanel is appropriately set, it also changes the size of the JFrame by slightly increasing it... How can I stop this from happening?
Thanks!
Thanks to the answers, I got it working!
pack() seemed to be the problem.
Here is the final result, in case someone has the same problem:
this.getContentPane().removeAll();
CURRENT_CONTENT = new JPanel();
this.getContentPane().add(CURRENT_CONTENT);
this.revalidate();
Thanks again!

JPanel button is not at the correct place

i made a custom JFrame for the desktop application and i added a JPanel on the very top of the app to serve as a subtitute of the title box. the problem is when i added a button it located right in the middle of the JPanel instead of the usual left top. AND it would not move even if i set it at a different location.
here is the code:
JFrame f = new JFrame("Hello");
f.setResizable(true);
JPanel pa = new JPanel();
JButton btn = new JButton("Exit");
btn.setBackground(Color.white);
btn.setText("Button");
btn.setSize(300, 80);
btn.setLocation(50, 0);
pa.setBackground(Color.red);
pa.setPreferredSize(new Dimension(width,60));
pa.add(btn);
f.setBackground(Color.white);
f.setUndecorated(true);
f.getContentPane().add(pa, BorderLayout.NORTH);
f.setSize(new Dimension(width,height));
f.setLocation(200, 200);
f.setVisible(true);
You use a BorderLayout in the frame. You can do the same thing in the panel.
pa.setLayout(new BorderLayout());
pa.add(btn, BorderLayout.WEST);
In general, setLocation tends to fight against the layout manager, so you usually don't want to use it unless you're going to position everything by hand.
that is one way to do it, but BorderLayout way is not very good way because i also want to add another button next to it.
Then what this might need is a FlowLayout using FlowLayout.LEADING as the alignment.
But as general tips:
Provide ASCII art or a simple drawing of the intended layout of the GUI (showing all components) at minimum size, and if resizable, with more width and height - to show how the extra space should be used.
For better help sooner, post a
Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example of your attempt.

Java Swing elements change every runtime

So I made a program using java's swing library. I made a program that graphs equations and here is the main method if it's relevant:
public static void main(String[] args) throws Exception {
JFrame frame= new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLayout(new GridLayout(1,2));
GraphPanel gp = new GraphPanel();
GraphPanel gp2 = new GraphPanel();
//gp.functs.add(new Function(Phrase.createPhrase("2(25-x^2)^(1/2)")));
//gp.functs.add(new Function(Phrase.createPhrase("-1.1((25-x^2)^(1/2))")));
gp.functs.add(new Function(Phrase.createPhrase("x^2")));
//gp.functs.add(new Function(Phrase.createPhrase("-4/x^2+6")));
gp2.functs.add(new Function(Phrase.createPhrase("sinx")));
frame.add(gp);
frame.add(gp2);
frame.pack();
frame.setSize(800, 800);
//gp.setBorder(BorderFactory.createLineBorder(Color.RED));
gp.setBounds(100, 100, 700, 700);//I WANT THIS TO ALWAYS RUN
}
I ran two trial runs of the program WITHOUT CHANGING ANY PART OF IT and this is what it looked like:
Then the next time i ran it:
If it's relevant, GraphPanel is of type JLabel.
I know that if i use a null absolute LayoutManager, it will always work. But I'm just wondering why swing has such inconsistencies in it. I've noticed stuff like this before but I though it was just some error in the program. Why is this?
Thanks in advance!
Start by moving frame.setVisible(true); to the end of the main method
This gp.setBounds(100, 100, 700, 700); is pointless, as gp is under the control of a layout manager (GridLayout)
There's no point in using both path and setSize, pack is generally a safer option, but that will depend on your components correctly overriding getPreferredSize
But I'm just wondering why swing has such inconsistencies in it. I've noticed stuff like this before but I though it was just some error in the program. Why is this?
Mostly because you're not using the API properly. It's possible, because of the way a JFrame is physically attached to a native peer, that the frame may or may not actually be visible on the screen when you reach gp.setBounds.
Also, because you're doing all your work from within the "main" thread and not the Event Dispatching Thread, you're running the risk of a race condition between them, see Initial Threads for more details.
Swing is VERY flexible, it's also unforgiving when you do the wrong things (or things the wrong way)

JFrame in netbeans not implementing

I'm trying to implement a new Jframe in net beans with
JFrame frame = new JFrame("shooter");
however when it is run nothing is happening. I have run the same code in another IDE and it works fine however I am not seeing a JFrame on the screen. I ran a test to see weather everything was compiling and that there was no syntactical or lexical error so nothing seems to be wrong per say.
Thanks.
It is difficult to provide an answer with the information you've provided, but I'll try my best. By instantiating a new JFrame object, you aren't making it appear on the screen.
An example of creating and showing a new frame would look something like this
JFrame frame = new JFrame("Title");
frame.setSize(500, 500); //This method will set the size of the frame to 500 by 500.
frame.setVisible(true); //This method makes the frame visible
I hope I could be of help.

Inconsistent results in Eclipse for Java Swing

I am teaching myself Java and I am reading "Java All in One Desk Reference For Dummies." I am currently using the code provided in the book to practice Swing. Here is the code I am using that comes from the book: `import javax.swing.*;
public class JavaBook6 extends JFrame
{
public static void main(String[] args)
{
new JavaBook6();
}
public JavaBook6()
{
this.setSize(400, 400);
this.setLocation(500, 0);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("Sample");
this.setVisible(true);
JPanel pnlMain = new JPanel();
JCheckBox chkMy = new JCheckBox("Save");
JButton btnMy = new JButton("Search");
JTextField txtMy = new JTextField(20);
pnlMain.add(chkMy);
pnlMain.add(txtMy);
pnlMain.add(btnMy);
this.add(pnlMain);
}
}
I seem to get inconsistent results when I press run. A window always shows up. However, sometimes the only thing displayed in the window is the Frame title and other times the components such as JCheckBox, JTextArea and JButton show up, as I would expect.
My question is why do the components show up sometimes and not others? I have tried using other components and get the same inconsistent results.
As I stated I am a beginner and I therefore have a very basic understanding of how java works, so please forgive me if the answer to my question is obvious.
I'm not too impressed with the text book:
The GUI should be created on the EDT. Read the section from the Swing tutorial on Concurrency for more information. I would also recommend you use the examples from the tutorials since they incorporate the suggestions from the tutorial.
Component must be added to the GUI before the setVisible( true ) method is invoked. (There are ways around this, but for now keep it simple and follow this rule).
You generally need to do...
this.pack();
before it will display everything.
I suspect if you resize the window, everything shows up?
Adding the pack() tells the layout manager to position and size all the components.
Also if you resize the window or force it to refresh in some way it will also display the components.

Categories