ปัญหาคือผมต้องการให้เปลี่ยนสีพิ้นหลังของ jtextfield ขณะรับ input มาครับ แต่ผมลองหา google ดูแล้วครับ มาแก้ใน code ตัวเองแล้วมันก็ไม่ยอมเปลี่ยนสีเลยครับไม่ว่าจะเป็น input อะไรก็ตาม อยากรู้ว่าผมผิดตรงไหนอ่ะครับ
[Spoil] คลิกเพื่อดูข้อความที่ซ่อนไว้import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.Random;
import java.util.Scanner;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;
public class Suduku extends JFrame implements DocumentListener{
private JTextField cell;
private Font fontCell;
private JPanel gridPanel;
private int showValue;
private Scanner input;
private Random rand;
private int randomNumber;
private int height;
private int width;
private int x;
private int y;
private String[][] valueSet = {{"0", "0", "0"},
{"0", "0", "0"},
{"0", "0", "0"}};
private boolean[][] showStatus = {{false, false, false},
{false, false, false},
{false, false, false}};
public Suduku(){
super("Suduku Assignment");
startup();
settingNumber();
settingShowNumber();
setupGUIValue();
createMainGUI();
}
private void setupGUIValue(){
this.fontCell = new Font("Comic Sans MS", Font.BOLD, 20);
}
private void startup(){
System.out.print("Insert many digits which need to occur(3 - 6): ");
this.input = new Scanner(System.in);
this.showValue = input.nextInt();
this.height = 3;
this.width = 3;
}
private void settingShowNumber(){
int xPosition = 0, yPosition = 0;
rand = new Random();
while(this.showValue > 0){
xPosition = rand.nextInt(3);
yPosition = rand.nextInt(3);
if(!(showStatus[yPosition][xPosition])){
showStatus[yPosition][xPosition] = true;
this.showValue--;
}
}
}
private void settingNumber(){
int xAxis = 0, yAxis = 0;
this.rand = new Random();
System.out.println(this.height);
while(yAxis < this.height){
while(xAxis < this.width){
this.randomNumber = rand.nextInt(9) + 1;
if(IsIterateInHeight() || IsIterateInWidth() || IsIterateInColumn()){
this.randomNumber = rand.nextInt(9) + 1;
}
else{
valueSet[yAxis][xAxis] = Integer.toString(this.randomNumber);
this.randomNumber = rand.nextInt(9) + 1;
xAxis++;
}
}
yAxis++;
xAxis = 0;
}
}
private boolean IsIterateInColumn(){
for(int yAxis = 0; yAxis < this.height; yAxis++){
for(int xAxis = 0; xAxis < this.width; xAxis++){
if(this.randomNumber == Integer.parseInt(valueSet[yAxis][xAxis]))
return true;
}
}
return false;
}
private boolean IsIterateInHeight(){
for(int yAxis = 0; yAxis < this.height; yAxis++){
if(this.randomNumber == Integer.parseInt(valueSet[yAxis][this.width - 1])){
return true;
}
}
return false;
}
private boolean IsIterateInWidth(){
for(int xAxis = 0; xAxis < this.width; xAxis++){
if(this.randomNumber == Integer.parseInt(valueSet[this.height - 1][xAxis])){
return true;
}
}
return false;
}
private void checkIsvalid(int xPosition, int yPosition){
}
private void createMainGUI(){
Container controler = getContentPane();
controler.setLayout(new GridLayout(3, 3));
for (int yPosition = 0; yPosition < this.height; yPosition++) {
for(int xPosition = 0; xPosition < this.width; xPosition++){
this.x = xPosition;
this.y = yPosition;
gridPanel = new JPanel();
if(showStatus[yPosition][xPosition]){
this.cell = new JTextField(valueSet[yPosition][xPosition]);
this.cell.setEditable(false);
this.cell.setOpaque(true);
}
else{
this.cell = new JTextField("");
this.cell.setEditable(true);
this.cell.setOpaque(false);
}
controler.add(gridPanel);
gridPanel.setLayout(new GridLayout(1, 3)); //subtable
gridPanel.setPreferredSize(new Dimension(20, 20)); //subtable Dimension = 450px/3.
this.cell.setBackground(Color.WHITE);
this.cell.setFont(this.fontCell);
this.cell.setHorizontalAlignment(JTextField.CENTER);
this.cell.getDocument().addDocumentListener(this);
gridPanel.add(this.cell);
}
}
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
controler.setPreferredSize(new Dimension(200, 200)); //50px per cell.
pack();
setVisible(true);
setLocationRelativeTo(null);
}
public void changedUpdate(DocumentEvent event){
if(this.cell.getText() == valueSet[this.y][this.x]){
this.cell.setBackground(Color.GREEN);
}
else{
this.cell.setBackground(Color.RED);
}
}
public void insertUpdate(DocumentEvent event){}
public void removeUpdate(DocumentEvent event){}
public static void main(String[] args) {
new Suduku();
}
}
Java ครับ
[Spoil] คลิกเพื่อดูข้อความที่ซ่อนไว้