我開始與Java和我有一個簡單的問題,我想得到更多我的JCheckBox被選中或沒有。爲此,我知道我必須使用comboBox.isSelected(),但是在我想使用它的方法中,我無法引用對象JCheckBox。下面的代碼:檢查JRadioButton是否被選中
import java.awt.BorderLayout;
public class AgregarPlato extends JDialog {
private final JPanel contentPanel = new JPanel();
public static void main(String[] args) {
try {
AgregarPlato dialog = new AgregarPlato();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public AgregarPlato() {
setBounds(100, 100, 546, 459);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
JRadioButton radio = new JRadioButton("\u00BFDesea llevar Stock?");
radio.setFont(new Font("Tahoma", Font.PLAIN, 11));
radio.setBounds(91, 207, 168, 23);
contentPanel.add(radio);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton aceptarButton = new JButton("Aceptar");
aceptarButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (radio.isSelected()) {
System.out.println("It doesnt work");
}
}
});
aceptarButton.setActionCommand("OK");
buttonPane.add(aceptarButton);
getRootPane().setDefaultButton(aceptarButton);
}
{
JButton cancelarButton = new JButton("Cancelar");
cancelarButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
cancelarButton.setActionCommand("Cancel");
buttonPane.add(cancelarButton);
}
}
}
}
問題糾正:) –
它的工作原理!非常感謝你! 此外,我想製作一個組合框,其中的項目必須是sql查詢的結果,例如 SELECT name FROM category,並且ComboBox必須顯示該表的所有名稱,它將如何? 對不起,我是全新的編程和Java! 非常感謝你! –
這聽起來像是一個新帖子的主題 – Reimeus