import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
class Main {
public static void main( String[] args ) {
InputFrame frame = new
InputFrame();
}
}
class InputFrame extends JFrame implements
ActionListener {
static ArrayList<JComboBox<String>> Select
= new ArrayList<JComboBox<String>>();
public InputFrame() {
setDefaultCloseOperation(
EXIT_ON_CLOSE );
setResizable( false );
getContentPane().setPreferredSize(
new Dimension( 300, 300 ) );
pack();
getContentPane().setLayout( null
);
String Data[] = { /*1から10の連番
*/ };
for( int i = 0; i < 10; i++ ) {
Select.add(new
JComboBox<String>( Data ));
Select.get(i).setBounds(0,
20*i, 100, 20 );
getContentPane().add(
Select.get(i) );
}
JButton OKButton = new JButton(
"OK" );
OKButton.addActionListener( this
);
OKButton.setActionCommand( "OK" );
OKButton.setBounds( 0, 20*10, 100,
20 );
getContentPane().add( OKButton );
setVisible( true );
}
public void actionPerformed(ActionEvent e)
{
if( e.getActionCommand().equals(
"OK" ) ) {
System.out.println(
String.valueOf( Select.size() ) );
}
}
}
このコードだと上手く表示されるのですが、
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
class InputFrame extends JFrame implements
ActionListener {
static ArrayList<JComboBox<String>>
TeamSelect = new ArrayList<JComboBox<String>>();
public InputFrame() {
setDefaultCloseOperation(
EXIT_ON_CLOSE );
setLocationRelativeTo( null );
setResizable( false );
getContentPane().setPreferredSize(
new Dimension( 300, 300 ) );
pack();
getContentPane().setLayout( null
);
/*チーム名選択*/
String TeamData[][] =
{ { "東北楽天", "西武", "ロッテ", "ソフトバ
ンク", "オリックス", "日本ハム" },
{ "巨人", "阪神", "広島", "中日", "DeNA", "東
京ヤクルト" } };
String Data[] = { /*1から10の連番
*/ };
ArrayList<JComboBox<String>>
TeamSelect = new ArrayList<JComboBox<String>>();
for( int i = 0; i < 2; i++ ) {
TeamSelect.add(new
JComboBox<String>( TeamData[i] ));
TeamSelect.get(i).setBounds(
getContentPane().getWidth()/2*i, 0,
getContentPane().getWidth()/2, 20 );
getContentPane().add(
TeamSelect.get(i) );
}
/*選手名入力*/
JTextField PlayerNameBox[][] = new
JTextField[2][10];
for( int j = 0; j < 2; j++ ) {
for( int i = 0; i < 10;
i++ ) {
PlayerNameBox[j]
[i] = new JTextField();
PlayerNameBox[j]
[i].setBounds( getContentPane().getWidth()/2*j,
20*i +20, getContentPane().getWidth()/2-50, 20 );
getContentPane().add( PlayerNameBox[j][i] );
}
}
/*選手ポジション*/
ArrayList<ArrayList<JComboBox<String>>>
PositionSelect = new
ArrayList<ArrayList<JComboBox<String>>>();
for( int j = 0; j < 2; j++ ) {
PositionSelect.add( new
ArrayList<JComboBox<String>>() );
for( int i = 0; i < 10;
i++ ) {
PositionSelect.get(j).add( new JComboBox<String>(
V.PlayerPos[j] ) );
PositionSelect.get(j).get(i).setBounds(
getContentPane().getWidth()/2*(j+1)-50, 20*i +20,
50, 20 );
getContentPane().add( PositionSelect.get(j).get(i)
);
}
}
/*ボタン*/
JButton PlayButton = new JButton(
"試合開始" );
PlayButton.addActionListener( this
);
PlayButton.setActionCommand(
"Play" );
PlayButton.setBounds(
getContentPane().getWidth()/2,
getContentPane().getHeight()-20,
getContentPane().getWidth()/2, 20 );
getContentPane().add( PlayButton
);
setVisible( true );
}
public void actionPerformed(ActionEvent e)
{
if( e.getActionCommand().equals(
"Play" ) ) {
System.out.println(
String.valueOf( TeamSelect.size() ) );
setVisible( false );
MainFrame frame = new
MainFrame();
}
}
}
このコードだと要素数が0になってしまっています。
何が悪いのでしょうか。
きたないコードで申し訳ありません。
後のコードのInputFrameクラスは、違うクラスから呼び
出しているのでmainスレッドはありません。