So I have this weird thing happening to one of my JFrames
in my Java app, it seems like after I reopen the layout the positioning of my components just change out of no where. I'm used to work with springLayout
to position my components, and out of about 12 frames in my application this is the first time I see something like this happening.
This is my code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Edits extends JFrame implements ActionListener{
//change price
JLabel currentPrice = new JLabel("Current Price");
JLabel changePrice = new JLabel("New Price");
JFormattedTextField oldprice = new JFormattedTextField(new DecimalFormat("#0.000"));
JFormattedTextField newprice = new JFormattedTextField(new DecimalFormat("#0.000"));
JButton okPrice = new JButton("Save Changes");
JButton closePrice = new JButton("Close");
//change user info
JLabel currentUser = new JLabel();
JLabel changeUser = new JLabel();
JLabel currentPass = new JLabel();
JLabel changePass = new JLabel();
JLabel confirmPass = new JLabel();
JTextField oldUser = new JTextField();
JTextField newUser = new JTextField();
JPasswordField oldPass = new JPasswordField();
JPasswordField newPass = new JPasswordField();
JPasswordField confirmNewPass = new JPasswordField();
JButton okUser = new JButton("Save Changes");
JButton closeUser = new JButton("Close");
//change purchased quantity
JLabel currentQte = new JLabel();
JLabel changeQte = new JLabel();
JFormattedTextField oldQte = new JFormattedTextField(NumberFormat.getIntegerInstance());
JFormattedTextField newQte = new JFormattedTextField(NumberFormat.getIntegerInstance());
JButton okQte = new JButton("Save Changes");
JButton closeQte = new JButton("Close");
public Edits() {
super();
}
public void changePrice() {
//init frame
this.setTitle("Chnage Medicine's Price");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JFrame.setDefaultLookAndFeelDecorated(false);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dim.width/6-this.getSize().width/2, dim.height/16-this.getSize().height/2);
this.setSize(350, 170);
this.setResizable(false);
this.setVisible(true);
//init sizes
Dimension labels = new Dimension(100,25);
Dimension inputs = new Dimension(200,25);
Dimension btn = new Dimension(80,35);
Dimension btn1 = new Dimension(120,35);
//Labels size
currentPrice.setSize(labels);
currentPrice.setMaximumSize(labels);
currentPrice.setMinimumSize(labels);
currentPrice.setPreferredSize(labels);
changePrice.setSize(labels);
changePrice.setMaximumSize(labels);
changePrice.setMinimumSize(labels);
changePrice.setPreferredSize(labels);
//inputs size
oldprice.setSize(inputs);
oldprice.setMaximumSize(inputs);
oldprice.setMinimumSize(inputs);
oldprice.setPreferredSize(inputs);
newprice.setSize(inputs);
newprice.setMaximumSize(inputs);
newprice.setMinimumSize(inputs);
newprice.setPreferredSize(inputs);
//button sizes
okPrice.setSize(btn1);
okPrice.setMaximumSize(btn1);
okPrice.setMinimumSize(btn1);
okPrice.setPreferredSize(btn1);
closePrice.setSize(btn);
closePrice.setMaximumSize(btn);
closePrice.setMinimumSize(btn);
closePrice.setPreferredSize(btn);
this.add(currentPrice);
this.add(oldprice);
this.add(changePrice);
this.add(newprice);
this.add(okPrice);
this.add(closePrice);
//set layout
SpringLayout layout = new SpringLayout();
this.setLayout(layout);
//old label
layout.putConstraint(SpringLayout.NORTH, currentPrice, 15, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, currentPrice, 20, SpringLayout.WEST, this);
//old input
layout.putConstraint(SpringLayout.NORTH, oldprice, 15, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, oldprice, 10, SpringLayout.EAST, currentPrice);
//new label
layout.putConstraint(SpringLayout.NORTH, changePrice, 15, SpringLayout.SOUTH, currentPrice);
layout.putConstraint(SpringLayout.WEST, changePrice, 20, SpringLayout.WEST, this);
//new input
layout.putConstraint(SpringLayout.NORTH, newprice, 15, SpringLayout.SOUTH, currentPrice);
layout.putConstraint(SpringLayout.WEST, newprice, 10, SpringLayout.EAST, changePrice);
//OK
layout.putConstraint(SpringLayout.NORTH, okPrice, 15, SpringLayout.SOUTH, newprice);
layout.putConstraint(SpringLayout.WEST, okPrice, 65, SpringLayout.WEST, this);
//close
layout.putConstraint(SpringLayout.NORTH, closePrice, 15, SpringLayout.SOUTH, newprice);
layout.putConstraint(SpringLayout.WEST, closePrice, 20, SpringLayout.EAST, okPrice);
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
This is an image of the layout I want:
And this is what keeps happening:
Does anyone know what's wrong?