接下來添加文本組件JTextField和密碼組件JPasswordField。 效果如圖: ...
接下來添加文本組件JTextField和密碼組件JPasswordField。
/**
* @author: lishuai
* @date: 2018/11/26 13:51
*/
public class WeimingSyncApplets {
public static void main(String[] args) {
// 面板組件
JPanel taskPanel = new JPanel();
JPanel dbPanel = new JPanel();
JTabbedPane tabbedPane = buildJTabbedPane(taskPanel, dbPanel);
buildFrame(tabbedPane);
}
private static JTabbedPane buildJTabbedPane(JPanel taskPanel, JPanel dbPanel) {
// 選項卡面板
JTabbedPane tabbedPane = new JTabbedPane();
// 通過BorderFactory來設置邊框的特性
tabbedPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
tabbedPane.add("執行任務", taskPanel);
tabbedPane.add("數據源配置", dbPanel);
// 下拉框
dbPanel.add(buildJLabel("資料庫類型", 10, 20, 80, 25));
String dbs[] = {"mysql", "oracle", "sqlserver"};
dbPanel.add(buildJComboBox("mysql", "mysql", dbs, 0, 100, 20, 165, 25));
// 文本框
dbPanel.add(buildJLabel("用戶名", 10, 50, 80, 25));
JTextField dbUserName = buildJTextField("dbUserName", "dbUserName", 20, 100, 50, 165, 25);
dbPanel.add(dbUserName);
// 密碼
dbPanel.add(buildJLabel("密碼", 10, 80, 80, 25));
JPasswordField dbPassWord = buildJPasswordField("dbPassWord", "dbPassWord", 20, 100, 80, 165, 25);
dbPanel.add(dbPassWord);
return tabbedPane;
}
private static JPasswordField buildJPasswordField(String defaultValue, String name, int columns, int x, int y, int width, int height) {
JPasswordField jPasswordField = new JPasswordField(columns);
jPasswordField.setText(defaultValue);
jPasswordField.setName(name);
jPasswordField.setBounds(x, y, width, height);
return jPasswordField;
}
private static JTextField buildJTextField(String defaultValue, String name, int columns, int x, int y, int width, int height) {
JTextField text = new JTextField(columns);
text.setText(defaultValue);
text.setName(name);
text.setBounds(x, y, width, height);
return text;
}
private static JComboBox buildJComboBox(Object selectedItem, String name, String[] elements, int selectedIndex, int x, int y, int width, int height) {
DefaultComboBoxModel codeTypeModel = new DefaultComboBoxModel();
// elements 下拉框中的選項
for (String element : elements) {
codeTypeModel.addElement(element);
}
JComboBox codeTypeBox = new JComboBox(codeTypeModel);
codeTypeBox.setName(name);
// 預設選中的下拉框選項
codeTypeBox.setSelectedItem(selectedItem);
// codeTypeBox.setSelectedItem(selectedIndex);
codeTypeBox.setBounds(x, y, width, height);
// 添加下拉框事件監聽器
codeTypeBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
// 選擇的下拉框選項
System.out.println(e.getItem());
}
}
});
return codeTypeBox;
}
private static JLabel buildJLabel(String name, int x, int y, int width, int height) {
JLabel label = new JLabel(name);
label.setBounds(x, y, width, height);
return label;
}
private static void buildFrame(JComponent component) {
// 窗體容器
JFrame frame = new JFrame("數據同步工具");
frame.add(component);
// JFrame.EXIT_ON_CLOSE 退出
// JFrame.HIDE_ON_CLOSE 最小化隱藏
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
// 設置佈局
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(BorderLayout.CENTER, component);
// 設置視窗最小尺寸
frame.setMinimumSize(new Dimension(1060, 560));
// 調整此視窗的大小,以適合其子組件的首選大小和佈局
frame.pack();
// 設置視窗相對於指定組件的位置
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// 設置視窗尺寸是否固定不變
frame.setResizable(true);
}
}
效果如圖: