我有一个用jcombobox和jtextfield init创建框架的类,用户可以在其中插入一些数据。然后我有另一个类,用户使用swing调用该类。尽管swing类正常工作,但当我从另一个类调用它时,它只会在程序终止后出现。我需要它在用户插入数据之前打开,然后单击“确定”以便输入的数据可以在代码中进一步使用。
我用这个调用swing类main:
new constraints();
constraints.main(null);
和摇摆类主要是:
public static void main(String[] args)
{
Runnable runnable = new Runnable()
{
@Override
public void run()
{
new constraints().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
完整代码:
public class constraints {
private static JTextField tField;
private MyDocumentFilter documentFilter;
private JLabel amountLabel;
private static String amountString = "Select Quantity of Products (in ktones): ";
public static String textstr = "" ;
public static String sig="" ;
public static String sigfinal="";
private void displayGUI()
{
// final JFrame frame = new JFrame("Constraints");
final JDialog dialog = new JDialog();
// frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog.setDefaultCloseOperation(
JDialog.DISPOSE_ON_CLOSE);
amountLabel = new JLabel(amountString);
JPanel contentPane = new JPanel();
contentPane.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
tField = new JTextField(10);
amountLabel.setLabelFor(tField);
String[] Strings = { "Less", "Equal", "More"};
final JComboBox combo = new JComboBox(Strings) ;
combo.setSelectedIndex(0);
JButton button = new JButton("Ok");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
textstr = tField.getText();
sig = (String) combo.getSelectedItem();
if (sig=="Less") {
sigfinal="l";
}else if (sig=="Equal"){
sigfinal="e" ;
}else if (sig=="More"){
sigfinal="m";
}
}
});
JButton button2 = new JButton("Close");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
dialog.dispose();
}
});
((AbstractDocument)tField.getDocument()).setDocumentFilter(
new MyDocumentFilter());
contentPane.add(amountLabel);
contentPane.add(combo);
contentPane.add(tField);
contentPane.add(button);
contentPane.add(button2);
// JRootPane rootPane = frame.getRootPane();
JRootPane rootPane = dialog.getRootPane();
rootPane.setDefaultButton(button);
dialog.pack();
dialog.setVisible(true);
dialog.setContentPane(contentPane);
dialog.setLocationByPlatform(true);
// frame.setContentPane(contentPane);
// frame.pack();
// frame.setLocationByPlatform(true);
// frame.setVisible(true);
}
public static void main(String[] args)
{
Runnable runnable = new Runnable()
{
@Override
public void run()
{
new constraints().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
class MyDocumentFilter extends DocumentFilter
{
@Override
public void insertString(DocumentFilter.FilterBypass fp
, int offset, String string, AttributeSet aset)
throws BadLocationException
{
int len = string.length();
boolean isValidInteger = true;
for (int i = 0; i < len; i++)
{
if (!Character.isDigit(string.charAt(i)))
{
isValidInteger = false;
break;
}
}
if (isValidInteger)
super.insertString(fp, offset, string, aset);
else
Toolkit.getDefaultToolkit().beep();
}
@Override
public void replace(DocumentFilter.FilterBypass fp, int offset
, int length, String string, AttributeSet aset)
throws BadLocationException
{
int len = string.length();
boolean isValidInteger = true;
for (int i = 0; i < len; i++)
{
if (!Character.isDigit(string.charAt(i)))
{
isValidInteger = false;
break;
}
}
if (isValidInteger)
super.replace(fp, offset, length, string, aset);
else
Toolkit.getDefaultToolkit().beep();
}
}
错误:
Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The window is showing on screen.
at java.awt.Window.setLocationByPlatform(Unknown Source)
at constraints.displayGUI(constraints.java:101)
at constraints.access$1(constraints.java:29)
at constraints$3.run(constraints.java:116)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
答案 0 :(得分:2)
您需要使用某种对话框,这将在对话框可见时停止代码执行。
请查看How to make dialogs了解详情。
此时的基本建议是将JFrame
更改为JDialog
。确保你做模态。
根据对OP的更改进行了更新
不要忘记进行模态对话!
final JDialog dialog = new JDialog();
dialog.setModal(true);
dialog.setDefaultCloseOperation(
JDialog.DISPOSE_ON_CLOSE);
对话框可以是模态的。当模态对话框可见时,它会阻止用户 输入到程序中的所有其他窗口。 JOptionPane创建 JDialogs是模态的。要创建非模态对话框,您必须使用 直接使用JDialog类。
虽然略高一些,但您可以查看How to use modality in dialogs
根据其他例外更新
打开对话框/窗口的方式存在问题。它可能会生成一个大小为0x0的窗口。而不是......
dialog.pack();
dialog.setVisible(true);
dialog.setContentPane(contentPane);
dialog.setLocationByPlatform(true);
尝试使用...
dialog.setContentPane(contentPane);
dialog.pack();
dialog.setLocationByPlatform(true);
dialog.setVisible(true);
对代码进行最后一组更改后,对话框将按预期显示...
您可能还希望阅读Code Conventions for the Java Programming Language
<强>更新... 强>
将displayGUI
中的constraints
方法从private
更改为public
...
public void displayGUI() {
改变你的“跑步者”课程看起来更像......
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
Constraints constraints = new Constraints();
constraints.displayGUI();
System.out.println("Hello world");
}
});
问题是,constraints
main正在传递构建和显示UI的事件到事件调度线程(这是好的和正确的)并立即返回,让调用者代码继续运行... < / p>
知道那个电话链会打猎我......