我正在构建我的第一个 gui,到目前为止一切正常,除了JDialog
. 它在第一次使用时相应地接受名称和进程列表。但是当我把它拉回来输入新的输入时,它仍然没有响应。我认为这不是线程问题,因为我已经在System.out.println ( SwingUtilities.isEventDispatchThread() );
整个源代码中使用多个语句测试了代码。这是可能导致问题的部分代码。
package testme;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Test {
JDialog dialog;
JButton horseList, ok, clear;
JPanel jpDialog = new JPanel();
JPanel buttonPanel = new JPanel();
GridBagLayout gbLayout = new GridBagLayout();
BorderLayout borderLayout = new BorderLayout();
GridBagConstraints gbc = new GridBagConstraints();
int fnh = 8;
JTextField[] jtxt = new JTextField[fnh];
int[] hNum = new int[fnh];
int[] hVal = new int[fnh];
String[] hNam = new String[fnh];
JFrame jfr = new JFrame();
public Test() {
jfr.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
jfr.setTitle("My Alladin Lamp");
jfr.setSize( 200, 80 );
jfr.setVisible( true );
jfr.setLayout( borderLayout );
horseList = new JButton( "Enter Horse Names" );
jfr.add( horseList, BorderLayout.CENTER );
horseList.addActionListener( new ActionListener() {
@Override
public void actionPerformed( ActionEvent e ) {
dialog = new JDialog( jfr, "Enter Horse Names", true );
dialog.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
dialog.setSize( 260, 400 );
jpDialog.setLayout( gbLayout );
JLabel label;
String str;
for( int i = 0; i < fnh; i++ )
{
gbc.gridx = 0;
gbc.gridy = i;
str = new Integer( i+1 ) + ".";
label = new JLabel( str );
jpDialog.add( label, gbc );
gbc.gridx = 1;
gbc.gridy = i;
gbc.ipady = 4;
gbc.insets = new Insets(4,0,0,0);
jtxt[i] = new JTextField(15);
jpDialog.add( jtxt[i], gbc );
}
buttonPanel = new JPanel();
ok = new JButton( "OK" );
ok.addActionListener( new ActionListener() {
@Override
public void actionPerformed( ActionEvent e ) {
for( int i = 0; i < fnh; i++ ) {
hNam[i] = jtxt[i].getText();
}
dialog.dispose();
}
});
buttonPanel.add( ok );
clear = new JButton ( "CLEAR" );
clear.addActionListener( new ActionListener() {
@Override
public void actionPerformed( ActionEvent e ) {
for( int i = 0; i < fnh; i++ )
if ( !"".equals( jtxt[i].getText() ) )
jtxt[i].setText( "" );
}
});
buttonPanel.add( clear );
JScrollPane jscr = new JScrollPane( jpDialog );
dialog.add( jscr, BorderLayout.CENTER );
dialog.add( buttonPanel, BorderLayout.SOUTH );
dialog.setVisible( true );
}
});
}
// -------------------------------------------------------------------------
public static void main( String args[] ) {
SwingUtilities.invokeLater( new Runnable() {
@Override
public void run()
{
Test test = new Test();
}
});
}
}