您好我尝试在现有JCheckBox
上添加JLabel或JPanel
,具体取决于按下按钮时选择的单选按钮,这是我的代码到目前为止:
package GUI;
import java.awt.FlowLayout;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
/**
*
* @author ipodi
*/
public class Note extends javax.swing.JFrame {
public Note() {
initComponents();
Options.add(rBtNote);
Options.add(rBtCheck);
setLocationRelativeTo(null);
Entries.setLayout(new FlowLayout());
}
private int noLabel = 0;
private int noCheck = 0;
@SuppressWarnings("unchecked")
private void initComponents() {
Options = new javax.swing.ButtonGroup();
jPanel1 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
info = new javax.swing.JLabel();
jPanel5 = new javax.swing.JPanel();
jPanel6 = new javax.swing.JPanel();
jButton2 = new javax.swing.JButton();
rBtNote = new javax.swing.JRadioButton();
rBtCheck = new javax.swing.JRadioButton();
jTextField1 = new javax.swing.JTextField();
Entries = new javax.swing.JPanel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
OpenMenu = new javax.swing.JMenuItem();
jMenu3 = new javax.swing.JMenu();
jSeparator2 = new javax.swing.JPopupMenu.Separator();
SaveMenu = new javax.swing.JMenuItem();
jSeparator3 = new javax.swing.JPopupMenu.Separator();
Exit = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
About = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMinimumSize(new java.awt.Dimension(100, 450));
jPanel1.setLayout(new java.awt.BorderLayout());
info.setText("jLabel1");
jPanel3.add(info);
jPanel1.add(jPanel3, java.awt.BorderLayout.PAGE_END);
getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);
jPanel5.setLayout(new java.awt.BorderLayout());
jButton2.setText("Add Entry");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
rBtNote.setText("Note");
rBtCheck.setText("Check list");
javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
jPanel6.setLayout(jPanel6Layout);
jPanel6Layout.setHorizontalGroup(
jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
.addGap(22, 22, 22)
.addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel6Layout.createSequentialGroup()
.addComponent(rBtNote)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jPanel6Layout.createSequentialGroup()
.addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel6Layout.createSequentialGroup()
.addComponent(rBtCheck)
.addGap(102, 102, 102)
.addComponent(jButton2))
.addComponent(jTextField1))
.addGap(42, 42, 42))))
);
jPanel6Layout.setVerticalGroup(
jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
.addContainerGap()
.addComponent(rBtNote)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(rBtCheck)
.addComponent(jButton2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel5.add(jPanel6, java.awt.BorderLayout.PAGE_END);
jPanel5.add(Entries, java.awt.BorderLayout.CENTER);
getContentPane().add(jPanel5, java.awt.BorderLayout.CENTER);
jMenu1.setText("File");
OpenMenu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
OpenMenu.setText("Open...");
jMenu1.add(OpenMenu);
jMenu3.setText("Recent Notes");
jMenu1.add(jMenu3);
jMenu1.add(jSeparator2);
SaveMenu.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
SaveMenu.setText("Save Note");
jMenu1.add(SaveMenu);
jMenu1.add(jSeparator3);
Exit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
Exit.setText("Exit Note");
Exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ExitActionPerformed(evt);
}
});
jMenu1.add(Exit);
jMenuBar1.add(jMenu1);
jMenu2.setText("About");
About.setText("About");
jMenu2.add(About);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
pack();
}// </editor-fold>
private void ExitActionPerformed(java.awt.event.ActionEvent evt) {
Selection S = new Selection();
if (SaveMenu.isEnabled() == true) {
int opt = JOptionPane.showConfirmDialog(rootPane, "Do you want to save the changes on the Note?", "Alert", JOptionPane.YES_NO_CANCEL_OPTION);
if (opt == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(rootPane, "YEY!!");
S.setVisible(true);
dispose();
} else if (opt == JOptionPane.NO_OPTION) {
S.setVisible(true);
dispose();
}
} else {
S.setVisible(true);
dispose();
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
if (jTextField1.getText().length() < 4) {
JOptionPane.showMessageDialog(rootPane, "Please set a word with minumum of 4 characters.");
} else {
int bug = 0;
if (rBtNote.isSelected()) {
noLabel++;
JLabel label = new JLabel(jTextField1.getText());
Entries.add(label);
label.setName("jl" + noLabel);
rBtNote.setSelected(false);
} else if (rBtCheck.isSelected()) {
noCheck++;
JCheckBox check = new JCheckBox(jTextField1.getText(), false);
Entries.add(check);
check.setName("ch" + noCheck);
rBtCheck.setSelected(false);
}
else{
JOptionPane.showMessageDialog(rootPane, "Please select an option.");
}
}
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Selection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Selection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Selection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Selection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Selection().setVisible(true);
}
});
}
private javax.swing.JMenuItem About;
private javax.swing.JPanel Entries;
private javax.swing.JMenuItem Exit;
private javax.swing.JMenuItem OpenMenu;
private javax.swing.ButtonGroup Options;
private javax.swing.JMenuItem SaveMenu;
private javax.swing.JLabel info;
private javax.swing.JButton jButton2;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel5;
private javax.swing.JPanel jPanel6;
private javax.swing.JPopupMenu.Separator jSeparator2;
private javax.swing.JPopupMenu.Separator jSeparator3;
private javax.swing.JTextField jTextField1;
private javax.swing.JRadioButton rBtCheck;
private javax.swing.JRadioButton rBtNote;
}
我知道所有的代码,但错误仍然在这个领域:
if (jTextField1.getText().length() < 4) {
JOptionPane.showMessageDialog(rootPane, "Please set a word with minumum of 4 characters.");
} else {
int bug = 0;
if (rBtNote.isSelected()) {
noLabel++;
JLabel label = new JLabel(jTextField1.getText());
Entries.add(label);
label.setName("jl" + noLabel);
rBtNote.setSelected(false);
} else if (rBtCheck.isSelected()) {
noCheck++;
JCheckBox check = new JCheckBox(jTextField1.getText(), false);
Entries.add(check);
check.setName("ch" + noCheck);
rBtCheck.setSelected(false);
}
else{
JOptionPane.showMessageDialog(rootPane, "Please select an option.");
}
}
}
当我运行它时没有错误,但它没有在JFrame
或JPanel
上显示。
有什么建议?