我正在使用递归方法为用户输入的Boggle板上的JButton着色。例如,如果单词“CAT”用作单词参数,该方法将搜索按钮[] []数组以找到旁边有“A”的“C”和旁边的“T”和将相应的按钮着色为橙色并返回true。但是,我的代码有一个问题,像CATATATAT这样的单词也会返回true,因为我的代码无意中允许重用按钮。
问题是我无法检查JButton是否已经使用过(已经是橙色)。当我在递归方法中使用getBackground()时,我发现JButtons仍然是默认的白色,即使它们是视觉上是橙色的。有关如何检测JButton已被使用的任何想法(已经是橙色)?
JButton[][] buttons = new JButton[4][4];
Color orange = new Color(245, 130, 32);
假设JButton数组在每个
上都填充了一个随机字母的新按钮public boolean findWord(String word) {
clearButtons();
for (int row = 0; row < length; row++) {
for (int col = 0; col < length; col++) {
if (findWord(word, row, col)) {
System.out.println("");
return true;
}
}
}
return false;
}
private boolean findWord(String word, int row, int col) {
if(word.equals("")) {
return true;
}
if (row < 0 || row >= length ||
col < 0 || col >= length ||
!(buttons[row][col].getText().equals(word.substring(0,1))) ||
buttons[row][col].getBackground().equals(orange) // <- always is false
) {
return false;
}
String rest = word.substring(1, word.length());
boolean letter =
findWord(rest, row-1, col-1) ||
findWord(rest, row-1, col) ||
findWord(rest, row-1, col+1) ||
findWord(rest, row, col-1) ||
findWord(rest, row, col+1) ||
findWord(rest, row+1, col-1) ||
findWord(rest, row+1, col) ||
findWord(rest, row+1, col+1);
if(letter) {
buttons[row][col].setBackground(orange); // <- despite this
}
return letter;
}
我试图尽可能地减少程序以使其可运行。只需在文本区域键入一些重复部分单词的内容即可。 这是一个完整的可运行版本:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.text.JTextComponent;
public class BoggleGame extends JFrame implements ActionListener, KeyListener {
private static final long serialVersionUID = -3125864417227564060L;
String[] wordArray = new String[0];
static String username;
int length = 4;
public JTextArea searchTextArea = new JTextArea(0,12);
DefaultListModel wordListModel= new DefaultListModel();
JButton[][] buttons = new JButton[length][length];
JList wordList = new JList(wordListModel);
JScrollPane wordScrollPane = new JScrollPane(wordList);
JPanel northPanel = new JPanel();
JPanel timePanel = new JPanel();
JPanel usernamePanel = new JPanel();
JPanel southPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel eastPanel = new JPanel();
ImageIcon logo = new ImageIcon(getClass().getResource("logo.png"));
Color white = new Color(255,255,255);
Color blue = new Color(0,150 ,240);
Color orange = new Color(245, 130, 32);
Color dark_orange = new Color(180, 117 , 70);
public static void main(String[] arguments){
new BoggleGame();
}
public BoggleGame(){
// Set Title
super("");
// Set up Frame
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set Layout
getContentPane().setLayout(new BorderLayout());
nimbusLookAndFeel();
// North Panel
northPanel.setLayout(new GridLayout(1,3));
northPanel.setPreferredSize(new Dimension(550,100));
northPanel.setBackground(blue);
// Username Panel
usernamePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
usernamePanel.setBackground(blue);
JLabel usernameLabel = new JLabel("Username:");
usernamePanel.add(usernameLabel);
JTextArea usernameTextArea = new JTextArea();
usernameTextArea.setText(Login.username);
usernamePanel.add(usernameTextArea);
northPanel.add(usernamePanel);
usernameTextArea.setEditable(false);
add(northPanel, BorderLayout.NORTH);
northPanel.add(usernamePanel);
// Logo
northPanel.add(new JLabel(logo));
// Time Panel
timePanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
timePanel.setBackground(blue);
JLabel timeLabel = new JLabel("Time:");
timePanel.add(timeLabel);
JTextArea timeTextArea = new JTextArea();
timeTextArea.setText("0");
timePanel.add(timeTextArea);
northPanel.add(timePanel);
timeTextArea.setEditable(false);
add(northPanel, BorderLayout.NORTH);
// South Panel
southPanel.setPreferredSize(new Dimension(550,50));
southPanel.setBackground(blue);
searchTextArea.setLineWrap(true);
southPanel.add(searchTextArea);
searchTextArea.setBorder(BorderFactory.createLineBorder(Color.BLACK));
searchTextArea.setFont(new Font("Geniva", Font.PLAIN, 30));
searchTextArea.addKeyListener((KeyListener) this);
JButton submitButton = new JButton("Submit");
submitButton.addActionListener(this);
southPanel.add(submitButton);
JButton quitButton = new JButton("Quit");
quitButton.addActionListener(this);
southPanel.add(quitButton);
getContentPane().add(southPanel, BorderLayout.SOUTH);
// Center Panel
centerPanel.setPreferredSize(new Dimension(400,400));
centerPanel.setBackground(blue);
centerPanel.setLayout(new GridLayout(length, length));
//generate buttons
for(int i=0; i<length; i++) {
for(int j=0; j<length; j++) {
buttons[i][j]=new JButton(Character.toString(((char)(int)(Math.random() * 25 + 65))));
buttons[i][j].setBackground(white);
//buttons[i][j].setForeground(white);
buttons[i][j].setFont(new Font("Geniva", Font.BOLD, 200/length));
buttons[i][j].addActionListener(this);
centerPanel.add(buttons[i][j]);
}
}
getContentPane().add(centerPanel, BorderLayout.CENTER);
// East Panel
eastPanel.add(wordScrollPane);
wordScrollPane.setPreferredSize(new Dimension(150,400));
eastPanel.setBackground(blue);
getContentPane().add(eastPanel, BorderLayout.EAST);
// Finalize
pack();
searchTextArea.requestFocusInWindow();
setLocationRelativeTo(null);
setVisible(true);
}
public void nimbusLookAndFeel() {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
}
}
public void clearButtons() {
for(int i= 0; i<buttons.length; i++) {
for(int j=0; j<buttons[0].length; j++) {
buttons[i][j].setBackground(white);
}
}
}
public void submitWord() {
if(findWord(searchTextArea.getText()) && !(searchTextArea.getText().equals(""))){
wordListModel.addElement(searchTextArea.getText().toUpperCase());
} else {
wordListModel.addElement("Not a word!");
}
searchTextArea.getDocument().putProperty("filterNewlines", Boolean.TRUE);
wordList.ensureIndexIsVisible(wordListModel.size()-1);
searchTextArea.setText("");
}
@Override
public void actionPerformed(ActionEvent e) {
// Check if source is the Submit Button
if(((JButton) e.getSource()).getText().equals("Submit")){
submitWord();
// Check if source is the Quit Button
} else if(((JButton) e.getSource()).getText().equals("Quit")){
dispose();
// Source must be a Letter Button
} else if(!((JButton) e.getSource()).getBackground().equals(orange)){
searchTextArea.append(((JButton) e.getSource()).getText());
((JButton) e.getSource()).setBackground(orange);
}
searchTextArea.requestFocusInWindow();
}
@Override
public void keyPressed(KeyEvent e) {
searchTextArea.setText(searchTextArea.getText().toUpperCase());
if (e.getKeyCode()==KeyEvent.VK_ENTER){
submitWord();
}
}
@Override
public void keyReleased(KeyEvent e) {
searchTextArea.setText(searchTextArea.getText().toUpperCase());
if(!findWord(searchTextArea.getText())) {
clearButtons();
}
if (e.getKeyCode()==KeyEvent.VK_ENTER){
searchTextArea.setText("");
}
}
@Override
public void keyTyped(KeyEvent e) {
}
// here is the method that does not work!
public boolean findWord(String word) {
clearButtons();
for (int row = 0; row < length; row++) {
for (int col = 0; col < length; col++) {
if (findWord(word, row, col)) {
System.out.println("");
return true;
}
}
}
return false;
}
private boolean findWord(String word, int row, int col) {
if(word.equals("")) {
return true;
}
if (row < 0 || row >= length ||
col < 0 || col >= length ||
!(buttons[row][col].getText().equals(word.substring(0,1))) ||
buttons[row][col].getBackground().equals(orange) //<-doesn't work
) {
return false;
}
System.out.println(buttons[row][col].getBackground());
String rest = word.substring(1, word.length());
boolean letter =
findWord(rest, row-1, col-1) ||
findWord(rest, row-1, col) ||
findWord(rest, row-1, col+1) ||
findWord(rest, row, col-1) ||
findWord(rest, row, col+1) ||
findWord(rest, row+1, col-1) ||
findWord(rest, row+1, col) ||
findWord(rest, row+1, col+1);
if(letter) {
buttons[row][col].setBackground(orange);
}
return letter;
}
}
答案 0 :(得分:0)
Color
获取此组件的背景颜色。
返回:此组件的背景颜色;如果这个组件没有 有背景颜色,其父级的背景颜色是 返回
此方法返回Color.orange
的实例,因此您需要比较Color.ORANGE
或public function contact_process()
{
$name=$this->input->post('txt_name');
$phone=$this->input->post('txt_phone');
$from_email=$this->input->post('txt_email');
$message=$this->input->post('txt_message');
$config = Array(
'protocol' => 'sendmail',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'my_email_id@gmail.com',
'smtp_pass' => 'my_password',
'mailtype' => 'html',
'charset' => 'utf-8',
'priority' => '1',
'wordwrap' => 'true'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$to_email = 'to_email_id@gmail.com';
$subject = "Product Request Mail";
$body = "<html>
<head><title>Best Price</title></head>
<body>
<div style='max-width: 800px; margin: 0; padding: 30px 0;'>
<table width='80%' border='1' cellpadding='0' cellspacing='0'>
<tr>
<td width='5%'></td>
<td align='left' width='95%' style='font: 13px/18px Arial, Helvetica, sans-serif;'>
<h2 style='font: normal 20px/23px Arial, Helvetica, sans-serif; margin: 0; padding: 0 0 18px; color: black;'>Contact Mail</h2>
<br />
<big style='font: 16px/18px Arial, Helvetica, sans-serif;'> Name : '.$name.'</big><br />
<big style='font: 16px/18px Arial, Helvetica, sans-serif;'> Phone : '.$phone.'</big><br />
<big style='font: 16px/18px Arial, Helvetica, sans-serif;'> Email : '.$from_email.'</big><br />
<big style='font: 16px/18px Arial, Helvetica, sans-serif;'> Message : '.$message.'</big><br />
</td>
</tr>
</table>
</div>
</body>
</html>";
//send mail
$this->email->from($from_email, $name);
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($body);
//echo $this->email->send();
//echo $this->email->print_debugger();
if ($this->email->send())
{
redirect('user/bestprice/contact/', 'refresh');
}
else
{
redirect('user/bestprice/contact/', 'refresh');
}
}
答案 1 :(得分:0)
感谢大家的帮助,我能够通过添加按钮数组作为参数来找到答案。
public boolean findWord(String word, int row, int col, JButton[][] buttons)
最终方法看起来像这样。
public boolean findWord(String word) {
clearButtons(); //make buttons white again
for (int row = 0; row < length; row++) {
for (int col = 0; col < length; col++) {
if (findWord(word, row, col, buttons)) {
return true;
}
}
}
return false;
}
private boolean findWord(String word, int row, int col, JButton[][] buttons) {
if(word.equals("")) {
return true;
}
if (row < 0 || row >= length ||
col < 0 || col >= length ||
!(this.buttons[row][col].getText().equals(word.substring(0,1))) ||
buttons[row][col].getBackground().equals(orange)
) {
return false;
}
String rest = word.substring(1, word.length());
boolean letter =
findWord(rest, row-1, col-1, buttons) ||
findWord(rest, row-1, col, buttons) ||
findWord(rest, row-1, col+1, buttons) ||
findWord(rest, row, col-1, buttons) ||
findWord(rest, row, col+1, buttons) ||
findWord(rest, row+1, col-1, buttons) ||
findWord(rest, row+1, col, buttons) ||
findWord(rest, row+1, col+1, buttons);
if(!letter) {
this.buttons[row][col].setBackground(white);
}
return letter;
}
}