Java的Swing布局⼀、流布局FlowLayout
默认地,⾃左向右逐个排列
当⼀⾏排满时,⾃动排到下⼀⾏
setPreferredSize():控制每个控件的显⽰⾼度和宽度
案例效果
MyFrame.java
package swing布局;
import javax.swing.*;
import java.awt.*;
public class MyFrame extends JFrame {
public MyFrame(String title){
super(title);
Container contentPane=getContentPane();
//这⾥的参数设置的是对齐⽅式FlowLayout.LEFT左对齐
LayoutManager layout=new FlowLayout(FlowLayout.LEFT);
contentPane.setLayout(layout);
JLabel a1=new ColorfulLabel("1",Color.YELLOW);
JLabel a2=new ColorfulLabel("2",Color.GREEN);
JLabel a3=new ColorfulLabel("3",Color.LIGHT_GRAY);
JLabel a4=new ColorfulLabel("4",Color.CYAN);
JLabel a5=new ColorfulLabel("5",Color.WHITE);
contentPane.add(a1);
contentPane.add(a2);
contentPane.add(a3);
contentPane.add(a4);
contentPane.add(a5);
//设置宽和⾼
a4.setPreferredSize(new Dimension(150,30));
}
private static class ColorfulLabel extends JLabel{
public ColorfulLabel(String text,Color bgColor){
super(text);
setOpaque(true);
setBackground(bgColor);
setPreferredSize(new Dimension(60,30));
setHorizontalAlignment(SwingConstants.CENTER);
}
}
}
SwingDemo.java
package swing布局;
import javax.swing.*;
public class SwingDemo {
private static void CreateGUI(){
JFrame frame=new MyFrame("流布局");
frame.setSize(400,300);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
CreateGUI();
}
});
}
}
⼆、边界布局BorderLayout
把容器分为上、下、左、右、中五个区域setPreferredSize()
对于上下边界,可以调整⾼度
对于左右边界,可以调整宽度
运⾏结果
MyFrame2.java
package swing布局;
import javax.swing.*;
import java.awt.*;
public class MyFrame2 extends JFrame {
public MyFrame2(String title){
super(title);
Container contentPane=getContentPane();
contentPane.setLayout(new BorderLayout());
JLabel a1=new ColorfulLabel("1",Color.YELLOW);
JLabel a2=new ColorfulLabel("2",Color.GREEN);
JLabel a3=new ColorfulLabel("3",Color.LIGHT_GRAY); JLabel a4=new ColorfulLabel("4",Color.CYAN);
JLabel a5=new ColorfulLabel("5",Color.WHITE);
//五个区域
contentPane.add(a1,BorderLayout.PAGE_START);
contentPane.add(a2,BorderLayout.PAGE_END);
contentPane.add(a3,BorderLayout.LINE_START);
contentPane.add(a4,BorderLayout.LINE_END);
contentPane.add(a5,BorderLayout.CENTER);
//设置宽和⾼
a1.setPreferredSize(new Dimension(0,80));
a3.setPreferredSize(new Dimension(100,0));
}
private static class ColorfulLabel extends JLabel{
public ColorfulLabel(String text,Color bgColor){
super(text);
setOpaque(true);
setBackground(bgColor);
setPreferredSize(new Dimension(60,30));
setHorizontalAlignment(SwingConstants.CENTER); }
}
}
SwingDemo02.java
package swing布局;
import javax.swing.*;
public class SwingDemo02 {
private static void CreateGUI(){
JFrame frame=new MyFrame2("边界布局");
frame.setSize(400,300);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
CreateGUI();
}
});
}
}
三、卡⽚布局CardLayout
运⾏结果
通过切换下拉框的内容切换卡⽚
MyFrame3.java
package swing布局;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class MyFrame3 extends JFrame {
JPanel cards=new JPanel();
JComboBox<String> options=new JComboBox<>();
public MyFrame3(String title){
super(title);
//内容⾯板
Container contentPane=getContentPane();
contentPane.setLayout(new BorderLayout());
//向下拉框列表添加内容
options.addItem("第⼀个⾯板");
options.addItem("第⼆个⾯板");
/
/将下拉框添加⾄边界布局的顶部
contentPane.add(options,BorderLayout.PAGE_START);
//将卡⽚布局添加⾄边界布局的center
contentPane.add(cards,BorderLayout.CENTER);
//创建第⼀个⾯板
JPanel p1=new JPanel();
p1.add(new JButton("红"));
p1.add(new JButton("绿"));
p1.add(new JButton("蓝"));
//创建第⼆个⾯板
JPanel p2=new JPanel();
p2.add(new JLabel("输⼊"));
p2.add(new JTextField(20));
//将这两个⾯板添加到卡⽚布局中,其中的第⼆个参数为布局的名称,⽅便调⽤ cards.setLayout(new CardLayout());
cards.add(p1,"buttons");
cards.add(p2,"text");
//添加事件响应
options.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
ItemChange();
}
});
}
private void ItemChange() {
CardLayout cardLayout=(Layout();
int SelectedIndex();
System.out.println(index);
if (index==0){
cardLayout.show(cards,"buttons");
}else if(index==1){
cardLayout.show(cards,"text");
}
}
}
SwingDemo03.java
package swing布局;
import javax.swing.*;
public class SwingDemo03 {
private static void CreateGUI(){
JFrame frame=new MyFrame3("卡⽚布局"); frame.setSize(400,300);
frame.setVisible(true);
javaswing和javafx}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() { @Override
public void run() {
CreateGUI();
}
});
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论