2016.3.11(Java图形用户界面)

Posted 稳重的橙子

tags:

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

Swing程序的建立步骤

1 建立容器
2 建立组件
3 将组建添加都容器
4 设置布局

public class SimpleFrame extends JFrame {

private int width;
private int height;
private Container contentP;
private JLabel usernameLabel;
private JLabel passwordLabel;
private JTextField usernameField;
private JPasswordField passwordField;
private JRadioButton maleBtn;
private JRadioButton femaleBtn;
private JCheckBox hobbyCheckBox1;
private JCheckBox hobbyCheckBox2;
private JCheckBox hobbyCheckBox3;
private JComboBox addressCombBox;
private JTextArea describeTextArea;
private JButton submitBtn;
private JButton cancelBtn;

public SimpleFrame() {
this.width = 1024;
this.height = 600;
this.setTitle("我的第一个GUI窗体");
this.setSize(this.width, this.height);
Toolkit tool = Toolkit.getDefaultToolkit();
double width = tool.getScreenSize().getWidth();
double height = tool.getScreenSize().getHeight();
this.setLocation((int) (width - this.width) / 2,
(int) (height - this.height) / 2);

this.setIconImage(tool.createImage("img/logo.png"));

// this.setLocationRelativeTo(null);//设置窗体居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.addContent();

this.setVisible(true);
}

public void addContent() {
this.contentP = this.getContentPane();
this.contentP.setLayout(null);

//文本 和 输入框
this.usernameLabel = new JLabel("用户名");
this.usernameLabel.setBounds(350, 10, 90,35);
this.usernameLabel.setFont(new Font("宋体", Font.PLAIN, 16));
this.contentP.add(this.usernameLabel);//添加文本组件

this.usernameField = new JTextField();
this.usernameField.setBounds(450, 10, 200,35);
this.contentP.add(this.usernameField);

this.passwordLabel = new JLabel("密 码");
this.passwordLabel.setBounds(350, 60, 90,35);
this.passwordLabel.setFont(new Font("宋体", Font.PLAIN, 16));
this.contentP.add(this.passwordLabel);//添加文本组件

//密码框
this.passwordField = new JPasswordField();
this.passwordField.setBounds(450, 60, 200,35);
this.passwordField.setEchoChar(‘*‘);
// this.passwordField.setForeground(Color.RED);
this.contentP.add(this.passwordField);


//单选框
JLabel genderTitle = new JLabel("性别");
genderTitle.setBounds(350, 110, 80, 30);
genderTitle.setFont(new Font("宋体", Font.PLAIN, 16));
this.contentP.add(genderTitle);

this.maleBtn = new JRadioButton("男");
this.maleBtn.setBounds(440, 110, 50,35);
this.contentP.add(this.maleBtn);
this.femaleBtn = new JRadioButton("女");
this.femaleBtn.setBounds(500, 110, 50,35);
this.contentP.add(this.femaleBtn);
ButtonGroup bp = new ButtonGroup();
bp.add(this.maleBtn);
bp.add(this.femaleBtn);

//复选框
this.hobbyCheckBox1 = new JCheckBox("运动");
this.hobbyCheckBox2 = new JCheckBox("电影");
this.hobbyCheckBox3 = new JCheckBox("音乐");
this.hobbyCheckBox1.setBounds(350, 160, 80,35);
this.hobbyCheckBox2.setBounds(440, 160, 80,35);
this.hobbyCheckBox3.setBounds(540, 160, 80,35);

this.contentP.add(this.hobbyCheckBox1);
this.contentP.add(this.hobbyCheckBox2);
this.contentP.add(this.hobbyCheckBox3);

//下拉框
this.addressCombBox = new JComboBox(new String[]{"成都", "德阳", "绵阳", "乐山", "资阳"});
this.addressCombBox.addItem("达州");
this.addressCombBox.addItem("自贡");
this.addressCombBox.setSelectedIndex(5);
this.addressCombBox.setBounds(350, 210, 120, 25);
this.contentP.add(this.addressCombBox);

//文本域
this.describeTextArea = new JTextArea();
this.describeTextArea.setBounds(350, 250, 300, 100);
// this.describeTextArea.setColumns(5);
// this.describeTextArea.setRows(3);
this.describeTextArea.setBorder(BorderFactory.createLineBorder(Color.BLACK));
this.contentP.add(this.describeTextArea);

//按钮
this.submitBtn = new JButton("确定");
this.submitBtn.setBounds(400, 400, 80, 30);
this.contentP.add(this.submitBtn);

this.cancelBtn = new JButton("取消");
this.cancelBtn.setBounds(500, 400, 80, 30);
this.contentP.add(this.cancelBtn);

}

以上是关于2016.3.11(Java图形用户界面)的主要内容,如果未能解决你的问题,请参考以下文章

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

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

JAVA的图形用户界面代码

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

Java编写一个图形界面

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