图形用户界面(三)

1.面板也是一种容器,不是一个单独的窗口,它只是包含在窗口中的一个区域,必须将面板添加到窗体中。
2. 文本框
public class PanelDemo extends JFrame{
private JPanel panel;
private JButton button;
private void init(){
button=new JButton(“按钮”);
panel=new JPanel();//流式布局 居中对齐
panel.add(button);
panel.setBackground(Color.blue);
this.add(panel);
this.setResizable(false);
this.setTitle(“QQ登录”);
this.setSize(255,125);
this.setLocation(400,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public PanelDemo(){
init();}
}

public class Test{
public static void main(String[] args){
new PanelDemo();}}

3.屏幕中间位置
Dimension dim=getToolkit().getScreenSize();
int w=dim.width/2;
int h=dim.height/2;
this.setSize(255,125);
this.setLocation(w-255/2,h-125/2);
4.滚动条
public class PanelDemo extends JFrame{
private JPanel panel;
private JButton button;
private JTextArea area;
private JScrollPane scrollPane;
private void init(){
//显示文本,行数,列数
area=new JTextArea(“多行文本框”,5,20);
scrollPane=newJScrollPane(area);
this.setLayout(new FlowLayout());
this.add(scrollPane);
this.setTitle(“QQ登录”);
this.setSize(255,125);
this.setLocation(400,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public PanelDemo(){
init();}
}

public class Test{
public static void main(String[] args){
new PanelDemo();}}

5.多选框
public class PanelDemo extends JFrame{
private JPanel panel;
private JButton button;
private JTextArea area;
private JScrollPane scrollPane;
private JCheckBox box1,box2;
private void init(){
box1=new JCheckBox(“运动”);
box2=new JCheckBox(“读书”);
//多选按钮是否被选中
box1.isSelected();
//显示文本,行数,列数
area=new JTextArea(“多行文本框”,5,20)

猜你喜欢

转载自blog.csdn.net/weixin_45802395/article/details/106876290