我需要確保當我選擇一個特定的單選按鈕時,會出現一個包含三個JSpinner和三個JLabel的JPanel。Java - JRadioButton選擇出現面板
我寫了這個代碼:
JRadioButton custom = new JRadioButton("Custom");
SpinnerModel spinnerModel = new SpinnerNumberModel(2, 0, 80, 1);
JSpinner height = new JSpinner(spinnerModel);
SpinnerModel spinnerModel2 = new SpinnerNumberModel(2, 0, 80, 1);
JSpinner width = new JSpinner(spinnerModel2);
SpinnerModel spinnerModelMine = new SpinnerNumberModel(1, 0, 99, 1);
JSpinner mines = new JSpinner(spinnerModelMine);
當我選擇自定義一個JRadioButton,此JPanel應該出現:
GroupLayout panelEstGroupLayout = new GroupLayout(panelEst);
panelEstGroupLayout.setHorizontalGroup(
panelEstGroupLayout.createParallelGroup(Alignment.LEADING)
.addGroup
(
panelEstGroupLayout.createSequentialGroup()
.addGap(5)
.addGroup
(
panelEstGroupLayout.createParallelGroup(Alignment.TRAILING)
.addComponent(startGame)
.addComponent(boxTotal, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
)
)
.addGroup
(
panelEstGroupLayout.createSequentialGroup()
.addContainerGap()
.addGroup
(
panelEstGroupLayout.createParallelGroup(Alignment.TRAILING)
.addGroup
(
Alignment.LEADING, panelEstGroupLayout.createSequentialGroup()
.addGap(6)
.addComponent(heightLabel, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(widthLabel, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(minesLabel, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
)
.addGroup
(
Alignment.LEADING, panelEstGroupLayout.createSequentialGroup()
.addComponent(height, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(width, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(mines, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)
)
)
)
);
panelEstGroupLayout.setVerticalGroup(
panelEstGroupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(panelEstGroupLayout.createSequentialGroup()
.addGap(5)
.addComponent(boxTotal, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGroup(panelEstGroupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(panelEstGroupLayout.createSequentialGroup()
.addGap(74)
.addComponent(startGame))
.addGroup(Alignment.TRAILING, panelEstGroupLayout.createSequentialGroup()
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(panelEstGroupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(heightLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(widthLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(minesLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(panelEstGroupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(height)
.addComponent(width, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(mines, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(43)))
.addGap(0))
);
我插入此代碼到如果與條件:if(custom.isSelected())
但它不沒有工作。爲什麼?
請提供[mcve]。 – Berger
您需要一個ActionListener來檢查是否選中該按鈕。否則,程序會檢查你的if點是否被選中。又是一個'GroupLayout'不是一個完整的面板或框架或任何 – XtremeBaumer
[如何寫一個動作監聽](https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html) – River