我知道如何使用java Random class随机数.
这将随机出现0-13 13次之间的数字;
public static void main(String[] args) {
int ctr = 13;
int randomNum = 0;
while(ctr != 0) {
Random r = new Random();
randomNum = r.nextInt(13);
ctr--;
System.out.println(ctr +": " + randomNum);
}
}
Run Code Online (Sandbox Code Playgroud)
题
- 我想在0-13之间随机输入13次
- 如果第一个随机数是例如(5),那么我的第二个随机数将从0-13中随机任意一个数字,排除5 ;
如果第二个随机数是例如(4),那么我的第三个随机数将从0-13中随机任意一个,排除5和4 ; 等等.有没有办法做到这一点?
我使用了FormLayout,我想将所有组件(JLabel和JtextField)设置在JFrame的中心。
我尝试使用panel.setAlignmentX和panel.setAlignmentY,但是它不起作用。
panel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.setAlignmentY(Component.CENTER_ALIGNMENT);
Run Code Online (Sandbox Code Playgroud)
下面显示的是我的代码,请帮助谢谢。
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
@SuppressWarnings("serial")
public class UI extends JFrame {
private JPanel panel = new JPanel();
public UI() {
FormLayout layout = new FormLayout(
"pref, pref, pref, pref",
"pref, pref, pref, pref");
panel.setLayout(layout);
CellConstraints cc = new CellConstraints();
panel = new JPanel (layout);
panel.add(new JLabel("L1 "), cc.xy(2, 2));
panel.add(new JTextField(15), cc.xy(3, 2));
panel.add(new JLabel("L2 "), cc.xy(2, 3));
panel.add(new JTextField(15), cc.xy(3, 3));
panel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.setAlignmentY(Component.CENTER_ALIGNMENT);
add(panel); …Run Code Online (Sandbox Code Playgroud)