Java中GUI编程总结—Swing(窗口、面板、弹窗、标签、按钮、列表、文本框)
3.Swing3.1 窗口 面板//标签居中class Myframe2 extends JFrame{public void init(){this.setBounds(10,10,200,300);this.setVisible(true);JLabel lable=new JLabel("欢迎来到狂神");this.add(lable);//让文本标签居中,设置水平对齐labl.
·
3.Swing
3.1 窗口 面板
//标签居中
class Myframe2 extends JFrame{
public void init(){
this.setBounds(10,10,200,300);
this.setVisible(true);
JLabel lable=new JLabel("欢迎来到狂神");
this.add(lable);
//让文本标签居中,设置水平对齐
lable.setHorizontalAlignment(SwingConstants.CENTER);
//获得一个容器
Container container=this.getContentPane();
container.setBackground(Color.yellow);
}
}
//初始化
public class javaPractice {
//init();初始化
public void init(){
//顶级窗口
JFrame jf=new JFrame("这是一个JFrame窗口");
jf.setBounds(10,10,200,300);
jf.setVisible(true);
jf.setBackground(Color.blue);
JLabel lable=new JLabel("欢迎来到狂神");
jf.add(lable);
//关闭事件
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new javaPractice().init();
}
}
3.2 弹窗
public class javaPractice extends JFrame {
public javaPractice(){
this.setVisible(true);
this.setBounds(100,100,700,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放东西 容器
Container container=this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button=new JButton("点击弹出一个对话框");
button.setBounds(30,30,200,50);
//点击这个按钮的时候,弹出一个窗口
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialog();
}
});
container.add(button);
}
public static void main(String[] args) {
new javaPractice();
}
}
class MyDialog extends JDialog{
public MyDialog() {
this.setVisible(true);
this.setBounds(200,200,400,400);
//默认就有this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container=this.getContentPane();
container.setLayout(null);
container.add(new Label("学习java"));
}
}
3.3 标签
label
new TLabel("xxx")
图标 icon
//图标、需要实现的类,Frame继承
public class javaPractice extends JFrame implements Icon {
private int width;
private int height;
public javaPractice() {//无参构造
}
public javaPractice(int width,int height) {
this.width=width;
this.height=height;
}
public void init(){
javaPractice iconDemo=new javaPractice(15,15);
//图标放在标签上,也可以放在按钮上!
JLabel label =new JLabel("icontest",iconDemo,SwingConstants.CENTER);
Container container=getContentPane();
container.add(label);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new javaPractice().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
图片 icon
public class javaPractice extends JFrame {
public javaPractice(){
//获取图片的地址
JLabel label=new JLabel("Imageicon");
URL url=javaPractice.class.getResource("tx.jpg");
ImageIcon imageIcon=new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container=getContentPane();
container.add(label);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100,100,200,200);
}
public static void main(String[] args) {
new javaPractice();
}
}
3.4 面板
- JPanel
public class javaPractice extends JFrame {
public javaPractice(){
Container container=this.getContentPane();
container.setLayout(new GridLayout(2,1,10,10));
JPanel panel1=new JPanel(new GridLayout(1,3));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
panel1.add(new JButton("1"));
container.add(panel1);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new javaPractice();
}
}
- JScroll
public class javaPractice extends JFrame {
public javaPractice(){
Container container=this.getContentPane();
//文本域
JTextArea textArea=new JTextArea(20,50);
textArea.setText("欢迎学习狂神说java");
//Scroll面板
JScrollPane scrollPane=new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new javaPractice();
}
}
3.5 按钮
- 图片按钮
public class javaPractice extends JFrame {
public javaPractice(){
Container container=this.getContentPane();
//将一个图片变为图标
URL resource=javaPractice.class.getResource("tx.jpg");
Icon icon=new ImageIcon(resource);
//把这个图标放在按钮上
JButton button=new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");
container.add(button);
this.setVisible(true);
this.setBounds(100,100,300,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new javaPractice();
}
}
- 单选按钮
public class javaPractice extends JFrame {
public javaPractice(){
Container container=this.getContentPane();
//将一个图片变为图标
URL resource=javaPractice.class.getResource("tx.jpg");
Icon icon=new ImageIcon(resource);
//单选框
JRadioButton radioButton1=new JRadioButton("button1");
JRadioButton radioButton2=new JRadioButton("button2");
JRadioButton radioButton3=new JRadioButton("button3");
//单选只能选择一个,所以一个组中只能选择一个
container.add(radioButton1,BorderLayout.CENTER);
container.add(radioButton2,BorderLayout.NORTH);
container.add(radioButton3,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,100,300,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new javaPractice();
}
}
- 复选按钮
public class javaPractice extends JFrame {
public javaPractice(){
Container container=this.getContentPane();
//多选框
JCheckBox checkBox01=new JCheckBox("checkBox01");
JCheckBox checkBox02=new JCheckBox("checkBox02");
container.add(checkBox01,BorderLayout.NORTH);
container.add(checkBox02,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,100,300,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new javaPracti ce();
}
}
3.6 列表
- 下拉框
public class javaPractice extends JFrame {
public javaPractice(){
Container container=this.getContentPane();
JComboBox status=new JComboBox();
status.addItem(null);
status.addItem("正在热映");
status.addItem("已下架");
status.addItem("即将上映");
container.add(status);
this.setVisible(true);
this.setBounds(100,100,300,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new javaPractice();
}
}
- 列表框
public class javaPractice extends JFrame { public javaPractice(){ Container container=this.getContentPane(); //String[] content={"1","2","3"}; Vector contents=new Vector(); //列表中需要加入内容 JList jList=new JList(contents); contents.add("zhangsan"); contents.add("lisi"); contents.add("wangwu"); container.add(jList); this.setVisible(true); this.setBounds(100,100,300,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new javaPractice(); } }
- 应用场景
- 选择地区,或者一些单个选项
- 列表,展示信息,一般是动态扩容
3.7 文本框
- 文本框
public class javaPractice extends JFrame {
public javaPractice(){
Container container=this.getContentPane();
JTextField textField=new JTextField("hello");
JTextField textField0=new JTextField("word",20);
container.add(textField,BorderLayout.SOUTH);
container.add(textField0,BorderLayout.NORTH);
this.setVisible(true);
this.setBounds(100,100,300,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new javaPractice();
}
}
- 密码框
public class javaPractice extends JFrame {
public javaPractice(){
Container container=this.getContentPane();
//画板
JPasswordField passwordField=new JPasswordField();
passwordField.setEchoChar('*');
container.add(passwordField);
this.setVisible(true);
this.setBounds(100,100,300,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new javaPractice();
}
}
- 文本域
public class javaPractice extends JFrame {
public javaPractice(){
Container container=this.getContentPane();
//文本域
JTextArea textArea=new JTextArea(20,50);
textArea.setText("欢迎学习狂神说java");
//Scroll面板
JScrollPane scrollPane=new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,300,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new javaPractice();
}
}
更多推荐
已为社区贡献5条内容
所有评论(0)