java,怎么设置label的位置和大小?
4个回答
展开全部
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Choice extends JFrame{
private JRadioButton button1 = null;
private JRadioButton button2 = null;
private JTextField field1 = null;
private JTextField field2 = null;
public Choice(){
setSize(500,500);
setLocation(100,50);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(getPanel());
setVisible(true);
}
private Container getPanel() {
JPanel panel = new JPanel();
//控制比如按钮的位置的话,把按钮所在的面板(按钮添加到哪里,哪里的设置为null)布局设置为null,
panel.setLayout(null);
button1 = new JRadioButton("A");
button2 = new JRadioButton("B");
ButtonGroup group = new ButtonGroup();
field1 = new JTextField();
field2 = new JTextField();
group.add(button1);
group.add(button2);
//给按钮添加点击事件的监听
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
field1.setText("选择了:A");
field2.setText("选择了:A");
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
field1.setText("选择了:B");
field2.setText("选择了:B");
}
});
//设置按钮在面板的为主,第一是左上角的x坐标,第二个是左上角y左边,第三个是控件宽度,第四个是高度
button1.setBounds(60, 10, 40,40);
button2.setBounds(100, 10, 40,40);
field1.setBounds(60, 50, 80,20);
field2.setBounds(60, 80, 80,20);
panel.add(button2);
panel.add(button1);
panel.add(field2);
panel.add(field1);
return panel;
}
public static void main(String[] args) {
new Choice();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Choice extends JFrame{
private JRadioButton button1 = null;
private JRadioButton button2 = null;
private JTextField field1 = null;
private JTextField field2 = null;
public Choice(){
setSize(500,500);
setLocation(100,50);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(getPanel());
setVisible(true);
}
private Container getPanel() {
JPanel panel = new JPanel();
//控制比如按钮的位置的话,把按钮所在的面板(按钮添加到哪里,哪里的设置为null)布局设置为null,
panel.setLayout(null);
button1 = new JRadioButton("A");
button2 = new JRadioButton("B");
ButtonGroup group = new ButtonGroup();
field1 = new JTextField();
field2 = new JTextField();
group.add(button1);
group.add(button2);
//给按钮添加点击事件的监听
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
field1.setText("选择了:A");
field2.setText("选择了:A");
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
field1.setText("选择了:B");
field2.setText("选择了:B");
}
});
//设置按钮在面板的为主,第一是左上角的x坐标,第二个是左上角y左边,第三个是控件宽度,第四个是高度
button1.setBounds(60, 10, 40,40);
button2.setBounds(100, 10, 40,40);
field1.setBounds(60, 50, 80,20);
field2.setBounds(60, 80, 80,20);
panel.add(button2);
panel.add(button1);
panel.add(field2);
panel.add(field1);
return panel;
}
public static void main(String[] args) {
new Choice();
}
}
展开全部
可以绝对定位,大小调整宽和高
追问
能不能具体点哦,谢谢
追答
就是用样式制定它的坐标,
#box_relative {
position: absolute;
left: 30px;
top: 20px;
}
这里的left和top 就指定了位置
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
setLocation. 和setSize
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
位置可以通过div来控制
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询