Java布局管理器使用方法_MySQL
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java布局管理器使用方法_MySQL,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5535字,纯文字阅读大概需要8分钟。
内容图文
![Java布局管理器使用方法_MySQL](/upload/InfoBanner/zyjiaocheng/570/7333b7622f63426ea12c5ededca17a7b.jpg)
首先,建一个frame文件(Application应用程序),在Design中将this中的layout设置为BorderLayout。
第二,在组件盘内点选Swing Container页签,选取Jpanel图标,在this中上方拖拽一块区域,布局器会自动调整位置与大小;同样的方法在中下方也拖拽一块区域;在Swing Container页签,选取jScrollPane图标,将jScrollPane在中间拖拽一块区域。拖拽的顺序一定要先上后下再中间。为了方便区分,在Properties的background中,将上方的Jpanel1区域设置为红色,下方的Jpanel2区域设置为橙色,中间的jScrollPane1为粉红色。将Jpanel1和Jpanel2的layout设置为flowLayout(必须要手动设置,不要采用默认值)。
第三,在Jpanel中放入一个Jlable标题栏,JTextField1文本框和Jbutton按钮,在组件盘内点选Swing 页签,选取JLable图标在Jpanel1的中画一个标题栏,将text改为“请输入查询条件”,再选取JtextField在Jpanel1中画一个文本框,将text改为空,最后选取Jbutton在Jpanel1中再画一个按钮将text改为“查询”。画完后他们都是在中间,而且大小固定,这时点选Jpanel的flowLayout1将右边Properties中的alignment设置为LEFT,这时Jpanel1中的组键就会向左排列。选中其中一个组键,在Properties中的preferredSize可以设置组键的宽和高。同样的方法在Jpanel2中画三个Jbutton按钮,将text分别设为“增加”、“删除”、“修改”。点选Jpane2的flowLayout2将右边Properties中的hgap设置为30(按钮的间距,可根据自己的需要调整数值大小), 这样就调整了三个按钮之间的距离,设置vgap还可以改变Jpane2区域的高度。
第四,在jScrollPane1中建一个表格用来显示数据库数据的内容,在组件盘内点选Swing 页签,选取JTable图标,将Jtable加入到jScrollPane1中。
最后,将this中的defaultCloseOperation改为EXIT_ON_CLOSE,这样在关闭窗口时程序会自动退出。
程序源代码如下(除中文注释部分的两句是自己加上去,其余是自动生成):
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
public class Frame1
extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField();
JButton jButton1 = new JButton();
FlowLayout flowLayout1 = new FlowLayout();
FlowLayout flowLayout2 = new FlowLayout();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
GridLayout gridLayout1 = new GridLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTable jTable1 = new JTable();
public Frame1() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Frame1 frame1 = new Frame1();
frame1.setSize(new Dimension(400, 350));
frame1.show();
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(borderLayout1);
jPanel1.setBackground(Color.red);
jPanel1.setLayout(flowLayout1);
jPanel2.setBackground(Color.red);
jPanel2.setLayout(flowLayout2);
jPanel3.setBackground(Color.pink);
jPanel3.setLayout(gridLayout1);
jLabel1.setPreferredSize(new Dimension(100, 16));
jLabel1.setText("请输入查询条件");
jTextField1.setPreferredSize(new Dimension(140, 22));
jTextField1.setText("");
jButton1.setText("查询");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
flowLayout1.setAlignment(FlowLayout.LEFT);
flowLayout1.setHgap(5);
flowLayout1.setVgap(10);
jButton2.setText("增加");
jButton3.setText("删除");
jButton4.setText("修改");
flowLayout2.setHgap(30);
flowLayout2.setVgap(5);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().add(jPanel1, BorderLayout.NORTH);
jPanel1.add(jLabel1, null);
jPanel1.add(jTextField1, null);
jPanel1.add(jButton1, null);
this.getContentPane().add(jPanel2, BorderLayout.SOUTH);
jPanel2.add(jButton2, null);
jPanel2.add(jButton3, null);
jPanel2.add(jButton4, null);
this.getContentPane().add(jPanel3, BorderLayout.CENTER);
jPanel3.add(jScrollPane1, null);
jScrollPane1.getViewport().add(jTable1, null);
}
//模拟查询数据库
void jButton1_actionPerformed(ActionEvent e) {
try { //制作表
Vector vcol = new Vector(); //列名
Vector vrow = new Vector(); //内容
for (int col = 1; col < 31; col++) {
vcol.addElement("列" + col);
}
for (int row = 1; row < 101; row++) {
Vector vr1 = new Vector();
for (int col = 1; col < 31; col++) {
vr1.addElement(row + "/" + col);
}
vrow.addElement(vr1);
}
DefaultTableModel dtm = new DefaultTableModel(vrow, vcol);
jTable1 = new JTable(vrow, vcol);
jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); //滚动条设置左右滚
this.jScrollPane1.getViewport().add(jTable1, null); //在滚动条中放入表
}
catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
}
class Frame1_jButton1_actionAdapter
implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
内容总结
以上是互联网集市为您收集整理的Java布局管理器使用方法_MySQL全部内容,希望文章能够帮你解决Java布局管理器使用方法_MySQL所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。