Hiện tại thì mình mới bắt đầu học Java GUI, khi làm bài tập thấy có phần JTextArea chỗ này, không biết phải làm như thế nào để mình có thanh cuộn, mình cũng đã thử tìm trên google về JList nhưng vẫn không hiểu mấy @@
Còn đây là code của mình
package bai02;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class SoNguyenTo extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextField txtSNT;
private JButton buttonG;
private JTextArea txtArea;
public SoNguyenTo(){
setTitle("Primes");
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
createGUI();
}
private void createGUI() {
setLayout(null);
add(txtSNT = new JTextField());
int x=50,y=10,width=200,height=20;
txtSNT.setBounds(x, y, width, height);
add(buttonG = new JButton("Generate"));
buttonG.setBounds(x+=210, y, width-=110, height);
add(txtArea = new JTextArea());
txtArea.setBounds(50, y+=30, 300, 200);
txtArea.setEditable(false);
buttonG.addActionListener(this);
}
public static void main(String[] args) {
new SoNguyenTo().setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
if(o.equals(buttonG)){
int n = Integer.parseInt(txtSNT.getText());
int dem=0,run=2;
String let="";
while(dem!=n){
if(kiemTraSoNguyenTo(run)){
dem++;
let+= (run + "\n");
}
run++;
}
txtArea.setText(let);
}
}
private boolean kiemTraSoNguyenTo(int x){
int i,m;
if(x==2 || x==3)
return true;
if(x==1 || x%2==0 || x%3==0)
return false;
m=(int)Math.sqrt(x);
for(i=5;i<=m;i+=6){
if(x%i==0 || x%(i+2)==0)
return false;
}
return true;
}
}



83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?