java swing 布局,JFrame JPanel,各组件及功能的使用

        最近公司需要用到swing做界面,java中的swing用的是越来越少了。看了一下swing的知识总结了以下功能,大家不喜勿喷,有什么错误的地方,大家包涵并指出,刚毕业,望各位前辈指正,想结交被称为大神的你们。

 package library;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Label;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Library  implements ActionListener {
BufferedReader br = null;
JFrame frame = new JFrame("图书馆盘点系统");
Container contentPane = frame.getContentPane();
// contentPane.setBackground(Color.CYAN);
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
// 组件
JButton inventory = new JButton("盘点");
JButton search = new JButton("查询");
JButton operationbooks = new JButton("图书操作");
JButton Statistic = new JButton("统计");
JButton overview = new JButton("概览");
JButton inventorydetail = new JButton("盘点详情");
JButton wrongcase = new JButton("错架");
JButton unonbookshelf = new JButton("未在架");
JButton start = new JButton("开始");
JButton pause = new JButton("暂停");
JButton end = new JButton("结束");
Label lable1 = new Label("状态:   正在扫描 " );
DefaultTableModel tableModel = new DefaultTableModel();
JTable table = new JTable(tableModel);
JScrollPane scrollpane = new JScrollPane(table);

public Library() {
frame.setLayout(null);
panel1.setBackground(Color.white);
panel2.setBackground(Color.white);
panel3.setBackground(Color.white);
panel4.setBackground(Color.white);
panel5.setBackground(Color.white);
panel1.setLayout(null);
panel2.setLayout(null);
panel3.setLayout(null);
panel4.setLayout(null);
panel5.setLayout(null);
search.addActionListener(this);
start.addActionListener(this);


// 添加表格

tableModel.addColumn("图书馆编号");
tableModel.addColumn("图书编码");
tableModel.addColumn("图书名称");
tableModel.addColumn("图书条形码");
tableModel.addColumn("A");
tableModel.addColumn("B");
table.getColumnModel().getColumn(0).setPreferredWidth(230);
table.getColumnModel().getColumn(1).setPreferredWidth(130);
table.getColumnModel().getColumn(2).setPreferredWidth(130);
table.getColumnModel().getColumn(3).setPreferredWidth(130);
table.getColumnModel().getColumn(4).setPreferredWidth(130);
table.getColumnModel().getColumn(5).setPreferredWidth(130);
table.setRowHeight(20);
// 水平滚动条
scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(scrollpane, BorderLayout.CENTER);
frame.getContentPane().add(panel4, BorderLayout.PAGE_END);
panel4.add(scrollpane);
scrollpane.setBounds(10, 40, 490, 380);


// 组件位置
panel1.setBounds(0, 0, 800, 50);
panel2.setBounds(0, 70, 550, 30);
panel3.setBounds(0, 120, 150, 450);
panel4.setBounds(130, 120, 500, 450);
panel5.setBounds(630, 120, 150, 450);
inventory.setBounds(110, 12, 80, 30);
search.setBounds(220, 12, 80, 30);
operationbooks.setBounds(330, 12, 100, 30);
Statistic.setBounds(460, 12, 80, 30);
overview.setBounds(40, 40, 80, 30);
inventorydetail.setBounds(40, 100, 80, 30);
wrongcase.setBounds(40, 150, 80, 30);
unonbookshelf.setBounds(40, 200, 80, 30);
start.setBounds(30, 50, 80, 30);
pause.setBounds(30, 100, 80, 30);
end.setBounds(30, 150, 80, 30);
lable1.setBounds(60, 2, 250, 30);
lable1.setFont(new java.awt.Font("Dialog", 1, 15));
lable1.setForeground(Color.GREEN);


// 组件添加
panel1.add(inventory);
panel1.add(search);
panel1.add(operationbooks);
panel1.add(Statistic);
panel2.add(lable1);
panel3.add(overview);
panel3.add(inventorydetail);
panel3.add(wrongcase);
panel3.add(unonbookshelf);
panel5.add(start);
panel5.add(pause);
panel5.add(end);
contentPane.add(panel2);
contentPane.add(panel3);
contentPane.add(panel4);
contentPane.add(panel5);



contentPane.add(panel1, BorderLayout.CENTER); // 将JPanel实例添加到JFrame的南侧
//frame.setSize(Toolkit.getDefaultToolkit().getScreenSize()); //全屏显示
// frame.setExtendedState(JFrame.ICONIFIED); //最小化


frame.setSize(800, 600);
frame.setVisible(true);
frame.setResizable(false);


}


public static void main(String[] args) {
new Library();
}


@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==search){
new Search();
frame.setVisible(false);
}
if (e.getSource() == start) {
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream("F:\\数据.txt")));
String str = null;
while ((str = br.readLine()) != null) {
String[] s = str.split(",");
tableModel.addRow(s);
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}


}

你可能感兴趣的:(java swing 布局,JFrame JPanel,各组件及功能的使用)