1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| package day1;
import javax.swing.*;
import javax.swing.border.Border;
import java.util.*;
import java.sql.*;
public class Test3 extends JFrame {
private JPanel jContentPane = null;
// JComboBox jComboBox = new JComboBox();
private JComboBox jComboBox = null;
private JButton jButton = null;
public ArrayList countryTab = new ArrayList();
public String[] country = new String[136];
private javax.swing.JLabel jLabel = null;
public static void main(String[] args) {
Test3 ts = new Test3();
}
/**
* This is the default constructor
*/
public Test3() {
super("World");
initialize();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//getDataCombo(); // set up the combo country
JComboBox jcb = new JComboBox();
jContentPane.add(jcb);
setVisible(true);
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setContentPane(getJContentPane());
this.setSize(548, 349);
this.setTitle("My Swing");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJComboBox(), null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJLabel(), null);
}
return jContentPane;
}
/**
* This method initializes jComboBox
*
* @return javax.swing.JComboBox
*/
private JComboBox getJComboBox() {
getDataCombo(); // set up the combo country
//JComboBox jComboBox = new JComboBox(country);
if (jComboBox == null) {
jComboBox = new JComboBox(country);
jComboBox.setBounds(256, 44, 182, 24);
jComboBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println(
jComboBox.getItemCount()
+ "/"
+ jComboBox.getSelectedItem()
+ "/"
+ jComboBox.getSelectedIndex());
String ns = (String) jComboBox.getSelectedItem();
if (jComboBox.getSelectedItem().equals("Canada")) {
jButton.setEnabled(false);
jButton.setText("Ok");
} else {
jButton.setEnabled(true);
jButton.setText("Oui");
}
}
});
System.out.println("Null");
}
return jComboBox;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(446, 10, 69, 58);
jButton.setText("Go");
jButton.setIcon(
new ImageIcon(getClass().getResource("/images/pc.gif")));
jButton.setHorizontalTextPosition(
javax.swing.SwingConstants.CENTER);
}
return jButton;
}
public void getDataCombo() {
int i = 0;
String data = "jdbc:odbc:WorldEnergy";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(data, "", "");
Statement st = conn.createStatement();
ResultSet rec =
st.executeQuery("SELECT DISTINCT Country " + "FROM Coal " +
//"WHERE " + "(Country='" + arguments[0] + "') " +
"ORDER BY Country");
while (rec.next()) {
String str = rec.getString(1);
//country[i] = rec.getString(1);
country[i] = str;
countryTab.add(rec);
i++;
}
// jComboBox = new JComboBox(country);
st.close();
} catch (SQLException s) {
System.out.println(
"SQL Error: "
+ s.toString()
+ " "
+ s.getErrorCode()
+ " "
+ s.getSQLState());
} catch (Exception e) {
System.out.println("Error: " + e.toString() + e.getMessage());
}
}
/**
* This method initializes jLabel
*
* @return javax.swing.JLabel
*/
private JLabel getJLabel() {
if(jLabel == null) {
jLabel = new JLabel();
jLabel.setBounds(405, 12, 36, 22);
jLabel.setText("");
jLabel.setIcon(new ImageIcon(getClass().getResource("/images/icon_cry.gif")));
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
jLabel.setHorizontalTextPosition(SwingConstants.CENTER);
}
return jLabel;
}
} // @jve:visual-info decl-index=0 visual-constraint="18,8" |
Partager