GUI application
3. Thêm Mouse listener/Mouse motion listener vào TrayIcon
4. Ví dụ về System Tray đáp ứng chọn lựa từ user
5. Chuyển đổi giao diện thành “Windows look and feel” trong Java 6
6. Chuyển đổi giao diện thành “Motif look and feel” trong Java 6
7. In ấn trong các Text Component
8. MDI – Muliple Docoment Interface Application
mitsu said
Em có tạo 1 class với các nút bấm, mục đích của em là khi bấm 1 nút tương ứng thì mình sẽ thay đổi màu nền của 1 frame thuốc lớp khác. Nhưng em ko để thao tác với các đối tượng khác lớp được. Có cách nào để từ 1 lớp này có thể thao tác với lớp khác ko thầy.
YinYang said
Bạn chỉ cần truyền nó vào construc như 1 tham số, khi đó đối tượng sẽ được tham chiếu đến và sử dụng bình thường
chiencong said
chào thầy!!!
thầy ơi, thầy có thề cho em 1 đoạn code mà làm hiển thị giờ, phút, giây nó chạy lên 1 cái Jframe chính được không dạ.
còn việc hiện thị ngày giờ thì em biết rồi ạ.
Thân thầy
vovanhai said
Đây:
package vovanhai.worpress.com;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class MyClock extends JFrame implements ActionListener{
private static final long serialVersionUID = 194979931331258758L;
private JLabel lblDisplay;
private Timer timer;
public MyClock() {
setTitle(“My Clock”);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400,250);
this.add(lblDisplay=new JLabel(“”,JLabel.CENTER));
lblDisplay.setFont(new Font(“Arial”,Font.BOLD,40));
timer=new Timer(1000,this);
timer.start();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(timer)) {
Calendar cal=Calendar.getInstance();
String s=cal.getTime().toString();
lblDisplay.setText(s);
}
}
public static void main(String[] args) {
new MyClock().setVisible(true);
}
}
tuankiet said
chào thầy.
em có thể thay đổi cái title bar trong cái JFrame được ko vậy.
tại thấy cái biểu tượng càfephim thấy buồn ngủ quá.
thân thầy.hihihi
nguyenthanhcong said
THẦY ƠI. VẬY EM MUỐN HIỆN NGÀY THÁNG THÌ SAO THẦY.
THÂN THẦY.
tuankiet said
thầy ơi!!
cho em hỏi .có 1 cái jframe trên đó em có 4 cái nút a,b,c,d.
giờ thầy có thể chỉ cho em làm sao mà em nhấn vô 1 trong 4 nút thì nó thay đổi cái Jpanel theo ý mình dc ko ạ.
nếu em muốn show như vậy thì em phải extends hay implement,và xử lý event như nào vậy thầy.
thầy có thể cho em 1 code nhỏ nhỏ để thay đổi Jpanel dc ko thầy.
Em cám ơn thầy .
mong dc thư hồi âm của thầy .
nguyenthanhcong said
thầy ơi!!!
em viết chương trình máy tính.chương trình không bị lổi nhưng mà khi em dug thì nó báo lỗi là:
Exception in thread “AWT-EventQueue-0” java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at MyCacular.CacularFrame.(CacularFrame.java:12)
at MyCacular.Test_Cacular$1.run(Test_Cacular.java:18)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
——————
đoạn code của em là:
package MyCacular;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class Test_Cacular {
/**
* @param args
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
CacularFrame Frame=new CacularFrame();
Frame.setVisible(true);
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
——————
package MyCacular;
import javax.swing.JFrame;
public class CacularFrame extends JFrame{
private static final long serialVersionUID = 1L;
public CacularFrame()
{
setTitle(“MAY TINH MINI”);
setSize(400,400);
CacularPanel panel=new CacularPanel();
add(panel);
pack();
}
}
—————————
package MyCacular;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CacularPanel extends JFrame{
private JButton display;
private JPanel panel;
private double result;
private String lastCommand;
private boolean start;
private static final long serialVersionUID = 1L;
public CacularPanel()
{
setLayout(new BorderLayout());
result =0;
lastCommand =”=”;
start=true;
//add de no hien thi:
display=new JButton(“0”);
display.setEnabled(false);
add(display,BorderLayout.NORTH);
ActionListener insert=new InsertAction();
ActionListener command=new CommandAction();
//add button tren 4×4 gird:
panel=new JPanel();
panel.setLayout(new GridLayout(5,5));
addButton(“(“,insert);
addButton(“)”,insert);
addButton(“sqrt”,insert);
addButton(“fresh”,command);
addButton(“7”,insert);
addButton(“8”,insert);
addButton(“9”,insert);
addButton(“/”,command);
addButton(“4”,insert);
addButton(“5”,insert);
addButton(“6”,insert);
addButton(“*”,command);
addButton(“1”,insert);
addButton(“2”,insert);
addButton(“3”,insert);
addButton(“-“,command);
addButton(“0”,insert);
addButton(“.”,insert);
addButton(“=”,insert);
addButton(“+”,command);
add(panel,BorderLayout.CENTER);
}
private void addButton(String label,ActionListener listener)
{
JButton button=new JButton(label);
button.addActionListener(listener);
panel.add(button);
}
private class InsertAction implements ActionListener
{
@Override
public void actionPerformed(ActionEvent event)
{
String input=event.getActionCommand();
if(start)
{
display.setText(“”);
start=false;
}
display.setText(display.getText()+input);
}
}
private class CommandAction implements ActionListener
{
@Override
public void actionPerformed(ActionEvent event)
{
String command =event.getActionCommand();
if(start)
{
if(command.equals(“-“))
{
display.setText(command);
start=false;
}
else lastCommand=command;
}
else
{
calculate(Double.parseDouble(display.getText()));
lastCommand=command;
start=true;
}
}
}
public void calculate(double x)
{
if(lastCommand.equals(“+”)) result +=x;
else if(lastCommand.equals(“-“)) result -=x;
else if(lastCommand.equals(“*”)) result *=x;
else if(lastCommand.equals(“/”)) result /=x;
else if(lastCommand.equals(“=”)) result =x;
//else if(lastCommand.equals(“sqrt”)) result =x;
else if(lastCommand.equals(“fresh”)) result =0;
display.setText(“”+result);
}
}
vovanhai said
Bạn đổi khai báo:
public class CacularPanel extends JPanel
thay vì public class CacularPanel extends JFrame
Bởi làm như bạn có nghĩa là đưa 1 JFrame vào 1 JFrame.
Chúc mừng năm mới!
vovanhai said
Thay đổi icon trên title bar:
java.awt.Image img=this.getToolkit().createImage(“img/attention.gif”);
this.setIconImage(img);
vovanhai said
”
thầy ơi!!
cho em hỏi .có 1 cái jframe trên đó em có 4 cái nút a,b,c,d.
giờ thầy có thể chỉ cho em làm sao mà em nhấn vô 1 trong 4 nút thì nó thay đổi cái Jpanel theo ý mình dc ko ạ.
nếu em muốn show như vậy thì em phải extends hay implement,và xử lý event như nào vậy thầy.
thầy có thể cho em 1 code nhỏ nhỏ để thay đổi Jpanel dc ko thầy.
Em cám ơn thầy .
mong dc thư hồi âm của thầy .
”
Câu hỏi của em chưa rõ ràng. Em muốn thay đổi JPanel là thay đổi thế nào? Em nói chi tiết điều em cần nhé!
tuankiet said
ví dụ em có 1 cái JFrame chính .
trên cái JFrame đó .em có 4 nút. nó nằm dọc theo bên góc trái FRAME.
khi em nhấn vô 1 trong 4 nút thì cái Jpanel thay đổi.
4 nút là làm thay đổi 4 lần đó thầy.
không biết thầy hiểu ý em chưa?
thân thầy.
vovanhai said
Để làm được như thế, em viết 4 lớp và các lớp này extends từ JPanel.
Trên frame chính, em tạo 2 panel, 1 cái chứa 4 nút nằm bên trái, 1 cái tên pcen nằm giữa. Khi nhấn 1 nút, em cứ add cái panel mà em muốn vào pcen là ngon lành. Nhưng nhớ trước khi add thì phải remove tất cả component ra khỏi pcen đã nhé. Chcu1 vui
tuankiet said
Thay đổi icon trên title bar:
java.awt.Image img=this.getToolkit().createImage(”img/attention.gif”);
this.setIconImage(img);
———
thầy ơi. em lấy đoạn code này rồi mà sao ko thay đổi dc cái title bar vậy thầy.
em đưa hình vào trong Bin , và trong scr trong phần bài tập mà vẫn ko dc . thầy có thể giải thích em cái à
thân thầy
ditimniemvui said
em tạo 1 cái JFrame login USE,PASS.!!!
làm sao mà nó liên kết giống dc như cái phần mềm điểm danh của thầy mà thầy đăng nhập dc đó vậy thầy.
hihihihi!!!!
thầy có thể giải thích sơ dùm em cái thưa thấy.
chúc mừng năm mới .
ditimniemvui said
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.Border;
public class fLogin extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
java.awt.Container con;
GridBagLayout gbl;
GridBagConstraints gbc;
BorderLayout bdLogin;
GridLayout gl;
JPanel pnLogin,pnEnd;
JTextField txtUser;
JPasswordField txtPass;
JLabel lblUser,lblPass;
JButton btnLogin,btnCancel;
public fLogin()
{
super();
con = getContentPane();
gbl = new GridBagLayout();
gbc = new GridBagConstraints();
gl = new GridLayout(1,2);
Border border = BorderFactory.createTitledBorder(“Form Login”);
bdLogin = new BorderLayout();
pnLogin = new JPanel();
pnEnd = new JPanel();
txtUser = new JTextField(12);
txtPass = new JPasswordField(12);
txtPass.setEchoChar(‘*’);
lblUser = new JLabel(“UserName”);
lblPass = new JLabel(“Password”);
btnLogin = new JButton(“Login”);
btnLogin.addActionListener(this);
btnCancel = new JButton(“Cancel”);
btnCancel.addActionListener(this);
pnLogin.setLayout(gbl);
pnLogin.setBorder(border);
gbc.gridx = 0;
gbc.gridy = 0;
pnLogin.add(lblUser,gbc);
gbc.gridx = 1;
pnLogin.add(txtUser,gbc);
gbc.gridx = 0;
gbc.gridy = 1;
pnLogin.add(lblPass,gbc);
gbc.gridx = 1;
pnLogin.add(txtPass,gbc);
pnEnd.setLayout(gl);
pnEnd.add(btnLogin);
pnEnd.add(btnCancel);
con.setLayout(bdLogin);
con.add(pnLogin,BorderLayout.CENTER);
con.add(pnEnd,BorderLayout.SOUTH);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==btnCancel)
{
System.exit(0);
}
if(e.getSource()==btnLogin)
{
txtUser.setText(“chua lien ket co so du lieu”);
}
}
public static void main(String[] args)
{
new fLogin().setVisible(true);
}
}
vovanhai said
EM phải học cách connect database đã. Nếu em là sv của tôi thì học kỳ tới chúng ta sẽ học. Chúc vui!
nguyenviettram said
happy new year 2009.
chúc thầy có 1 năm mới vui khỏe, thành công.
nhất là có nhiều tiền nha thầy.
hihihih.
vovanhai said
nếu trong eclipse, em phải tạo 1 thư mục img trong project(nhấn chuột phải lên project, chọn new->Folder) rồi copy hình vào đó.
Mai Quyen said
Em chào thầy,
Em mới học java và em có một bài tập muốn nhờ thầy giúp đỡ. Bài tâp của em như sau ạ:
Cho một tập dữ liệu file dạng txt
ví dụ :
Soda;1.2; 1.3; 1.5; 1.4; 1.5; 1.7; 1.8; 1.2; 1.3; 1.1; 1.6; 1.2
pome;2.0; 2.3; 2.4; 2.2; 2.5; 2.1; 2.6; 2.5; 2.9; 2.7; 3.0; 2.8
evian;0.8; 0.5; 0.7; 0.6; 0.5; 0.8; 0.7; 0.6; 0.5; 0.7; 0.9; 0.7
Mỗi dong của file gồm tên của đồ uống và giá của chúng trong từng tháng (tháng 1- tháng 12)
1,Yêu cầu đọc dữ liệu và tính trung bình của giá của từng loại đồ uống trong vòng 6 tháng đầu và 6 tháng cuối
2, Sửa đổi chương trình để đưa ra đồ thị của từng loại sản phẩm trong 12 tháng và mỗi sản phẩm sẽ có mầu khác nhau và có ghi chú trong đồ thị về tên của sản phẩm.
Thưa thầy, tính giá trị trung bình của từng sản phẩm trong 6 tháng đầu em đã viết. tuy nhiên phần sau em chưa nghĩ ra phải làm để sửa chương trình rồi đưa ra đồ thị. Em mong thầy hướng dẫn giúp ạ. Em cảm ơn thầy nhiều!
Đầy là chương trình tính giá trị trung bình của từng sản phẩm trong 6 tháng đâu a;
import java.io.*;
public class exercice33 {
public static void main(String[]args)throws java.io.IOException, java.io.FileNotFoundException{
String ligne;
String []Tab;
FileReader file= new FileReader(“C:\\Users\\Pham mai quyen\\Documents\\NetBeansProjects\\AtalierInformatique\\src\\Produit1.txt”);
BufferedReader in=new BufferedReader(file);
do{
ligne= in.readLine();
if(ligne!=null){
Tab=ligne.split(“;”);
double m=0;
for (int i=1;i<=6;i++){
System.out.print(Tab[i]+" ");
m +=Double.parseDouble(Tab[i])/6;
}
System.out.println();
System.out.println("Moyenne de prix de "+Tab[0]+" dans 6 premiers mois est: " + m);
}
}
while (ligne!=null);
}
}
mai said
mai said
bi loi khi nhan nut them khi load lai thi van co du lieu
Jimmy said
Dear,Thầy em tạo ra 4 cái Checkbox,nhưng nó nằm không thẳng cột.Cách nào để mình add Checkbox thẳng cột không lệch ra,lệch vô.
import java.awt.*;
import java.awt.event.*;
public class AL2 extends Frame implements WindowListener,ActionListener {
TextField filename1 = new TextField(20);
TextField filename2 = new TextField(20);
Button b;
private int numClicks = 0;
CheckboxGroup radioGroup;
Checkbox radio1;
Checkbox radio2;
Checkbox radio3;
Checkbox radio4;
public static void main(String[] args) {
AL2 myWindow = new AL2(“Superposition.java”);
myWindow.setSize(700,200);
myWindow.setVisible(true);
}
public AL2(String title) {
super(title);
setLayout(new FlowLayout());
addWindowListener(this);
b = new Button(“Go”);
add(b);
add(filename1);
add(filename2);
b.addActionListener(this);
radioGroup = new CheckboxGroup();
radio1 = new Checkbox(“Superposition by sequence alignment and modify the last column by RMSD”, radioGroup,false);
radio2 = new Checkbox(“Superposition by sequence alignment and modify the last column by temperature factor”, radioGroup,false);
radio3 = new Checkbox(“Superposition by 3D structure and modify the last column by RMSD”,radioGroup,true);
radio4 = new Checkbox(“Superposition by 3D structure and modify the last column by temperature factor”, radioGroup,true);
add(radio1);
add(radio2);
add(radio3);
add(radio4);
}
public void actionPerformed(ActionEvent e) {
numClicks++;
filename1.setText(“Heo kiss cop ” + numClicks + ” cai”);
}
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowOpened(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
}
Jimmy said
Nhờ thầy gợi ý:
cách tạo GUI cho cái chương trình tìm những từ giống nhau của 2 cái file.txt,hỏi người dùng nhập tên của file1,file2 sau đó mình nhấn button “go” run chương trình tạo ra file3 : chứa các từ đó.
Võ Văn Hải said
phải xem xét thuật toán lại. Nếu dùng vét cạn có lẽ là giải pháp sau cùng.
Võ Văn Hải said
không dùng FlowLayout, dùng BoxLayout the Y_AXIS là ok.
Bình said
em chào thầy, em là sinh viên, em đang học lập trình mạng bằng java netbean, thầy có thể hướng dẫn giúp em bài này được không ah,
ví dụ mình cho jcheckbox 1= Nguyễn, jcheckbox 2=Văn, jcheckbox 3=trường
khi mình check vào 1,2,3 thì se nối 3 chuổi là Nguyễn văn trường.
còn ngược lại nếu check 1,3 thì Nguyễn Trường.
em làm hoài nhung hok được
mong thầy giúp đỡ
em cảm ơn thầy
boyksochit said
Em chào thầy! Thầy ơi, em đang tìm hiểu về ngôn ngữ Java và muốn làm 1 chủ đề Ứng dụng Web (viết bằng ngôn ngữ Java). Thầy có thể góp ý cho em về vấn đề này không ạ? Em cảm ơn thầy
Hung said
em có cái Jtable với data như sau.
String dataValues[][] =
{{ “111”+”\n”+”aaa”, “222”+”\n”+”bbb”}, {“333″+”\n”+”ccc” },
{ “444”+”ddd”}…};
Sau khi hiển thị : 111aaa, 222bbb, 333ccc, 444ddd. nó không xuống hàng theo như “\n”
Thầy chỉ cách để em format text trong từng cell
Hung said
Em tạo jlist có 2 value : “111” ,”aaa” ,rồi add cái jlist vào cell của jtable ? có work không thầy. Nhờ thầy help
Nam said
Thầy cho em hỏi là làm thế nào để chèn Hình ảnh vào JTable, Em sử dụng Renderer theo hưỡng dẫn nhưng nó lại thực hiện với tất cả các ô trong cung 1 cột. Giờ em muốn các ô khác nhau có những hình ảnh khác nhau. Em cảm ơn thầy