So I'm trying to make it so that my JList consists of random values everytime I boot my program up (I was using a string array to randomize values from 2 different arrays). I have been struggling to get this working. The elements show when I use one array, but it doesn't work when I use the new array that consists of a string with 2 values from those 2 arrays. How can I make it so that the array with both random values from 2 arrays show up on my JList. I have tried everything starting with making a new array to using DeaultListModel. Any help is appreciated.
Here is my code:
public class game extends JFrame {
private JPanel contentPane;
private JTextField textField_1;
private JTextField textField_2;
//\u21BA is Skip, \u20E0 is Reverse. Action and colorName are the arrays I'm randomizing into the JList
private String[] action = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+2", "\u21BA", " \u20E0"};
private Color[] color = {Color.red, Color.blue, Color.green, Color.yellow};
private String[] colorName = {"Red", "Blue", "Green", "Yellow"};
private DefaultListModel deck;
/**
* Launch the application.
*/
public static void main (String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
game frame = new game();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public game() {
setTitle("Uno");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JTextArea textArea = new JTextArea();
textArea.setRows(5);
contentPane.add(textArea, BorderLayout.SOUTH);
\\What I'm struggling with
deck = new DefaultListModel();
for (int i = 0; i <= 7; i++) {
Random r = new Random();
deck.addElement(colorName[r.nextInt(colorName.length)] + " " + action[r.nextInt(action.length)]);
}
JList list = new JList(deck);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setFont(UIManager.getFont("ColorChooser.font"));
contentPane.add(list, BorderLayout.WEST);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.EAST);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[] {44, 0};
gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
JLabel lblNewLabel = new JLabel("Player 1 Card Amount");
lblNewLabel.setFont(new Font("Dialog", Font.PLAIN, 12));
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
gbc_lblNewLabel.gridx = 0;
gbc_lblNewLabel.gridy = 3;
panel.add(lblNewLabel, gbc_lblNewLabel);
textField_1 = new JTextField();
textField_1.setEditable(false);
textField_1.setFont(new Font("Dialog", Font.PLAIN, 12));
GridBagConstraints gbc_textField_1 = new GridBagConstraints();
gbc_textField_1.insets = new Insets(0, 0, 5, 0);
gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
gbc_textField_1.gridx = 0;
gbc_textField_1.gridy = 4;
panel.add(textField_1, gbc_textField_1);
textField_1.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("CPU Card Amount");
lblNewLabel_1.setFont(new Font("Dialog", Font.PLAIN, 12));
GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 0);
gbc_lblNewLabel_1.gridx = 0;
gbc_lblNewLabel_1.gridy = 6;
panel.add(lblNewLabel_1, gbc_lblNewLabel_1);
textField_2 = new JTextField();
textField_2.setFont(new Font("Dialog", Font.PLAIN, 12));
textField_2.setEditable(false);
GridBagConstraints gbc_textField_2 = new GridBagConstraints();
gbc_textField_2.fill = GridBagConstraints.HORIZONTAL;
gbc_textField_2.gridx = 0;
gbc_textField_2.gridy = 7;
panel.add(textField_2, gbc_textField_2);
textField_2.setColumns(10);
}
}
Aucun commentaire:
Enregistrer un commentaire