📄 mainwin.java
字号:
package jwin;
import java.awt.Color;
import java.awt.Component;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import com.jgoodies.forms.factories.DefaultComponentFactory;
public class MainWin {
private JTextArea textArea;
private JTextField textField;
private JTable table;
private JFrame frame;
private DBConnect dbc = new DBConnect();
private JTableDataSet dataset;
private chStr str = new chStr();
Connection con ;
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
MainWin window = new MainWin();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the application
*/
public MainWin() {
createContents();
}
/**
* Initialize the contents of the frame
*/
private void createContents() {
frame = new JFrame("java数据查询");
final JScrollPane scrollPane = new JScrollPane();
frame.addComponentListener(new ComponentAdapter() {
public void componentResized(final ComponentEvent e) {
scrollPane.setVisible(false);
scrollPane.setSize(frame.getWidth()-30, frame.getHeight()-300);
scrollPane.setVisible(true);
}
});
frame.setIconImages(null);
frame.getContentPane().setLayout(null);
frame.setBounds(100, 100, 644, 432);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
final JMenu menu = new JMenu();
menu.setText("文件");
menuBar.add(menu);
final JMenu menu_3 = new JMenu();
menu_3.setText("新建");
menu.add(menu_3);
final JMenuItem newItemMenuItem = new JMenuItem();
newItemMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
}
});
newItemMenuItem.setText("文本文档");
menu_3.add(newItemMenuItem);
final JMenuItem newItemMenuItem_1 = new JMenuItem();
newItemMenuItem_1.setText("xml文件");
menu_3.add(newItemMenuItem_1);
final JMenu menu_4 = new JMenu();
menu_4.setText("打开");
menu.add(menu_4);
final JMenuItem newItemMenuItem_4 = new JMenuItem();
newItemMenuItem_4.setText("数据库连接属性文件");
menu_4.add(newItemMenuItem_4);
final JMenuItem newItemMenuItem_5 = new JMenuItem();
newItemMenuItem_5.setText("数据库连接XML文件");
menu_4.add(newItemMenuItem_5);
scrollPane.setRowHeader(null);
scrollPane.setBounds(10, 232, 601, 136);
frame.getContentPane().add(scrollPane);
table = new JTable();
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
scrollPane.setViewportView(table);
scrollPane.setVisible(false);
final JLabel label = DefaultComponentFactory.getInstance().createTitle("显示数据");
label.setForeground(new Color(128, 0, 0));
label.setFont(new Font("楷体_GB2312", Font.PLAIN, 14));
label.setBounds(233, 211, 56, 15);
frame.getContentPane().add(label);
textField = new JTextField();
textField.setBounds(171, 24, 358, 22);
frame.getContentPane().add(textField);
final JLabel label_1 = new JLabel();
label_1.setText("数据库连接属性文件路径");
label_1.setBounds(10, 23, 155, 22);
frame.getContentPane().add(label_1);
textArea = new JTextArea();
textArea.setBorder(new LineBorder(Color.black, 1, false));
textArea.setBounds(10, 80, 519, 102);
frame.getContentPane().add(textArea);
final JLabel label_2 = new JLabel();
label_2.setForeground(new Color(255, 0, 0));
label_2.setFont(new Font("楷体_GB2312", Font.BOLD, 12));
label_2.setText("SQL区域");
label_2.setBounds(10, 51, 56, 23);
frame.getContentPane().add(label_2);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
con = dbc.ConnectionByProperty(textField.getText());
String sql = textArea.getText();
dataset = new JTableDataSet(sql,con);
table.setModel(dataset);
scrollPane.setSize(frame.getWidth()-20, frame.getHeight()-(frame.getHeight()-scrollPane.getHeight()+20));
scrollPane.setVisible(true);
}
});
button.setText("执行查询");
button.setBounds(425, 188, 104, 22);
frame.getContentPane().add(button);
final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
FileDialog fd = new FileDialog(frame,"",FileDialog.LOAD);
fd.setVisible(true);
textField.setText(fd.getDirectory()+fd.getFile());
//con = dbc.ConnectionByProperty(str.chStr(fd.getFile()));
}
});
button_1.setText("浏览文件");
button_1.setBounds(530, 24, 81, 22);
frame.getContentPane().add(button_1);
}
/**
* WindowBuilder generated method.<br>
* Please don't remove this method or its invocations.<br>
* It used by WindowBuilder to associate the {@link javax.swing.JPopupMenu} with parent.
*/
private static void addPopup(Component component, final JPopupMenu popup) {
component.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger())
showMenu(e);
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger())
showMenu(e);
}
private void showMenu(MouseEvent e) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -