[英]Thread Errors involving Swing's EDT
As you can see from my code I am totally missing a whole gaping concept here. 正如您从我的代码中看到的那样,我在这里完全缺少一个完整的概念。 My goal is to have the user input:
我的目标是让用户输入:
Jbutton text field: text orientation and mnemonic Jbutton文本字段:文本方向和助记符
I pass that info to my Button class that checks for errors and gives back a button already defined. 我将该信息传递给我的Button类,该类检查错误并返回已经定义的按钮。
Then populate the JFrame with said button as to the user specs. 然后,根据用户要求,使用上述按钮填充JFrame。
BASIC and obviously a course question, but I'm at my wit's end here. BASIC显然是一个课程问题,但我的智慧到此为止。 This is the first time I've asked for help so please take it easy on me.
这是我第一次寻求帮助,因此请放心。
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
/**
* This class will create a button using the Button class and with user input to define the instance
* of the Button Class.
*
*/
public class CreateButton extends JPanel
{
// instance variables
private static String userIn; // user input
private static Button userButton; // button to be manipulated
public static JButton createdButton; // finished button
/**
* Contructor for the CreateButton class.
*/
public static void main (String [] args)
{
System.out.println("\nThis program will create a button with some input for you.");
System.out.println("What would you like to call this button?");
userIn = StringIn.get();
userButton = new Button();
userButton.buttonText(userIn);
System.out.println("\nYou can orient your text on the button in the vertical and");
System.out.println("horizontal axis. We will start with the vertical. Where would you like");
System.out.println("the text, on the top, center, or bottom?");
userIn = StringIn.get();
String vertical = userIn;
System.out.println("\nNext let's select the horizontal alignment. Would you like the text");
System.out.println("aligned to the left, center, or right?");
userIn = StringIn.get();
String horizontal = userIn;
userButton.textPosition(vertical,horizontal);
System.out.println("\nFinally let's add a mnemonic or hotkey to the button. Pick a letter");
System.out.println("from a-z on the keyboard and you can activate the button by pushing");
System.out.println("ALT + your letter of choice. Please enter your letter:");
userIn = StringIn.get();
userButton.buttonMnemomic(userIn);
System.out.println("\nGreat let's create and see this button.");
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
//Create and set up the window.
JFrame frame = new JFrame("Create a Button");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
CreateButton newContentPane = new CreateButton();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
}
My button error checker code is as follows: 我的按钮错误检查器代码如下:
import javax.swing.AbstractButton;
import javax.swing.JButton;
/**
* This class will demonstrate the use of a button.
*
*/
public class Button
{
// instance variables
protected JButton myButton; // button to be created and manipulated
private String myButtonText; // text on the button
private String myButtonVerticalTextPosition;
private String myButtonHorizontalTextPosition;
private String myButtonMnemonic; // hotkey for the button
/**
* Constructor for objects of class Button
*/
public Button()
{
}
/**
*Set button text. String input.
*/
public void buttonText(String textIn)
{
myButtonText = textIn;
}
/**
*Set button text position. String input for vertical (top, center, bottom) and horizontal
*(left, center, right).
*/
public void textPosition(String verticalIn, String horizontalIn)
{
myButtonVerticalTextPosition = verticalIn;
boolean validInput = false;
do
{
if ((myButtonVerticalTextPosition.compareToIgnoreCase("top") == 0)||
(myButtonVerticalTextPosition.compareToIgnoreCase("centre") == 0) ||
(myButtonVerticalTextPosition.compareToIgnoreCase("center") == 0) ||
(myButtonVerticalTextPosition.compareToIgnoreCase("bottom") == 0))
{
validInput = true;
} else
{
System.out.println("\nPlease enter top, center, or bottom for vertical position:");
myButtonVerticalTextPosition = StringIn.get();
}
} while (validInput == false);
myButtonHorizontalTextPosition = horizontalIn;
validInput = false;
do
{
if ((myButtonHorizontalTextPosition.compareToIgnoreCase("left") == 0) ||
(myButtonHorizontalTextPosition.compareToIgnoreCase("centre") == 0) ||
(myButtonHorizontalTextPosition.compareToIgnoreCase("center") == 0) ||
(myButtonHorizontalTextPosition.compareToIgnoreCase("right") == 0))
{
validInput = true;
} else
{
System.out.println("\nPlease enter left, center, or right for horizontal position:");
myButtonHorizontalTextPosition = StringIn.get();
}
} while (validInput == false);
}
/**
*Set button mnemomic. String input for mnemomic [a-z].
*/
public void buttonMnemomic(String mnemomicIn)
{
myButtonMnemonic = mnemomicIn;
boolean validInput = false;
do
{
if (myButtonMnemonic.length() > 1)
{
System.out.println("\nPlease enter a letter from a-z: ");
myButtonMnemonic = StringIn.get();
} else if (!myButtonMnemonic.matches("^[a-zA-Z]+$"))
{
System.out.println("\nPlease enter a letter from a-z: ");
myButtonMnemonic = StringIn.get();
} else if ((myButtonMnemonic.length() == 1) &&
(myButtonMnemonic.matches("^[a-zA-Z]+$")))
{
validInput = true;
}
} while (validInput == false);
}
/**
*Create button. Void method to create the button to the variables provided.
*/
public void createButton()
{
// create new button
myButton = new JButton(myButtonText);
// set text position
switch (myButtonVerticalTextPosition)
{
case "top":
myButton.setVerticalTextPosition(AbstractButton.TOP);
break;
case "centre":
myButton.setVerticalTextPosition(AbstractButton.CENTER);
break;
case "center":
myButton.setVerticalTextPosition(AbstractButton.CENTER);
break;
case "bottom":
myButton.setVerticalTextPosition(AbstractButton.BOTTOM);
break;
default:
System.err.format("%n%s is an invalid entry.", myButtonVerticalTextPosition);
break;
}
switch (myButtonHorizontalTextPosition)
{
case "left":
myButton.setHorizontalTextPosition(AbstractButton.LEADING);
break;
case "centre":
myButton.setHorizontalTextPosition(AbstractButton.CENTER);
break;
case "center":
myButton.setHorizontalTextPosition(AbstractButton.CENTER);
break;
case "right":
myButton.setHorizontalTextPosition(AbstractButton.TRAILING);
break;
default:
System.err.format("%n%s is an invalid entry.", myButtonVerticalTextPosition);
break;
}
// set button mnemonic
StringBuilder hotKey = new StringBuilder("KeyEvent.VK_");
hotKey.append(myButtonMnemonic.toUpperCase());
myButton.setMnemonic(hotKey.charAt(0));
// set tool tip text
myButton.setToolTipText("Push the button. You know you want to.");
}
/**
*Returns a JButton for the button type.
*/
public JButton returnButton()
{
return myButton;
}
}
This all works up to the part where you add the "createdButton". 所有这些都可以在添加“ createdButton”的部分完成。 If I make it a default button it goes through the motions and puts up the default button.
如果我将其设置为默认按钮,它将经历所有动作并提出默认按钮。
FYI this is my StringIn code: 仅供参考,这是我的StringIn代码:
import java.util.Scanner;
import java.io.IOException;
/**
* This class will allow the user to type in string from the console.
*
*/
public class StringIn
{
// instance variables
private static String userIn;
public static String get()
{
try (Scanner in = new Scanner(System.in))
{
userIn = new String(in.nextLine()); // Read the string from console.
}
return userIn;
}
}
For the StringIn class just do: 对于StringIn类,只需执行以下操作:
import java.util.Scanner;
public class StringIn
{
// instance variables
private static Scanner scanner = new Scanner(System.in);
public static String get(){
return scanner.nextLine();
}
}
Edit2: 编辑2:
Okay, so I copied your code into my IDE and the error I got was an error originating in your StringIn class. 好的,所以我将您的代码复制到我的IDE中,并且得到的错误是源自您的StringIn类的错误。 ( I don't remember actually but that doesn't really matter. )
(我实际上不记得了,但这并不重要。)
Your StringIn class should look like the above example. 您的StringIn类应类似于上面的示例。
For your createAndShowGUI() function: 对于您的createAndShowGUI()函数:
private static void createAndShowGUI()
{
//Create and set up the window.
JFrame frame = new JFrame("Create a Button");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane. ( Why were you even doing this? )
frame.add(createdButton); //( To display the actual button you need to
//add it to the frame)
//Display the window.
frame.pack();
frame.setVisible(true);
}
Do like that^ 那样做^
I couldn't get the mnemonic related things to work properly, and I don't want to spend so much time on it, so I just removed them. 我无法使与助记符相关的事情正常工作,并且我不想花太多时间在上面,因此我将其删除。 The orientation thingies didn't work either.
方向的东西也不起作用。
Put this at the bottom in main(String args[]) 将其放在main(String args [])的底部
createdButton = userButton.createButton(); // Make createButton() return Button;
You don't need JButton anywhere in your code except for this part: 除了以下部分,您不需要在代码中的任何位置使用JButton:
public class Button extends JButton
// And of course the import statement...
You can use Button.TOP
instead of AbstractButton.TOP
which will get rid of an import statement for you. 您可以使用
Button.TOP
而不是AbstractButton.TOP
,它会为您消除导入语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.