上次java 按钮大小的设置与显示的问题 100
可以运用类Insets(topleftrightbottom)来改变JButton边界输入topleftrightbottom四个值可以即刻显示JButton边界的变化来...
可以运用类 Insets(top left right bottom)来改变JButton 边界
输入top left right bottom四个值可以即刻显示JButton 边界的变化
来完成上述变化么?
http://zhidao.baidu.com/question/554356648?quesup2&oldq=1
题目 展开
输入top left right bottom四个值可以即刻显示JButton 边界的变化
来完成上述变化么?
http://zhidao.baidu.com/question/554356648?quesup2&oldq=1
题目 展开
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();
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
Insets insets = new Insets();
insets.setLeft((Integer) leftJSpinner.getValue());
insets.setTop((Integer) topJSpinner.getValue());
insets.setRight((Integer) rightJSpinner.getValue());
insets.setBottom((Integer) bottomJSpinner.getValue());
changeButton.setBounds(insets.getLeft(), insets.getTop(), insets.getLeft() + insets.getRight(), insets.getTop()
+ insets.getBottom());// 设置Button边界
}
if (e.getSource() == cancelButton) {// 取消
changeButton.setBounds(150, 104, 100, 22);
topJSpinner.setValue(0);
leftJSpinner.setValue(0);
rightJSpinner.setValue(0);
bottomJSpinner.setValue(0);// 设置Button边界
}
}
}
class Insets {
private int top, left, right, bottom;
public Insets() {
}
public Insets(int top, int left, int right, int bottom) {
this.top = top;
this.left = left;
this.right = right;
this.bottom = bottom;
}
public int getTop() {
return top;
}
public void setTop(int top) {
this.top = top;
}
public int getLeft() {
return left;
}
public void setLeft(int left) {
this.left = left;
}
public int getRight() {
return right;
}
public void setRight(int right) {
this.right = right;
}
public int getBottom() {
return bottom;
}
public void setBottom(int bottom) {
this.bottom = bottom;
}
}
来自:求助得到的回答
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询