所以我在我的Java应用程序中发现了JFrames
之一的这个奇怪的事情,似乎在我重新打开布局后,我的组件的定位只是改变了。我习惯使用springLayout
来定位我的组件,在我的应用程序中大约12帧,这是我第一次看到这样的事情发生。
这是我的代码:
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) {
}
}
这是我想要的布局图片:
这就是持续发生的事情:
有谁知道什么是错的?