download here Frame
download J frame here
អាចមើលcode បាន
- code use Frame:
package gui;
import java.awt.Button; import java.awt.Checkbox; import java.awt.CheckboxGroup;
import java.awt.Color; import java.awt.Frame; import java.awt.Label;
import java.awt.TextArea; import java.awt.TextField; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Form extends Frame{
private Label lblTitle,lblNum,lblMin,lblMax,lblSort;
private TextField txtNum,txtMin,txtMax;
private Button btnCreate,btnSort,btnExit;
private Checkbox chkAs,chkDs;
private TextArea lst;
private int a[];
public void setForm()
{
setTitle("Random Number");
// setBackground(Color.CYAN);
setSize(300, 300);
setVisible(true);
setLocationRelativeTo(null);
closeForm();
setLayout(null);
}
public void closeForm(){addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{System.exit(0);}
});
}
public void addControls(){
int h=20,w=50,x=50,y=40,i=10;
lblTitle=new Label("Random Number");
lblTitle.setBounds(100, y, 100, h);
lblNum=new Label("Number");
lblNum.setBounds(x, y+h, w, h);
lblMin=new Label("MinNum");
lblMin.setBounds(x, y+h*2, w, h);
lblMax=new Label("MaxNum");
lblMax.setBounds(x, y+h*3, w, h);
lblSort=new Label("Sort");
lblSort.setBounds(x, y+h*4, w, h);
add(lblSort);add(lblMax);add(lblMin);add(lblNum);add(lblTitle);
txtNum=new TextField();
txtNum.setBounds(x*2+i,y+h,w*2+i,h);
txtMin=new TextField();
txtMin.setBounds(x*2+i, y+h*2, w*2+i, h);
txtMax=new TextField();
txtMax.setBounds(x*2+i, y+h*3, w*2+i, h);
add(txtMax);add(txtMin);add(txtNum);
CheckboxGroup g =new CheckboxGroup();
chkAs=new Checkbox("Ascendent",g,false);
chkDs=new Checkbox("Descendent",g,false);
chkAs.setBounds(x*2, y+h*4, w+23, h);
chkDs.setBounds(x*3+25, y+h*4, w+35, h);
add(chkAs);add(chkDs);
btnCreate=new Button("Create");
btnCreate.setBounds(x, 145, w, h);
btnSort=new Button("Sort");
btnSort.setBounds(x*2+h, 145, w, h);
btnExit=new Button("Exit");
btnExit.setBounds(x*3+y, 145, w, h);
add(btnExit);add(btnSort);add(btnCreate);
lst=new TextArea();
lst.setBounds(x, 145+h, 200, 100);
add(lst);
}
public void exitevent(){
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
});
}
public void createevent(){
btnCreate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
createArray();
printStar(a);
}
});
}
public int[] createArray(){
int n,min,max;
n=Integer.parseInt(txtNum.getText());
min=Integer.parseInt(txtMin.getText());
max=Integer.parseInt(txtMax.getText());
if (txtNum.getText().toString().equals("")
|| txtMin.getText().toString().equals("")
|| txtMax.getText().toString().equals(""))
return null;
a = new int[n];
for (int i = 0; i < a.length; i++) {
a[i] = getRandom(min,max);
}
return a;
}
public int getRandom(int min, int max) {
return min + (int) (Math.random() * (max - min + min));
}
public void printStar(int a[]) {
for (int d = 0; d < a.length; d++) {
lst.append(a[d] + ":");
for (int i = 0; i < a[d]; i++) {
lst.append("*");
}
lst.append("\n");
}
}
public void sortevent(){
btnSort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if(chkAs.getState()==true){
aSort(a);}
if(chkDs.getState()==true){
dSort(a);
}
}
});
}
public void aSort(int k[]){
int t;
for(int i=0;i<k.length-1;i++)
for(int j=i+1;j<k.length;j++)
if(k[i]>k[j]){
t=k[i];k[i]=k[j];k[j]=t;
}
lst.setText(null);
printStar(k);
}
public void dSort(int k[]){
int t;
for(int i=0;i<k.length-1;i++)
for(int j=i+1;j<k.length;j++)
if(k[i]<k[j]){
t=k[i];k[i]=k[j];k[j]=t;
}
lst.setText(null);
printStar(k);
}
public Form()
{
setForm();
addControls();
exitevent();
createevent();
sortevent();
}
public static void main(String[] args)
{
new Form();
}
}
- code use J frame
package hwgame;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.text.AbstractDocument.Content;
public class Hwgame extends JFrame {
private JLabel l1, l2, l3, l4, l5;
private JRadioButton rbAce, rbDec;
private JTextField txt1, txt3, txt2;
private JButton btnExit, btnCreate, btnSort;
private JTextArea txtshow;
private String num[];;
public void CreateForm() {
setLayout(null);
setSize(400, 500);
setVisible(true);
setTitle("Form");
setLocationRelativeTo(null);
Container con;
con = getContentPane();
con.setBackground(Color.YELLOW);
}
public void inCon() {
l1 = new JLabel("Create Array Random Value");
l1.setBounds(120, 50, 500, 20);
add(l1);
l2 = new JLabel("Number");
l2.setBounds(70, 70, 50, 20);
add(l2);
txt1 = new JTextField(50);
txt1.setBounds(150, 70, 100, 20);
add(txt1);
l3 = new JLabel("Min Value");
l3.setBounds(70, 90, 70, 20);
add(l3);
txt2 = new JTextField(50);
txt2.setBounds(150, 90, 100, 20);
add(txt2);
l4 = new JLabel("Max Value");
l4.setBounds(70, 110, 70, 20);
add(l4);
txt3 = new JTextField(50);
txt3.setBounds(150, 110, 100, 20);
add(txt3);
l5 = new JLabel("Type Sort");
l5.setBounds(70, 130, 70, 20);
add(l5);
rbAce = new JRadioButton("Acending");
rbAce.setBounds(150, 130, 100, 20);
add(rbAce);
rbDec = new JRadioButton("Decending");
rbDec.setBounds(250, 130, 100, 20);
add(rbDec);
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(rbAce);
buttonGroup.add(rbDec);
Box box = Box.createHorizontalBox();
box.setBounds(70, 150, 200, 50);
box.add(btnCreate = new JButton("Create"));
box.add(Box.createRigidArea(new Dimension(5, 0)));
box.add(btnSort = new JButton("Sort"));
box.add(Box.createRigidArea(new Dimension(5, 0)));
box.add(btnExit = new JButton("Exit"));
add(box);
txtshow = new JTextArea(200, 250);
txtshow.setBounds(50, 200, 300, 250);
JScrollBar bar = new JScrollBar();
bar.add(txtshow);
add(txtshow);
add(bar);
txtshow.setEditable(false);
txtshow.setBackground(Color.GREEN);
}
void sortDe(String d[]) {
String tmp;
for (int i = 0; i < d.length; i++) {
for (int j = i + 1; j < d.length; j++) {
if (Integer.parseInt(d[i].toString()) < Integer.parseInt(d[j]
.toString())) {
tmp = d[i];
d[i] = d[j];
d[j] = tmp;
}
}
}
printStar(d);
}
void sortAe(String d[]) {
String tmp;
for (int i = 0; i < d.length; i++) {
for (int j = i + 1; j < d.length; j++) {
if (Integer.parseInt(d[i].toString()) > Integer.parseInt(d[j]
.toString())) {
tmp = d[i];
d[i] = d[j];
d[j] = tmp;
}
}
}
printStar(d);
}
public void Sort() {
btnSort.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (rbAce.isSelected()) {
txtshow.setText("");
sortAe(num);
JOptionPane.showMessageDialog(null, "Sort Acending ");
return;
}
if (rbDec.isSelected()) {
txtshow.setText("");
sortDe(num);
JOptionPane.showMessageDialog(null, "Sort Decending");
return;
}
else {
JOptionPane.showMessageDialog(null,
"Please Choose One Before Click Sort");
return;
}
}
});
}
public void printStar(String show[]) {
for (int d = 0; d < show.length; d++) {
txtshow.append(show[d] + "");
for (int i = 0; i < Integer.parseInt(show[d]); i++) {
txtshow.append("*");
}
txtshow.append("\n");
}
}
public String getRandom(int min, int max) {
return min + (int) (Math.random() * (max - min + min)) + "";
}
public void Start() {
btnCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (txt1.getText().toString().equals("")
|| txt2.getText().toString().equals("")
|| txt3.getText().toString().equals(""))
return;
num = new String[Integer.parseInt(txt1.getText().toString())];
for (int i = 0; i < num.length; i++) {
num[i] = getRandom(
Integer.parseInt(txt2.getText().toString()),
Integer.parseInt(txt3.getText().toString()));
}
printStar(num);
btnCreate.setEnabled(false);
}
});
}
public void exit() {
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
}
public Hwgame() {
inCon();
CreateForm();
Start();
exit();
Sort();
}
public static void main(String agrs[]) {
new Hwgame();
}
}
No comments:
Post a Comment