|
|
|
MainFrameJavaJFrame หรืออะหลายๆอย่างจะเอาลงเรื่อยๆน่ะครับ |
|
|
|
|
|
|
|
Code (Java)
package miniTest;
public class Car {
int plate;
String model;
String make;
public Car(int plate,String model,String make){
this.plate = plate;
this.model = model;
this.make = make;
}
public String toString(){
return "plate:"+plate + ", model:"+ model + ", make:"+ make;
}
}
=======
package miniTest;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.util.*;
public class MainFrame extends JPanel implements ActionListener {
private static int frameWidth = 400;
private static int frameHeight = 300;
AddCarDlg addCarDlg;
ViewCarLabelDlg viewCarDlg;
Vector<String> cars;
private static final String mainString = "Main Menu";
private static final String carString = "Add Car";
private static final String viewString = "View Cars";
private static final String exitString = "Exit";
public MainFrame(JFrame frame){
super();
cars = new Vector();
addCarDlg = new AddCarDlg(frame, cars);
viewCarDlg = new ViewCarLabelDlg(frame);
int panelWidth = 500;
int panelHeight = 400;
setLayout(null);
JLabel mainLabel = new JLabel("Main Menu");
add(mainLabel);
JButton carBtn = new JButton(carString);
carBtn.addActionListener(this);
add(carBtn);
JButton viewBtn = new JButton(viewString);
viewBtn.addActionListener(this);
add(viewBtn);
JButton exitBtn = new JButton(exitString);
exitBtn.addActionListener(this);
add(exitBtn);
Dimension size = mainLabel.getPreferredSize();
mainLabel.setBounds((panelWidth - size.width) / 2, 20, size.width, size.height);
size = carBtn.getPreferredSize();
carBtn.setBounds((panelWidth - size.width) / 2, 60, size.width, size.height);
size = viewBtn.getPreferredSize();
viewBtn.setBounds((panelWidth - size.width) / 2, 100, size.width, size.height);
this.setPreferredSize(new Dimension(panelWidth, panelHeight));
}
public void actionPerformed(ActionEvent evt){
String actionCommand = evt.getActionCommand();
System.out.println("actionCommand:" + actionCommand);
if (actionCommand.equals(carString)){
System.out.println("equals " + carString);
addCarDlg.setVisible(true);
}else if (actionCommand.equals(viewString)) {
System.out.println("equals " + viewString);
viewCarDlg.viewCar();
} else if (actionCommand.equals(exitString)) {
System.out.println("equals " + exitString);
System.exit(0);
}
}
private static void ShowGUI(){
JFrame frame = new JFrame(" Main ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MainFrame(frame));
frame.pack();
frame.setVisible(true);
}
private static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
ShowGUI();
}
});
}
}
Tag : Java, JAVA
|
|
|
|
|
|
Date :
2017-09-11 21:07:32 |
By :
Niyom.Tha |
View :
2863 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (Java)
package lect9;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class UpdateCarDlg extends JDialog implements ActionListener {
private static final String okString = "OK";
private static final int width = 300;
private static final int height = 200;
JTextField plateTxt;
JTextField modelTxt;
JTextField makeTxt;
private static final String addString = "Add";
private static final String cancelString = "Cancel";
private Vector<Car> cars;
public UpdateCarDlg(JFrame frame, Vector data) {
super(frame, true);
this.cars = data;
Container pane = getContentPane();
pane.setLayout(null);
JLabel addCarLabel = new JLabel("Updata Car");
JLabel plateLabel = new JLabel("Plate:");
JLabel modelLabel = new JLabel("Model:");
JLabel makeLabel = new JLabel("Make:");
JButton addBtn = new JButton(addString);
JButton cancelBtn = new JButton(cancelString);
plateTxt = new JTextField(10);
modelTxt = new JTextField(20);
makeTxt = new JTextField(20);
addBtn.addActionListener(this);
cancelBtn.addActionListener(this);
pane.add(addCarLabel);
pane.add(plateLabel);
pane.add(modelLabel);
pane.add(makeLabel);
pane.add(plateTxt);
pane.add(modelTxt);
pane.add(makeTxt);
pane.add(addBtn);
pane.add(cancelBtn);
Dimension size = addCarLabel.getPreferredSize();
addCarLabel.setBounds((width - size.width) / 2, 5, size.width, size.height);
size = plateLabel.getPreferredSize();
plateLabel.setBounds(10, 25, size.width, size.height);
size = modelLabel.getPreferredSize();
modelLabel.setBounds(10, 50, size.width, size.height);
size = makeLabel.getPreferredSize();
makeLabel.setBounds(10, 75, size.width, size.height);
size = plateTxt.getPreferredSize();
plateTxt.setBounds(50, 25, size.width, size.height);
size = modelTxt.getPreferredSize();
modelTxt.setBounds(50, 50, size.width, size.height);
size = makeTxt.getPreferredSize();
makeTxt.setBounds(50, 75, size.width, size.height);
size = addBtn.getPreferredSize();
addBtn.setBounds(width / 2 - size.width - 10, 105, size.width, size.height);
size = cancelBtn.getPreferredSize();
cancelBtn.setBounds(width / 2 + 10, 105, size.width, size.height);
pane.setPreferredSize(new Dimension(width, height));
pack();
setVisible(false);
System.out.println("UpdareCarDialog() done!");
}
public void actionPerformed(ActionEvent evt) {
String actionCommand = evt.getActionCommand();
if (actionCommand.equals(addString)) {
System.out.println("equals " + addString);
try {
int plate = Integer.parseInt(plateTxt.getText());
String model = modelTxt.getText();
String make = makeTxt.getText();
Car car = new Car(plate, model, make);
updateCar(car);
plateTxt.setText(null);
modelTxt.setText(null);
makeTxt.setText(null);
} catch (Exception ex) {
System.err.println("Error! Invalid data.");
}
} else if (actionCommand.equals(cancelString)) {
System.out.println("equals " + cancelString);
setVisible(false);
}
}
void updateCar(Car car) {
try {
String updateSql = "UPDATE CAR SET MODEL='" + car.model + "', MAKE='" + car.make + "' WHERE PLATE=" + car.plate;
System.out.println("updateSql:" + updateSql);
ConnectionDB connDB = new ConnectionDB();
Connection con = connDB.getConnection();
Statement stmnt = null;
if (con != null) {
stmnt = con.createStatement();
stmnt.executeUpdate(updateSql);
stmnt.close();
con.close();
System.out.println("Car updated successfully.");
}
} catch (NumberFormatException ex) {
System.err.println("Error! Invalid plate.");
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
|
|
|
|
|
Date :
2017-09-11 21:44:23 |
By :
SparTaCus |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2017-09-12 16:29:48 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|