今日在整理两份表格数据,公式怎么都算不出来,一气之下写了个java类实现,后来想做成工具,所以自己画了个界面,用的swing实现的,期间遇到了一些小问题,记录一下
界面布局
swing有三种布局方式:
- FlowLayout布局
- BorderLayout布局
- GridLayout布局
FlowLayout布局是一种顺序排列布局,本身可以通过前中后参数决定位置
package gui;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import javax.swing.JFrame;
public class LiuSiBuJu {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame f = new JFrame("这是流式布局");
f.setBounds(600, 300, 900, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// f.setLayout(null);
f.setLayout(new FlowLayout(FlowLayout.CENTER));
f.setVisible(true);
Button b1 = new Button("1");
Button b2 = new Button("2");
Button b3 = new Button("3");
f.add(b1);
f.add(b2);
f.add(b3);
}
}
运行可得:
BorderLayout布局:可以根据五分屏指定组件的位置
package guitest;
import java.awt.BorderLayout;
import java.awt.Frame;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Dxnbz {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame f = new JFrame("五分屏布局");
f.setBounds(600, 300, 300, 300);
f.setVisible(true);
JButton b1 = new JButton("上");
JButton b2 = new JButton("左");
JButton b3 = new JButton("中");
JButton b4 = new JButton("右");
JButton b5 = new JButton("下");
f.add(b1,BorderLayout.NORTH);
f.add(b2,BorderLayout.WEST);
f.add(b3,BorderLayout.CENTER);
f.add(b4,BorderLayout.EAST);
f.add(b5,BorderLayout.SOUTH);
}
}
执行如图
GridLayout布局:里面有两个参数行和列,按钮会自动填充行列的格子
package guitest;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
*@author 作者 Your-Name:
*@version 创建时间:
*类说明:
*/
public class TableBuJu {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame f = new JFrame("表格布局");
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
f.setLayout(new GridLayout(2,3));//三行两列填充
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(b6);
f.setBounds(600, 300, 300, 300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
运行如图
三种布局可以通过JPANEL混合使用
package gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyFrame extends JFrame {
private JTextField textField1;
private JTextField textField2;
private JTextField textField3;
private JComboBox combpBox;
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JLabel label1;
private JLabel label2;
public MyFrame() {
setTitle("排期排期排了又排排期");
setBounds(600, 300, 900, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(10, 1));
JPanel inputPanel1 = new JPanel();
textField1 = new JTextField(60);
button1 = new JButton("按钮1");
inputPanel1.add(textField1,BorderLayout.CENTER);
inputPanel1.add(button1,BorderLayout.EAST);
JPanel inputPanel2 = new JPanel();
textField2 = new JTextField(60);
button2 = new JButton("按钮2");
inputPanel2.add(textField2,BorderLayout.CENTER);
inputPanel2.add(button2,BorderLayout.EAST);
JPanel inputPanel3 = new JPanel(new GridLayout(1, 4));
JPanel inputPanel4 = new JPanel(new FlowLayout());
JPanel inputPanel5 = new JPanel(new FlowLayout());
label1 =new JLabel("列表");
label1.setText("归属迭代");
combpBox = new JComboBox();
combpBox.addItem("1");
combpBox.addItem("2");
combpBox.addItem("3");
combpBox.addItem("4");
combpBox.addItem("5");
inputPanel4.add(label1);
inputPanel4.add(combpBox);
label2 =new JLabel("日期");
textField3 = new JTextField(10);
inputPanel5.add(label2);
inputPanel5.add(textField3);
inputPanel3.add(inputPanel4);
inputPanel3.add(inputPanel5);
JPanel inputPanel6= new JPanel();
button3 = new JButton("确认");
button4 = new JButton("退出");
inputPanel6.add(button3,BorderLayout.WEST);
inputPanel6.add(button4,BorderLayout.EAST);
add(inputPanel1);
add(inputPanel2);
add(inputPanel3);
add(inputPanel6);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MyFrame frame = new MyFrame();
frame.setVisible(true);
}
});
}
}
运行如图