作者:电脑信息网日期:
返回目录:设置问题
布局方式的问题,你换一种而已方式看看,别用borderLayout。
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSpinner;
public class ChangeBoundsDemo extends JFrame implements ActionListener {
private JButton changeButton = new JButton("jButton1");
private JButton submintButton = new JButton("OK");
private JButton cancelButton = new JButton("取消");
private JLabel topLabel = new JLabel("Top");
private JLabel rightLabel = new JLabel("Right");
private JLabel leftLabel = new JLabel("Left");
private JLabel bottomLabel = new JLabel("Bottom");
private JSpinner topJSpinner = new JSpinner();
private JSpinner rightJSpinner = new JSpinner();
private JSpinner leftJSpinner = new JSpinner();
private JSpinner bottomJSpinner = new JSpinner();
private Integer left, right, top, bottom;
public ChangeBoundsDemo() {
super("");// 设置窗口标题
try {
init();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new ChangeBoundsDemo();
}
private void init() {
this.setSize(480, 330);
Container c = getContentPane();
changeButton.setBounds(150, 104, 100, 22);
topLabel.setBounds(420, 20, 50, 20);
topJSpinner.setBounds(420, 42, 40, 20);
rightLabel.setBounds(420, 82, 50, 20);
rightJSpinner.setBounds(420, 104, 40, 20);
leftLabel.setBounds(420, 144, 50, 20);
leftJSpinner.setBounds(420, 166, 40, 20);
bottomLabel.setBounds(420, 206, 50, 20);
bottomJSpinner.setBounds(420, 228, 40, 20);
submintButton.setBounds(20, 260, 55, 22);
cancelButton.setBounds(300, 260, 70, 22);
submintButton.addActionListener(this);
cancelButton.addActionListener(this);
c.add(cancelButton);
c.add(submintButton);
c.add(changeButton);
c.add(topLabel);
c.add(topJSpinner);
c.add(rightLabel);
c.add(rightJSpinner);
c.add(leftLabel);
c.add(leftJSpinner);
c.add(bottomLabel);
c.add(bottomJSpinner);
c.setLayout(null);// 不设置布局
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 窗口退出
this.setResizable(false);// 窗口不e69da5e6ba90e799bee5baa6e79fa5e98193330可变
this.setLocationRelativeTo(null);// 窗口居中显示
this.setVisible(true);// 显示窗体
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == submintButton) {// OK
left = (Integer) leftJSpinner.getValue();
top = (Integer) topJSpinner.getValue();
right = (Integer) rightJSpinner.getValue();
bottom = (Integer) bottomJSpinner.getValue();
changeButton.setBounds(left, top, left + right, top + bottom);// 设置Button边界
}
if (e.getSource() == cancelButton) {// 取消
changeButton.setBounds(150, 104, 100, 22);
topJSpinner.setValue(0);
leftJSpinner.setValue(0);
rightJSpinner.setValue(0);
bottomJSpinner.setValue(0);// 设置Button边界
}
}
}
package image;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Ex
{
public static void main ( String args[] )
{
e5a48de588b6e79fa5e98193334JFrame frame = new JFrame ("BUTTON");
JButton button = new JButton ("clieck me");
frame.setLayout (null);
button.setBounds (( 800 - 100 ) / 2, ( 600 - 20 ) / 2, 100, 20);
frame.setSize (800, 600);
frame.setLocation (300, 200);
frame.add (button);
frame.setLocationRelativeTo (null);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setVisible (true);
}
}