2016.3.16(Java图形用户界面)

Posted 稳重的橙子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2016.3.16(Java图形用户界面)相关的知识,希望对你有一定的参考价值。

边界布局

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图形用户界面)的主要内容,如果未能解决你的问题,请参考以下文章

java图形用户界面,本人代码效果如下,但是行间距有点大,按钮之类的组件过大,怎么解决?

java 图形界面设计制作计算器

JAVA的图形用户界面代码

凯撒密码java编程实现图形界面化代码

Java编写一个图形界面

Java的图形用户界面的基本工具