java 按钮大小的设置与显示
通过输入随意数值改变JButton的TopBottomLeftRight四个属性来改变按钮边界大小可以即刻Button随数值变化而变化求完整代码...
通过输入随意数值改变JButton的 Top Bottom Left Right 四个属性 来改变按钮边界大小
可以即刻Button随数值变化而变化
求 完整代码 展开
可以即刻Button随数值变化而变化
求 完整代码 展开
2个回答
展开全部
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);// 窗口不可变
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边界
}
}
}
追问
大体已完成 只剩下细节方面修改
膜拜大神 谢了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询