package com.qiu.swing.layoutDemo;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.JTextField;
/**
*
* @author Qiu
*
*/
public class TextDemo extends JFrame{
final JButton button_show = new JButton("显示");
final JButton button_clear = new JButton("显示");
final JTextField text = new JTextField();
final Container con = this.getContentPane();
public TextDemo() {
this.setTitle("HelloWorld!");
this.setSize(300, 160);
// 居中
this.setLocationRelativeTo(null);
this.setUndecorated(true); // 去掉窗口的装饰
this.setResizable(false);
this.getRootPane().setWindowDecorationStyle(
JRootPane.INFORMATION_DIALOG);// 采用指定的窗口装饰风格
// 文字居中
text.setSize(100, 20);
Box vbox = Box.createVerticalBox();
Box xbox0 = Box.createHorizontalBox();
xbox0.add(text);
xbox0.add(button_show);
xbox0.add(button_clear);
vbox.add(xbox0);
vbox.add(Box.createVerticalStrut(100));
con.setLayout(new BoxLayout(con, BoxLayout.X_AXIS));
con.add(vbox);
button_show.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.setText("HelloWorld");
}
});
button_clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.setText("");
}
});
}
public static void main(String[] args) {
TextDemo home = new TextDemo();
home.setVisible(true);
}
}
温馨提示:答案为网友推荐,仅供参考