码迷,mamicode.com
首页 > 编程语言 > 详细

2016.3.16(Java图形用户界面)

时间:2016-03-17 00:40:40      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

边界布局

public class BorderLayoutTest extends JFrame{

public BorderLayoutTest(){
this.setSize(900,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("边界布局");
this.setLocationRelativeTo(null);//设置窗体居中

this.addContent();

this.setVisible(true);
}


public void addContent(){
Container con = this.getContentPane();
con.setLayout(new BorderLayout());

JButton btn1 = new JButton("确定1");
JButton btn2 = new JButton("确定2");
JButton btn3 = new JButton("确定3");
JButton btn4 = new JButton("确定4");
JButton btn5 = new JButton("确定5");

con.add(btn1, BorderLayout.EAST);
con.add(btn2, BorderLayout.WEST);
con.add(btn3, BorderLayout.NORTH);
con.add(btn4, BorderLayout.SOUTH);
con.add(btn5, BorderLayout.CENTER);
}


public static void main(String[] args) {
new BorderLayoutTest();
}
}

流布局

public class FlowLayoutTest extends JFrame{

public FlowLayoutTest(){
this.setSize(900,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("流布局");
this.setLocationRelativeTo(null);//设置窗体居中

this.addContent();

this.setVisible(true);
}


public void addContent(){
Container con = this.getContentPane();

con.setLayout(new FlowLayout());

JLabel label = new JLabel("Test");
JButton btn = new JButton("根据元素内容确定大小");
JButton btn2 = new JButton("确定");
JButton btn3 = new JButton("确定");
JButton btn4 = new JButton("确定");
JButton btn5 = new JButton("确定5");

con.add(label);
con.add(btn);
con.add(btn2);
con.add(btn3);
con.add(btn4);
con.add(btn5);
}


public static void main(String[] args) {
new FlowLayoutTest();
}

}
网络布局

public class GridLayoutTest extends JFrame {

public GridLayoutTest(){
this.setSize(900,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("网格布局");
this.setLocationRelativeTo(null);//设置窗体居中

this.addContent();

this.setVisible(true);
}


public void addContent(){
Container con = this.getContentPane();
con.setLayout(new GridLayout(2,3));

JButton btn1 = new JButton("确定1");
JButton btn2 = new JButton("确定2");
JButton btn3 = new JButton("确定3");
JButton btn4 = new JButton("确定4");
JButton btn5 = new JButton("确定5");
JButton btn6 = new JButton("确定6");
JButton btn7 = new JButton("确定7");
JButton btn8 = new JButton("确定8");
JButton btn9 = new JButton("确定9");

con.add(btn1);
con.add(btn2);
con.add(btn3);
con.add(btn4);
con.add(btn5);
con.add(btn6);
con.add(btn7);
con.add(btn8);
con.add(btn9);
}


public static void main(String[] args) {
new GridLayoutTest();
}

}

事件:
public class ActionListenerTest extends JFrame {

public ActionListenerTest(){
this.setSize(900,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("边界布局");
this.setLocationRelativeTo(null);//设置窗体居中

this.addContent();

this.setVisible(true);
}


private void addContent() {
Container con = this.getContentPane();
con.setLayout(new FlowLayout());

JButton btn1 = new JButton("红色");
JButton btn2 = new JButton("绿色");

// ButtonListener btLis = new ButtonListener(this);
// btn1.addActionListener(btLis);
// btn2.addActionListener(btLis);


btn1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
getContentPane().setBackground(Color.RED);
}});

btn2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
getContentPane().setBackground(Color.GREEN);
}});

con.add(btn1);
con.add(btn2);
}


// @Override
// public void actionPerformed(ActionEvent e) {
// System.out.println("进入");
// Object obj = e.getSource();
// JButton btn = (JButton)obj;
// String text = btn.getText();
//
// if(text.equals("红色")){
// this.getContentPane().setBackground(Color.RED);
// }
//
// if(text.equals("绿色")){
// this.getContentPane().setBackground(Color.GREEN);
// }
// }


public static void main(String[] args) {
new ActionListenerTest();

}

}

另一种方法添加事件 实现ActionListener

public class ButtonListener implements ActionListener {

private JFrame jf;

public ButtonListener() {

}

public ButtonListener(JFrame jf) {
this.jf = jf;
}

@Override
public void actionPerformed(ActionEvent e) {
System.out.println("进入123");
Object obj = e.getSource();
JButton btn = (JButton) obj;
String text = btn.getText();

if (text.equals("红色")) {
this.jf.getContentPane().setBackground(Color.RED);
}

if (text.equals("绿色")) {
this.jf.getContentPane().setBackground(Color.GREEN);
}
}

}

2016.3.16(Java图形用户界面)

标签:

原文地址:http://www.cnblogs.com/CMCM/p/5285682.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!